问题描述
我使用的是最新版本的 ansible 和 python
我写了一个测试用例来描述我面临的问题。
我运行以下 shell 脚本并传递三个应用程序名称,每个名称都是在同一节点上执行任务并休眠(等待)10 秒的 ansible 角色,这意味着它总共将执行超过(3X10)如果执行不是并行的,则为 30 秒
以下是我用来运行 ansible-roles 剧本的 shell 脚本和角色的代码:
cat /web/admin/playbooks/testpar/run_me_for_restarts.sh
SH_APP=$1
echo "OK... Let us execute the Ansible Playbook Now..."
for i in $(echo $SH_APP | sed "s/,/ /g")
do
ansible-playbook -v -i /web/admin/playbooks/testpar/va.hosts /web/admin/playbooks/testpar/va_action.yml -e APPNAME=$i
done
$ cat /web/admin/playbooks/testpar/va_action.yml
---
- hosts: localhost
user: wladmin
strategy: free
vars:
ansible_ssh_extra_args: -o StrictHostKeyChecking=no -o ServerAliveInterval=50
roles:
- { role: "{{APPNAME}}" }
cat /web/admin/playbooks/testpar/roles/<appname>/tasks/main.yml
---
- name: copying the Startup Wrap-up script to the Domain_Home/bin directory
template:
src: "{{role_path}}/templates/wrapper.j2"
dest: "/tmp"
mode: 0744
- name: Executing sleep task
command: "/tmp/wrapper.j2 &"
ignore_errors: yes
cat /web/admin/playbooks/testpar/roles/<appname>/templates/wrapper.j2
#!/bin/sh
echo "started ...." >/tmp/par_{{ APPNAME }}.log
sleep 10
echo "done ...." >/tmp/par_{{ APPNAME }}.log
下面的输出显示执行不是并行的,需要 43 秒而不是 10+ 秒
time /web/admin/playbooks/testpar/run_me_for_restarts.sh appone,apptwo,appthree
OK... Let us execute the Ansible Playbook Now...
Using /etc/ansible/ansible.cfg as config file
PLAY [localhost] **********************************************************************************************
TASK [Gathering Facts] ****************************************************************************************
ok: [localhost]
TASK [appone : copying the Startup Wrap-up script to the Domain_Home/bin directory] **************************
changed: [localhost] => {"changed": true,"checksum": "543c342a48380d78929cecacfb0319c5575d6d5d","dest": "/tmp/wrapper.j2","gid": 64332,"group": "wladmin","md5sum": "36126e45f90db4ebf16260582cca00c4","mode": "0744","owner": "wladmin","size": 100,"src": "/home/wladmin/.ansible/tmp/ansible-tmp-1623398176.73-12084-156235215973593/source","state": "file","uid": 600000008}
TASK [appone : Executing sleep task] **********************************
changed: [localhost] => {"changed": true,"cmd": ["/tmp/wrapper.j2","&"],"delta": "0:00:10.008392","end": "2021-06-11 02:56:28.387822","rc": 0,"start": "2021-06-11 02:56:18.379430","stderr": "","stderr_lines": [],"stdout": "","stdout_lines": []}
PLAY RECAP ****************************************************************************************************
localhost : ok=3 changed=2 unreachable=0 Failed=0 skipped=0 rescued=0 ignored=0
Using /etc/ansible/ansible.cfg as config file
PLAY [localhost] **********************************************************************************************
TASK [Gathering Facts] ****************************************************************************************
ok: [localhost]
TASK [apptwo : copying the Startup Wrap-up script to the Domain_Home/bin directory] **************************
changed: [localhost] => {"changed": true,"checksum": "bc42fc98344e9de310dc618c6e79de5db7a59de1","md5sum": "2c151bf785a5f7b0fca4b58cf238d50e","src": "/home/wladmin/.ansible/tmp/ansible-tmp-1623398190.63-12266-31151611835797/source","uid": 600000008}
TASK [apptwo : Executing sleep task] **********************************
changed: [localhost] => {"changed": true,"delta": "0:00:10.007971","end": "2021-06-11 02:56:42.306926","start": "2021-06-11 02:56:32.298955","stdout_lines": []}
PLAY RECAP ****************************************************************************************************
localhost : ok=3 changed=2 unreachable=0 Failed=0 skipped=0 rescued=0 ignored=0
Using /etc/ansible/ansible.cfg as config file
PLAY [localhost] **********************************************************************************************
TASK [Gathering Facts] ****************************************************************************************
ok: [localhost]
TASK [appthree : copying the Startup Wrap-up script to the Domain_Home/bin directory] ******************************
changed: [localhost] => {"changed": true,"checksum": "876833744db44de5afaabe598a31341b6fc800ac","md5sum": "651fb5088564dc481b92401e28267f8e","size": 92,"src": "/home/wladmin/.ansible/tmp/ansible-tmp-1623398205.43-12442-191618850932513/source","uid": 600000008}
TASK [appthree : Executing sleep task] **************************************
changed: [localhost] => {"changed": true,"delta": "0:00:10.010776","end": "2021-06-11 02:56:57.241882","start": "2021-06-11 02:56:47.231106","stdout_lines": []}
PLAY RECAP ****************************************************************************************************
localhost : ok=3 changed=2 unreachable=0 Failed=0 skipped=0 rescued=0 ignored=0
real 0m43.077s
user 0m13.285s
sys 0m3.957s
所有三个 /tmp/par_<appname>.log
都会生成。
我尝试将 &
添加到我的 shell 脚本中的 ansible-playbook 命令,这样每个应用程序都是一个新的 ansible-playbook 任务,它并行运行但只生成一个 /tmp/par_<appname>.log
而不是所有三个。此外,执行时间似乎与以前相同。
cat /web/admin/playbooks/testpar/run_me_for_restarts.sh
SH_APP=$1
echo "OK... Let us execute the Ansible Playbook Now..."
for i in $(echo $SH_APP | sed "s/,/ /g")
do
ansible-playbook -v -i /web/admin/playbooks/testpar/va.hosts /web/admin/playbooks/testpar/va_action.yml -e APPNAME=$i **&**
done
带有 &
更改的输出:
time /web/admin/playbooks/testpar/run_me_for_restarts.sh appone,appthree
OK... Let us execute the Ansible Playbook Now...
real 0m0.010s
user 0m0.003s
sys 0m0.008s
[wladmin@localhost testpar]$ Using /etc/ansible/ansible.cfg as config file
Using /etc/ansible/ansible.cfg as config file
Using /etc/ansible/ansible.cfg as config file
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
PLAY [localhost] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [localhost]
TASK [apptwo : copying the Startup Wrap-up script to the Domain_Home/bin directory] ***
ok: [localhost]
TASK [appone : copying the Startup Wrap-up script to the Domain_Home/bin directory] ***
ok: [localhost]
TASK [appthree : copying the Startup Wrap-up script to the Domain_Home/bin directory] ***
changed: [localhost] => {"changed": true,"src": "/home/wladmin/.ansible/tmp/ansible-tmp-1623397469.29-10776-21357236583355/source","uid": 600000008}
.....
.....
我已经在这里How to run couple of roles in parallel in Ansible playbook? 阅读了有关 ansible 角色并行执行的内容,
你能建议我如何让三个应用程序任务并行运行,这样我只需要大约 10 秒以上的时间吗?调整,解决方法也将不胜感激。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)