基本

ansible速度优化

  1. #M0 开启执行速度记录插件
  2. callback_whitelist = profile_tasks
  3. ANSIBLE_CALLBACK_WHITELIST = profile_tasks
  4. #M1.1 如可以关闭gather_fact
  5. #M1.2 设置fact缓存(支持jsonfile和redis)
  6. 设置facts缓存(缓存支持jsonfileredis)
  7. gathering: smart|implicit|explicit
  8. smart表示默认收集facts,但facts已有的情况下不会收集,即使用缓存facts
  9. implicit表示默认收集facts,要禁止收集,必须使用gather_facts: False
  10. explicit则表示默认不收集,要显式收集,必须使用gather_facts: Ture
  11. [defaults]
  12. gathering = smart
  13. fact_caching_timeout = 86400
  14. fact_caching = jsonfile
  15. fact_caching_connection = /path/to/cachedir
  16. #M1.3 关闭秘钥检测
  17. host_key_checking = False
  18. ANSIBLE_HOST_KEY_CHECKING = False
  19. #M1.4 关闭OpenSSH服务的DNS PTR
  20. sed -i '/^GSSAPI/s/yes/no/g;/UseDNS/d;/Protocol/aUseDNS no' /etc/ssh/sshd_config
  21. #M2 采用ssh多路复用(长连接方式)
  22. ssh_args = -o ControlMaster=auto -o ControlPersist=60s
  23. ANSIBLE_SSH_ARGS = -C -o ControlMaster=auto -o ControlPersist=5d
  24. #M3 采用Pipline
  25. #https://docs.ansible.com/ansible/latest/reference_appendices/config.html
  26. ANSIBLE_SSH_PIPELINING = True cat /etc/sudoers | grep requiretty
  27. #M4 修改task执行策略
  28. #修改ansible执行策略,需要ansible2.0版本以上,修改默认的配置linear --> strategy
  29. #M5 使用Mitogen的plugin插件
  30. https://mitogen.networkgenomics.com/howitworks.html
  31. wget https://files.pythonhosted.org/packages/source/m/mitogen/mitogen-0.2.7.tar.gz
  32. tar axf mitogen-0.2.7.tar.gz -C /opt/
  33. [defaults]
  34. strategy_plugins = /opt/mitogen-0.2.7/ansible_mitogen/plugins/strategy
  35. strategy = mitogen_linear

变量优先级

以下为优先级从低到高 注意:第一个不是命令行-e提供的变量, -e提供的是额外的变量是最后一个

command line values (eg “-u user”)
role defaults [1]
inventory file or script group vars [2]
inventory group_vars/all [3]
playbook group_vars/all [3]
inventory group_vars/* [3]
playbook group_vars/* [3]
inventory file or script host vars [2]
inventory host_vars/* [3]
playbook host_vars/* [3]
host facts / cached set_facts [4]
play vars
play vars_prompt
play vars_files
role vars (defined in role/vars/main.yml)
block vars (only for tasks in block)
task vars (only for the task)
include_vars
set_facts / registered vars
role (and include_role) params
include params
extra vars (always win precedence)

常见

问题:Jinja2模板空白控制及换行

#Template like this 
solr.replication.master=
    {% if ansible_eth0.ipv4.address == servermaster.eth0 %}
        false
    {% else %}
        true
    {% endif %}

solr.replication.slave=false
#And the output should look like this:
solr.replication.master=true
solr.replication.slave=false
#What I am actually getting is:
solr.replication.master=truesolr.replication.slave=false

解决: 在jinja2模板第一行加上 #jinja2: trim_blocks:False 参考

参考文档