vim /etc/ansible/ansible.cfg
ssh_args = -C -o ControlMaster=auto -o ControlPersist=7200s
·
SSH pipelining 是一个加速 Ansible 执行速度的简单方法,默认是关闭的。之所以默认关闭是为了兼容不同的 sudo 配置,主要是 requiretty 选项。如果不使用 sudo,建议开启 SSH pipelining。
打开此选项可以减少 Ansible 在执行时不进行脚本 SSH 传输的功能。不过,如果使用 sudo,必须关闭 requiretty 选项。
vim /etc/ansible/ansible.cfg
pipelining = true
·
注意: 如果使用 Redis 缓存 Facts 信息,需要执行 "pip install redis"
安装 Redis 插件。
国内常用pip源地址:
pip install redis -i http://mirrors.aliyun.com/pypi/simple/
vim /etc/ansible/ansible.cfg
gathering = smart
#smart:默认收集 facts,但 facts 已有的情况下不会收集。
#implicit:默认收集 facts,要禁止收集,必须使用 gather_facts: False。
#explicit:默认不收集,要显式收集,必须使用 gather_facts: Ture。
fact_caching = jsonfile
#redis:缓存到redis
#jsonfile:缓存到文本地
fact_caching_connection = /etc/ansible/facts_cache/
#fact_caching = jsonfile时,指定本地缓存路径
#fact_caching = redis时,指定redis地址。
#+ 例如:fact_caching_connection = 192.168.1.100:6379:0
#+ 如果redis配置了密码:fact_caching_connection = 192.168.1.100:6379:0:password
fact_caching_timeout = 86400
#缓存时间,单位为秒。需要新增
·
·
linear 策略即线性执行策略。线性执行策略指主机组内所有主机完成一个任务后才继续下一个任务的执行。
说直白点就是第一个任务在指定的主机都执行完,再进行第二个任务的执行,第二个任务在指定的主机都执行完后,再进行第三个任务的执行…… 以此类推。比如:执行时指定并发数为5,表示5台机器都完成第一个任务时,才统一进行第二个任务,…以此类推。
ansible-playbook playbook.yml -f 5
#或者在配置文件/etc/ansible/ansible.cfg指定并发数。
#+ forks = 5
·
free 策略即自由策略,即在一个 play 执行完之前,每个主机都各顾各的尽可能快的完成 play 里的所有任务,而不会因为其他主机没执行完任务而等待,不受线性执行策略那样的约束。所以这种策略的执行结果是无序的甚至是杂乱无章的,而且每次执行结果的task显示顺序很可能不一样。
vim /etc/ansible/ansible.cfg
strategy = free
·
异步模式:Ansible 一开始会将节点的任务扔在后台,并且每隔一段时间去检查这些节点的任务完成情况,直到检测到任务完成才返回信息给 Ansible,然后继续执行下一个任务,以此类推,直到完成。
在异步模式下,如果设置的检查时间间隔(poll)为 0,Ansible 将任务扔在后台,会立即返回信息给 Ansible。如果想要看到执行结果,需要使用 Ansible 提供的 async_status 这个模块。
·
---
- hosts: wpf002
remote_user: root
tasks:
- name: simulate long running op (5 sec), wait for up to 6 sec, poll every 2 sec
command: /bin/sleep 5
async: 6
poll: 2
register: sleep_result
- name: show sleep_result
debug:
var: sleep_result
async:这个任务执行时间的上限值。即如果任务执行超出这个时间,则认为任务失败。此参数若未设置,则为同步执行。
poll:任务异步执行时轮询的时间间隔。
·
pool 为 0 时,会立即返回信息给 ansible,然后后台继续执行任务。
[root@wpf ~]# ansible all -i wpf002, -m command -a 'sleep 5' -B 6 -P 0
wpf002 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"ansible_job_id": "732670762688.32132",
"changed": true,
"finished": 0,
"results_file": "/root/.ansible_async/732670762688.32132",
"started": 1
}
从返回信息可以看到一个 ansible_job_id,使用 async_status 模块查看这个 ansible_job_id,就可以得到此任务的执行结果。
·
---
- hosts: wpf002
remote_user: root
tasks:
- name: 'get job_result'
async_status: jid=732670762688.32132
register: job_result
- name: 'show job_result'
debug: var=job_result