Ansible 配置文件,由浅入深的全方位指南

运维
Ansible​配置文件真的是Ansible​自动化流程中“隐秘而伟大”的存在。只要咱们把配置文件这些弯弯绕绕的内容吃透,就能让整个自动化流程更加丝滑,提升效率又增加系统可靠性。

在自动化运维的广阔天地里,Ansible就是我们披荆斩棘的得力宝剑,而Ansible配置文件无疑是握住这把剑剑柄的关键所在。那它到底是怎么回事呢?下面咱们就来一探究竟。

文件位置和优先级

Ansible支持多级配置文件,优先级从高到低依次为:

  • 环境变量:通过 ANSIBLE_CONFIG 指定路径。
  • 当前目录:./ansible.cfg(项目级配置)。
  • 用户目录:~/.ansible.cfg(用户级配置)。
  • 系统默认:/etc/ansible/ansible.cfg(全局配置)。

可以通过以下命令查看生效的ansible.cfg文件。

root@code-server:~# ansible --version
ansible [core 2.17.10]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /root/.local/pipx/venvs/ansible-core/lib/python3.10/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /root/.local/bin/ansible
  python version = 3.10.12 (main, Feb  4 2025, 14:57:36) [GCC 11.4.0] (/root/.local/pipx/venvs/ansible-core/bin/python)
  jinja version = 3.1.6
  libyaml = True
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

配置文件长啥样?

先看看Ansible配置文件的骨架。它像一个大仓库,里面划分了好几个不同功能的区域。

root@code-server:~# grep -E '^\[' /etc/ansible/ansible.cfg
[defaults]
[inventory]
[privilege_escalation]
[paramiko_connection]
[ssh_connection]
[persistent_connection]
[accelerate]
[selinux]
[colors]
[diff]
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • [defaults]区,这里面装着Ansible操作通用的默认设置,就像是操作系统的初始配置,为后续各种操作奠定基础。
  • [inventory]区也很重要,它专门负责和主机清单打交道,确定哪里去找那些等着被管理的主机。
  • [privilege_escalation]区又是干啥的呢?它是用来设置特权升级相关选项的。打个比方,就像给特定人员发放“特殊通行卡”,可以让Ansible在执行任务时拥有更多权限。
  • [paramiko_connection]区和[ssh_connection]区,分别对应着使用不同库(Paramiko库和OpenSSH库)来建立SSH连接的选项,好比是不同的工具开锁,选择合适的“钥匙”才能打开与受管主机通信的“大门” 。
  • [persistent_connection]区负责配置持久连接选项,要是把和主机的连接看作是打电话,这里就是设置怎么保持“长线通话”不中断 。
  • [accelerate]区能开启加速模式选项,给Ansible工作起来"踩踩油门",提升运行效率。
  • [selinux]区针对SELinux相关内容进行设置,保障系统安全策略方面不出问题。
  • [colors]区趣味性就比较强啦,它负责设置Ansible命令输出的颜色选项,给单调的运维工作增添点色彩。
  • [diff]区决定是否打印任务前后的差异,这就像是在对比两份文件,明确哪里发生了改变 。

核心配置说明

以下是一个典型 ansible.cfg 的结构示例及关键参数解析:

[defaults] 模块:基础行为控制

[defaults]
# 定义库存文件路径(支持多个文件或目录)
inventory = ./inventory/
# 默认远程用户(可被 playbook 覆盖)
remote_user = admin
# 是否收集主机 facts 信息
gathering = smart
# 是否检查主机 SSH 密钥
host_key_checking = False
# 并发进程数(提升执行速度)
forks = 50
# 日志记录路径
log_path = /var/log/ansible.log
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

[privilege_escalation] 模块:权限提升配置

[privilege_escalation]
# 是否启用 sudo/su 提权
become = True
# 提权使用的命令(sudo/su/doas)
become_method = sudo
# 提权后的用户(默认 root)
become_user = root
# 是否询问 sudo 密码
become_ask_pass = False
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

[ssh_connection] 模块:SSH 连接优化

[ssh_connection]
# 启用 SSH 管道加速(减少连接次数)
pipelining = True
# 超时时间(单位秒)
timeout = 30
# 启用 ControlPersist 长连接
control_path = %(directory)s/ansible-ssh-%%h-%%p-%%r
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

[persistent_connection] 模块:连接复用

[persistent_connection]
# 连接保持时间(秒)
connect_timeout = 30
# 命令超时时间
command_timeout = 60
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

[inventory] 模块:库存扩展配置

[inventory]
enable_plugins = host_list,yaml,ini
# 启用动态库存缓存
cache = yes
# 缓存过期时间(秒)
cache_plugin = jsonfile
cache_timeout = 3600
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

[colors] 模块:输出颜色定制

[colors]
# 自定义输出颜色(如成功、错误状态)
highlight = white
verbose = blue
error = red
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

高级设置和优化方法

(1) 性能优化

  • 并发与管道:通过 forks 增加并行任务数,启用 pipelining 减少SSH开销。
  • Fact缓存:使用Redis或 JSON文件缓存facts,避免重复收集:
[defaults]
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_facts
  • 1.
  • 2.
  • 3.

(2) 插件管理:

启用/禁用插件:如禁用 host_key_checking 插件加速测试环境执行。

自定义模块路径:

[defaults]
library = /usr/share/ansible/custom_modules/
  • 1.
  • 2.

(3) 环境变量注入

通过 [defaults] 的 environment 参数向远程主机传递变量:

[defaults]
environment = {"http_proxy": "http://proxy.example.com:8080"}
  • 1.
  • 2.

配置示例

[defaults]
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_facts
inventory      = ./inventory
fork           = 20
ask_pass       = False
remote_user    =  root
log_path       = /var/log/ansible.log
host_key_checking = False
ansible_python_interpreter = /usr/bin/python3.9
[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

总结

Ansible配置文件真的是Ansible自动化流程中“隐秘而伟大”的存在。只要咱们把配置文件这些弯弯绕绕的内容吃透,就能让整个自动化流程更加丝滑,提升效率又增加系统可靠性。还等什么?赶紧上手搞懂它,让Ansible为你的自动化运维助力腾飞。

责任编辑:赵宁宁 来源: 攻城狮成长日记
相关推荐

2009-12-21 13:06:05

WCF Address

2009-09-17 09:01:10

CCNA学习指南CCNA

2009-12-11 11:23:22

策略路由配置

2022-06-06 08:47:32

Ansible配置文件

2020-07-31 07:54:15

Windows 10安全微软

2016-12-14 14:43:11

ButterknifeAndroid

2017-12-30 09:45:50

Linux发行版面向孩子

2009-10-26 13:41:49

机房监控

2010-08-04 14:15:44

nfs服务

2024-02-26 13:52:00

微服务Kubernetes.NET

2009-08-24 10:39:12

思科认证CCNA思科认证CCNA

2009-12-15 10:10:42

Ruby过程对象

2009-12-08 10:33:23

PHP应用问题

2009-09-08 09:46:44

思科认证介绍思科认证

2014-05-16 10:59:35

RDS 策略组策略对象

2009-11-03 14:26:13

EPON接入技术

2022-08-27 21:31:04

Tauri框架二进制

2011-03-21 15:08:56

MongoDBCouchDB

2021-08-24 07:57:26

KafkaRocketMQPulsar

2009-10-20 14:37:34

VB.NET文件操作
点赞
收藏

51CTO技术栈公众号