Ansible教程:ansible.cfg配置文件 作者:马育民 • 2026-09-20 15:15 • 阅读:10002 # 介绍 Ansible 核心配置文件:`/etc/ansible/ansible.cfg`(全局) 也可以在项目目录下放 `ansible.cfg`(项目局部,优先级更高),还支持环境变量。 查找当前生效配置:`ansible-config dump` # 常用配置项 ```ini [defaults] # 1. inventory 主机清单文件路径 inventory = ./inventory # 2. 默认远程登录用户 remote_user = root # 3. 主机密钥检查(首次ssh连接是否询问yes/no,生产建议开启,测试关闭) host_key_checking = False # 4. 并发执行数量,默认5 forks = 10 # 5. 超时时间,单位秒 timeout = 30 # 6. 角色存放目录 roles_path = ./roles:/etc/ansible/roles # 7. 主机清单缓存,提升大清单加载速度 inventory_cache_enabled = True # 8. 日志文件,开启后记录执行日志 log_path = /var/log/ansible.log # 9. 禁用弃用警告 deprecation_warnings = False # 10. 默认是否开启sudo提权 become = True # sudo提权用户 become_user = root # 提权方式:sudo / su become_method = sudo # 提权密码,不建议写在配置里,命令行 -K 输入 # become_pass = xxx # 11. 模块缓存,提升模块执行速度 module_cache = ./module_cache # 12. 收集主机facts信息,不需要可以关闭加速 gather_facts = True [privilege_escalation] # 提权相关,和上面defaults的become对应 become=True become_method=sudo become_user=root become_ask_pass=False [ssh_connection] # ssh管道加速,推荐开启 pipelining = True # ssh控制套接字复用,持久连接,大幅提速 control_path = %(directory)s/ansible-ssh-%%h-%%p-%%r control_path_dir = ~/.ansible/cp ssh_args = -o ControlMaster=auto -o ControlPersist=60s # 关闭sftp,改用scp传输 scp_if_ssh = True ``` 原文出处:/show_1GW45CVJs81v.html