博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
5.ansible异步
阅读量:6082 次
发布时间:2019-06-20

本文共 11445 字,大约阅读时间需要 38 分钟。

hot3.png

1.ansible异步:    主要目的是在执行多台服务器而且当单个任务执行完毕时长过长甚至会超过ssh超时时间,或执行一批任务时时长过长的问题     例如:1.执行一个任务花时20分钟ansible-playbook/ansible会长时间挂在前台,有可能运行时间比SSH超时时间还要长          2.为二十台服务器执行一个任务每个任务花费1分钟,那么二十台服务器总共执行完毕需要二十分钟这也是不友好的    解决该问题最简单的方式是异步执行它们,然后轮询直到任务执行完毕.2.异步并查看任务情况[root@web2 asynctest]# ansible-playbook  asynctest.yml -vvUsing /etc/ansible/ansible.cfg as config filePLAYBOOK: asynctest.yml ********************************************************************************************1 plays in asynctest.ymlPLAY [cluster] *****************************************************************************************************TASK [Gathering Facts] *********************************************************************************************ok: [192.168.222.139]ok: [192.168.222.142]ok: [192.168.222.140]ok: [192.168.222.141]META: ran handlersTASK [command] *****************************************************************************************************task path: /root/asynctest/asynctest.yml:5changed: [192.168.222.140] => {"ansible_job_id": "672788718586.6870", "changed": true, "finished": 0, "results_file": "/root/.ansible_async/672788718586.6870", "started": 1}changed: [192.168.222.142] => {"ansible_job_id": "49954320951.6876", "changed": true, "finished": 0, "results_file": "/root/.ansible_async/49954320951.6876", "started": 1}changed: [192.168.222.141] => {"ansible_job_id": "36457841980.6875", "changed": true, "finished": 0, "results_file": "/root/.ansible_async/36457841980.6875", "started": 1}changed: [192.168.222.139] => {"ansible_job_id": "579208698367.6879", "changed": true, "finished": 0, "results_file": "/root/.ansible_async/579208698367.6879", "started": 1}TASK [debug] *******************************************************************************************************task path: /root/asynctest/asynctest.yml:10ok: [192.168.222.139] => {    "result": {        "ansible_job_id": "579208698367.6879",         "changed": true,         "finished": 0,         "results_file": "/root/.ansible_async/579208698367.6879",         "started": 1    }}ok: [192.168.222.140] => {    "result": {        "ansible_job_id": "672788718586.6870",         "changed": true,         "finished": 0,         "results_file": "/root/.ansible_async/672788718586.6870",         "started": 1    }}ok: [192.168.222.141] => {    "result": {        "ansible_job_id": "36457841980.6875",         "changed": true,         "finished": 0,         "results_file": "/root/.ansible_async/36457841980.6875",         "started": 1    }}ok: [192.168.222.142] => {    "result": {        "ansible_job_id": "49954320951.6876",         "changed": true,         "finished": 0,         "results_file": "/root/.ansible_async/49954320951.6876",         "started": 1    }}TASK [async_status] ************************************************************************************************task path: /root/asynctest/asynctest.yml:12FAILED - RETRYING: async_status (30 retries left).FAILED - RETRYING: async_status (30 retries left).FAILED - RETRYING: async_status (30 retries left).FAILED - RETRYING: async_status (30 retries left).FAILED - RETRYING: async_status (29 retries left).FAILED - RETRYING: async_status (29 retries left).FAILED - RETRYING: async_status (29 retries left).FAILED - RETRYING: async_status (29 retries left).FAILED - RETRYING: async_status (28 retries left).FAILED - RETRYING: async_status (28 retries left).FAILED - RETRYING: async_status (28 retries left).FAILED - RETRYING: async_status (28 retries left).FAILED - RETRYING: async_status (27 retries left).FAILED - RETRYING: async_status (27 retries left).FAILED - RETRYING: async_status (27 retries left).FAILED - RETRYING: async_status (27 retries left).FAILED - RETRYING: async_status (26 retries left).FAILED - RETRYING: async_status (26 retries left).FAILED - RETRYING: async_status (26 retries left).FAILED - RETRYING: async_status (26 retries left).FAILED - RETRYING: async_status (25 retries left).FAILED - RETRYING: async_status (25 retries left).FAILED - RETRYING: async_status (25 retries left).FAILED - RETRYING: async_status (25 retries left).FAILED - RETRYING: async_status (24 retries left).FAILED - RETRYING: async_status (24 retries left).FAILED - RETRYING: async_status (24 retries left).FAILED - RETRYING: async_status (24 retries left).FAILED - RETRYING: async_status (23 retries left).FAILED - RETRYING: async_status (23 retries left).FAILED - RETRYING: async_status (23 retries left).FAILED - RETRYING: async_status (23 retries left).FAILED - RETRYING: async_status (22 retries left).FAILED - RETRYING: async_status (22 retries left).FAILED - RETRYING: async_status (22 retries left).FAILED - RETRYING: async_status (22 retries left).FAILED - RETRYING: async_status (21 retries left).FAILED - RETRYING: async_status (21 retries left).FAILED - RETRYING: async_status (21 retries left).FAILED - RETRYING: async_status (21 retries left).FAILED - RETRYING: async_status (20 retries left).FAILED - RETRYING: async_status (20 retries left).FAILED - RETRYING: async_status (20 retries left).FAILED - RETRYING: async_status (20 retries left).FAILED - RETRYING: async_status (19 retries left).FAILED - RETRYING: async_status (19 retries left).FAILED - RETRYING: async_status (19 retries left).FAILED - RETRYING: async_status (19 retries left).FAILED - RETRYING: async_status (18 retries left).FAILED - RETRYING: async_status (18 retries left).FAILED - RETRYING: async_status (18 retries left).FAILED - RETRYING: async_status (18 retries left).FAILED - RETRYING: async_status (17 retries left).FAILED - RETRYING: async_status (17 retries left).FAILED - RETRYING: async_status (17 retries left).FAILED - RETRYING: async_status (17 retries left).FAILED - RETRYING: async_status (16 retries left).FAILED - RETRYING: async_status (16 retries left).FAILED - RETRYING: async_status (16 retries left).FAILED - RETRYING: async_status (16 retries left).FAILED - RETRYING: async_status (15 retries left).FAILED - RETRYING: async_status (15 retries left).FAILED - RETRYING: async_status (15 retries left).FAILED - RETRYING: async_status (15 retries left).FAILED - RETRYING: async_status (14 retries left).FAILED - RETRYING: async_status (14 retries left).FAILED - RETRYING: async_status (14 retries left).FAILED - RETRYING: async_status (14 retries left).FAILED - RETRYING: async_status (13 retries left).FAILED - RETRYING: async_status (13 retries left).FAILED - RETRYING: async_status (13 retries left).FAILED - RETRYING: async_status (13 retries left).changed: [192.168.222.139] => {"ansible_job_id": "579208698367.6879", "attempts": 19, "changed": true, "cmd": "sleep 100 && hostname", "delta": "0:01:40.021598", "end": "2017-08-05 15:49:07.175202", "finished": 1, "rc": 0, "start": "2017-08-05 15:47:27.153604", "stderr": "", "stderr_lines": [], "stdout": "web2", "stdout_lines": ["web2"]}changed: [192.168.222.141] => {"ansible_job_id": "36457841980.6875", "attempts": 19, "changed": true, "cmd": "sleep 100 && hostname", "delta": "0:01:40.024560", "end": "2017-08-05 15:49:07.158323", "finished": 1, "rc": 0, "start": "2017-08-05 15:47:27.133763", "stderr": "", "stderr_lines": [], "stdout": "web2", "stdout_lines": ["web2"]}changed: [192.168.222.140] => {"ansible_job_id": "672788718586.6870", "attempts": 19, "changed": true, "cmd": "sleep 100 && hostname", "delta": "0:01:40.033291", "end": "2017-08-05 15:49:07.168699", "finished": 1, "rc": 0, "start": "2017-08-05 15:47:27.135408", "stderr": "", "stderr_lines": [], "stdout": "web2", "stdout_lines": ["web2"]}changed: [192.168.222.142] => {"ansible_job_id": "49954320951.6876", "attempts": 19, "changed": true, "cmd": "sleep 100 && hostname", "delta": "0:01:40.029308", "end": "2017-08-05 15:49:07.163930", "finished": 1, "rc": 0, "start": "2017-08-05 15:47:27.134622", "stderr": "", "stderr_lines": [], "stdout": "web2", "stdout_lines": ["web2"]}META: ran handlersMETA: ran handlersPLAY RECAP *********************************************************************************************************192.168.222.139            : ok=4    changed=2    unreachable=0    failed=0   192.168.222.140            : ok=4    changed=2    unreachable=0    failed=0   192.168.222.141            : ok=4    changed=2    unreachable=0    failed=0   192.168.222.142            : ok=4    changed=2    unreachable=0    failed=0[root@web2 asynctest]# vim asynctest.yml---- hosts: cluster  tasks:   - shell: sleep 100 && hostname     async: 100     poll: 0     register: result   - debug: var=result   - async_status: jid={
{ result.ansible_job_id }} register: job_result until: job_result.finished retries: 30第一个任务,指定shell任务为异步执行,100秒后任务超时失败。第二个任务,获取异步任务的返回值,其目的是获取jid。第三个任务,检查jid异步任务的状态,当异步任务的finished不为0时,异步任务执行成功。检查次数为30次,间隔5秒。可以看到在17次重试的时候成功了17*5=105刚好在100s内执行了,所以第105s检测的时候状态改变了所以完成任务,并且异步中poll为0不会等待单个任务完成之后再执行后面的任务指定其async最大超时时间以及轮询其状态的频率.如果你没有为 poll 指定值,那么默认的轮询频率是15秒钟。pool设置为0时,任务会立即返回,而不等待命令执行的结果,继续执行下面的任务这样就可以解决单个任务时长过长或需要执行多台的长时间任务上面是使用playbook来执行异步任务下面使用ansible来实现异步[root@web2 asynctest]# ansible cluster -B 100 -P 0 -m yum -a 'name=httpd' -vvUsing /etc/ansible/ansible.cfg as config fileMETA: ran handlers192.168.222.140 | SUCCESS => { "ansible_job_id": "217388642954.9630", "changed": true, "finished": 0, "results_file": "/root/.ansible_async/217388642954.9630", "started": 1}192.168.222.139 | SUCCESS => { "ansible_job_id": "664731498954.9636", "changed": true, "finished": 0, "results_file": "/root/.ansible_async/664731498954.9636", "started": 1}192.168.222.141 | SUCCESS => { "ansible_job_id": "786364247737.9644", "changed": true, "finished": 0, "results_file": "/root/.ansible_async/786364247737.9644", "started": 1}192.168.222.142 | SUCCESS => { "ansible_job_id": "706875309767.9649", "changed": true, "finished": 0, "results_file": "/root/.ansible_async/706875309767.9649", "started": 1}META: ran handlersMETA: ran handlers参数说明-B 100启用异步,超时时间100,-P 0 轮询时间为0使用async_status来获取异步状态信息[root@web2 asynctest]# ansible cluster -m async_status -a 'jid=706875309767.9649'192.168.222.139 | SUCCESS => { "ansible_job_id": "706875309767.9649", "changed": false, "finished": 1, "msg": "", "rc": 0, "results": [ "httpd-2.2.15-53.el6.centos.x86_64 providing httpd is already installed" ]}192.168.222.140 | SUCCESS => { "ansible_job_id": "706875309767.9649", "changed": false, "finished": 1, "msg": "", "rc": 0, "results": [ "httpd-2.2.15-53.el6.centos.x86_64 providing httpd is already installed" ]}192.168.222.141 | SUCCESS => { "ansible_job_id": "706875309767.9649", "changed": false, "finished": 1, "msg": "", "rc": 0, "results": [ "httpd-2.2.15-53.el6.centos.x86_64 providing httpd is already installed" ]}192.168.222.142 | SUCCESS => { "ansible_job_id": "706875309767.9649", "changed": false, "finished": 1, "msg": "", "rc": 0, "results": [ "httpd-2.2.15-53.el6.centos.x86_64 providing httpd is already installed" ]}异步任务的状态文件以jid命名的方式存放在远端主机的用户目录下的.ansible_async目录,本次使用的是root连接远端,所以目录是/root/.ansible_async。异步任务的返回值192.168.222.142 | SUCCESS => { "ansible_job_id": "706875309767.9649", "changed": true, "finished": 0, "results_file": "/root/.ansible_async/706875309767.9649", "started": 1}注意:在使用command, win_command, shell, win_shell, raw模块时,是不会返回信息的。

 

转载于:https://my.oschina.net/eddylinux/blog/1503295

你可能感兴趣的文章
一个想法(续二):换个角度思考如何解决IT企业招聘难的问题!
查看>>
tomcat指定配置文件路径方法
查看>>
linux下查看各硬件型号
查看>>
epoll的lt和et模式的实验
查看>>
Flux OOM实例
查看>>
07-k8s-dns
查看>>
Android 中 ListView 分页加载数据
查看>>
oracle启动报错:ORA-00845: MEMORY_TARGET not supported on this system
查看>>
Go方法
查看>>
Dapper丶DapperExtention,以及AbpDapper之间的关系,
查看>>
搞IT的同学们,你们在哪个等级__那些年发过的帖子
查看>>
且谈语音搜索
查看>>
MySQL数据库导入导出常用命令
查看>>
低版本Samba无法挂载
查看>>
Telegraf+Influxdb+Grafana构建监控平台
查看>>
使用excel 展现数据库内容
查看>>
C#方法拓展
查看>>
MySql.Data.dll的版本
查看>>
Linux系统磁盘管理
查看>>
hdu 2191 (多重背包+二进制优化)
查看>>