Ansible 配置文件,由淺入深的全方位指南
在自動(dòng)化運(yùn)維的廣闊天地里,Ansible就是我們披荊斬棘的得力寶劍,而Ansible配置文件無疑是握住這把劍劍柄的關(guān)鍵所在。那它到底是怎么回事呢?下面咱們就來一探究竟。
文件位置和優(yōu)先級(jí)
Ansible支持多級(jí)配置文件,優(yōu)先級(jí)從高到低依次為:
- 環(huán)境變量:通過 ANSIBLE_CONFIG 指定路徑。
- 當(dāng)前目錄:./ansible.cfg(項(xiàng)目級(jí)配置)。
- 用戶目錄:~/.ansible.cfg(用戶級(jí)配置)。
- 系統(tǒng)默認(rèn):/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
配置文件長啥樣?
先看看Ansible配置文件的骨架。它像一個(gè)大倉庫,里面劃分了好幾個(gè)不同功能的區(qū)域。
root@code-server:~# grep -E '^\[' /etc/ansible/ansible.cfg
[defaults]
[inventory]
[privilege_escalation]
[paramiko_connection]
[ssh_connection]
[persistent_connection]
[accelerate]
[selinux]
[colors]
[diff]
- [defaults]區(qū),這里面裝著Ansible操作通用的默認(rèn)設(shè)置,就像是操作系統(tǒng)的初始配置,為后續(xù)各種操作奠定基礎(chǔ)。
- [inventory]區(qū)也很重要,它專門負(fù)責(zé)和主機(jī)清單打交道,確定哪里去找那些等著被管理的主機(jī)。
- [privilege_escalation]區(qū)又是干啥的呢?它是用來設(shè)置特權(quán)升級(jí)相關(guān)選項(xiàng)的。打個(gè)比方,就像給特定人員發(fā)放“特殊通行卡”,可以讓Ansible在執(zhí)行任務(wù)時(shí)擁有更多權(quán)限。
- [paramiko_connection]區(qū)和[ssh_connection]區(qū),分別對(duì)應(yīng)著使用不同庫(Paramiko庫和OpenSSH庫)來建立SSH連接的選項(xiàng),好比是不同的工具開鎖,選擇合適的“鑰匙”才能打開與受管主機(jī)通信的“大門” 。
- [persistent_connection]區(qū)負(fù)責(zé)配置持久連接選項(xiàng),要是把和主機(jī)的連接看作是打電話,這里就是設(shè)置怎么保持“長線通話”不中斷 。
- [accelerate]區(qū)能開啟加速模式選項(xiàng),給Ansible工作起來"踩踩油門",提升運(yùn)行效率。
- [selinux]區(qū)針對(duì)SELinux相關(guān)內(nèi)容進(jìn)行設(shè)置,保障系統(tǒng)安全策略方面不出問題。
- [colors]區(qū)趣味性就比較強(qiáng)啦,它負(fù)責(zé)設(shè)置Ansible命令輸出的顏色選項(xiàng),給單調(diào)的運(yùn)維工作增添點(diǎn)色彩。
- [diff]區(qū)決定是否打印任務(wù)前后的差異,這就像是在對(duì)比兩份文件,明確哪里發(fā)生了改變 。
核心配置說明
以下是一個(gè)典型 ansible.cfg 的結(jié)構(gòu)示例及關(guān)鍵參數(shù)解析:
[defaults] 模塊:基礎(chǔ)行為控制
[defaults]
# 定義庫存文件路徑(支持多個(gè)文件或目錄)
inventory = ./inventory/
# 默認(rèn)遠(yuǎn)程用戶(可被 playbook 覆蓋)
remote_user = admin
# 是否收集主機(jī) facts 信息
gathering = smart
# 是否檢查主機(jī) SSH 密鑰
host_key_checking = False
# 并發(fā)進(jìn)程數(shù)(提升執(zhí)行速度)
forks = 50
# 日志記錄路徑
log_path = /var/log/ansible.log
[privilege_escalation] 模塊:權(quán)限提升配置
[privilege_escalation]
# 是否啟用 sudo/su 提權(quán)
become = True
# 提權(quán)使用的命令(sudo/su/doas)
become_method = sudo
# 提權(quán)后的用戶(默認(rèn) root)
become_user = root
# 是否詢問 sudo 密碼
become_ask_pass = False
[ssh_connection] 模塊:SSH 連接優(yōu)化
[ssh_connection]
# 啟用 SSH 管道加速(減少連接次數(shù))
pipelining = True
# 超時(shí)時(shí)間(單位秒)
timeout = 30
# 啟用 ControlPersist 長連接
control_path = %(directory)s/ansible-ssh-%%h-%%p-%%r
[persistent_connection] 模塊:連接復(fù)用
[persistent_connection]
# 連接保持時(shí)間(秒)
connect_timeout = 30
# 命令超時(shí)時(shí)間
command_timeout = 60
[inventory] 模塊:庫存擴(kuò)展配置
[inventory]
enable_plugins = host_list,yaml,ini
# 啟用動(dòng)態(tài)庫存緩存
cache = yes
# 緩存過期時(shí)間(秒)
cache_plugin = jsonfile
cache_timeout = 3600
[colors] 模塊:輸出顏色定制
[colors]
# 自定義輸出顏色(如成功、錯(cuò)誤狀態(tài))
highlight = white
verbose = blue
error = red
高級(jí)設(shè)置和優(yōu)化方法
(1) 性能優(yōu)化
- 并發(fā)與管道:通過 forks 增加并行任務(wù)數(shù),啟用 pipelining 減少SSH開銷。
- Fact緩存:使用Redis或 JSON文件緩存facts,避免重復(fù)收集:
[defaults]
fact_caching = jsonfile
fact_caching_connection = /tmp/ansible_facts
(2) 插件管理:
啟用/禁用插件:如禁用 host_key_checking 插件加速測(cè)試環(huán)境執(zhí)行。
自定義模塊路徑:
[defaults]
library = /usr/share/ansible/custom_modules/
(3) 環(huán)境變量注入
通過 [defaults] 的 environment 參數(shù)向遠(yuǎn)程主機(jī)傳遞變量:
[defaults]
environment = {"http_proxy": "http://proxy.example.com:8080"}
配置示例
[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
總結(jié)
Ansible配置文件真的是Ansible自動(dòng)化流程中“隱秘而偉大”的存在。只要咱們把配置文件這些彎彎繞繞的內(nèi)容吃透,就能讓整個(gè)自動(dòng)化流程更加絲滑,提升效率又增加系統(tǒng)可靠性。還等什么?趕緊上手搞懂它,讓Ansible為你的自動(dòng)化運(yùn)維助力騰飛。