管理大项目
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] ********************************************

最低0.47元/天 解锁文章
3769

被折叠的 条评论
为什么被折叠?



