Ansible 大项目管理实践笔记:并行任务、角色管理与负载均衡架构部署

管理大项目

Ansible 在大项目管理中的“并行配置与异步任务管理”方法

配置 async

async 异步任务

  • 测试 async + poll 参数对任务调度的影响

示例1: 任务执行失败,在规定时间内容任务没有执行完成。

async: 5 + poll: 2,任务超时 → 演示 异步任务超时失败的情况

 [yuxb@controller web 09:53:08]$ vim playbook.yml 
 [yuxb@controller web 09:54:09]$ cat playbook.yml 
 ---
 - name: connection
   hosts: node1
   tasks:
     - name: conneciton 
       shell: sleep 10
       async: 5
       poll: 2
 ​
 # 执行
 [yuxb@controller web 09:51:33]$ ansible-playbook playbook.yml 
 ​
 PLAY [connection] ***********************************************************************************
 ​
 TASK [Gathering Facts] ******************************************************************************
 ok: [node1]
 ​
 TASK [conneciton] ***********************************************************************************
 fatal: [node1]: FAILED! => {"changed": false, "msg": "async task did not complete within the requested time - 5s"}
 ​
 PLAY RECAP ******************************************************************************************
 node1                      : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   
 ​

示例2: 放入后台下载,立刻执行下一个任务。

async: 100 + poll: 0,任务后台运行 → 演示 放到后台执行,立即执行下一个任务

 [yuxb@controller web 09:54:12]$ vim playbook.yml 
 [yuxb@controller web 09:55:22]$ cat playbook.yml 
 ---
 - name: connection
   hosts: node1
   tasks:
     - name: download
       get_url: 
         url: http://192.168.48.100/ISOS/openEuler-24.03-LTS-x86_64-dvd.iso
         dest: /home/yuxb
       async: 100
       poll: 0
 ​
 # 执行
 [yuxb@controller web 09:54:43]$ ansible-playbook playbook.yml 
 ​
 PLAY [connection] ***********************************************************************************
 ​
 TASK [Gathering Facts] ******************************************************************************
 ok: [node1]
 ​
 TASK [download] *************************************************************************************
 changed: [node1]
 ​
 PLAY RECAP ******************************************************************************************
 node1                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
 ​
 ​

示例3: ansible 默认行为,等该任务执行完,再执行下一个任务。

async: 0 + poll: 2,相当于默认 → 演示 同步等待,按顺序执行任务

 [yuxb@controller web 09:55:24]$ vim playbook.yml 
 [yuxb@controller web 09:56:13]$ cat playbook.yml 
 ---
 - name: connection
   hosts: node1
   tasks:
     - name: conneciton 
       shell: sleep 10
       async: 0
       poll: 2
 ​
 # 执行
 [yuxb@controller web 09:55:37]$ ansible-playbook playbook.yml 
 ​
 PLAY [connection] ***********************************************************************************
 ​
 TASK [Gathering Facts] ******************************************************************************
 ok: [node1]
 ​
 TASK [conneciton] ***********************************************************************************
 changed: [node1]
 ​
 PLAY RECAP ******************************************************************************************
 node1                      : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
 ​

wait_for 模块

  • 测试 如何让 Playbook 在后台任务完成前,智能等待资源就绪

示例1: 测试文件是否存在

等待文件 /tmp/hello 被创建 → 演示 检测文件存在性

 [yuxb@controller web 09:56:16]$ vim playbook.yml 
 [yuxb@controller web 10:11:46]$ cat playbook.yml 
 ---
 - name: test wait for
   hosts: node1
   tasks:
     - shell: sleep 10 && touch /tmp/hello
       # async时间要大于sleep的时间
       async: 20
       poll: 0
       register: out
 ​
     - name: wait for create /tmp/hello
       wait_for:
         path: /tmp/hello
         state: present
         delay: 5
         timeout: 30
         sleep: 2
 ​
 # 测试
 [yuxb@controller web 09:56:39]$ ansible-playbook playbook.yml 
 ​
 PLAY [test wait for] ********************************************************************************
 ​
 TASK [Gathering Facts] ********************************************
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值