优秀文章:https://www.cnblogs.com/hujinzhong/p/12155691.html 1.安装 yum install epel-release -y yum install ansible –y 2.配置文件 [root@OP ansible]# grep -vE '^#|^$|^\[' ansible.cfg gathering = smart host_key_checking = False timeout = 5 log_path = /var/log/ansible.log module_name = shell private_key_file = ~/.ssh/new_www.key fact_caching = memory pipelining = True inventory = /etc/ansible/hosts # 这个参数表示资源清单inventory文件的位置 library = /usr/share/ansible # 指向存放Ansible模块的目录,支持多个目录方式,只要用冒号(:)隔开就可以 forks = 5 # 并发连接数,默认为5 sudo_user = root # 设置默认执行命令的用户 remote_port = 22 # 指定连接被管节点的管理端口,默认为22端口,建议修改,能够更加安全 host_key_checking = False # 设置是否检查SSH主机的密钥,值为True/False。关闭后第一次连接不会提示配置实例 timeout = 60 # 设置SSH连接的超时时间,单位为秒 log_path = /var/log/ansible.log # 指定一个存储ansible日志的文件(默认不记录日志) 3.优化-facts缓存 Ansible 1.8 版本开始,引入了 facts 缓存功能。 Ansible 的配置文件中可以修改 gathering 的值为 smart、implicit 或者 explicit。 smart 表示默认收集 facts,但 facts 已有的情况下不会收集,即使用缓存 facts; implicit 表示默认收集 facts,要禁止收集,必须使用 gather_facts: False; explicit 则表示默认不收集,要显式收集,必须使用 gather_facts: Ture。 在使用 facts 缓存时(即设置为 smart),Ansible 支持两种 facts 缓存:redis 和 jsonfile。 安装Python Redis模块 easy_install pip pip install redis 使用 redis 缓存 gathering = smart fact_caching_timeout = 86400 fact_caching = redis fact_caching_connection = 127.0.0.1:6379 # 若 redis 设置了密码 # fact_caching_connection = localhost:6379:0:admin 在使用 redis 缓存后,出现异常(若未出现,请忽略):TypeError: the JSON object must be str, not 'bytes', 加上 -vvv 打印出调试信息后,定位到 /usr/local/lib/python3.5/dist-packages/ansible-2.5.0-py3.5.egg/ansible/plugins/cache/redis.py 文件中的 第 90 行: self._cache[key] = json.loads(value) 将其修改为如下即可: self._cache[key] = json.loads(value.decode('utf-8')) 4.执行简单命令测试 ansible test -a 'w' ansible all -a "uptime" ansible sh_dev -m command -a "w" # 模块执行 ansible sh_dev -a "w" # 执行命令 ansible sh_dev --list-hosts # 查看组里的主机