I'm on the latest version of ansible and python
I wrote a testcase depicting the issue I m facing.
I run the below shell-script and pass three application names each of which is an ansible role that executes task on the same node and sleeps (waits) for 10 seconds which means in all it will execute for more than (3X10) 30 seconds if the execution is not parallel
Below is my code for shell script and roles that I use to run my ansible-roles playbook:
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
Below output shows that the execution is not parallel and takes 43 seconds instead of 10+ seconds
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", "dest": "/tmp/wrapper.j2", "gid": 64332, "group": "wladmin", "md5sum": "2c151bf785a5f7b0fca4b58cf238d50e", "mode": "0744", "owner": "wladmin", "size": 100, "src": "/home/wladmin/.ansible/tmp/ansible-tmp-1623398190.63-12266-31151611835797/source", "state": "file", "uid": 600000008}
TASK [apptwo : Executing sleep task] **********************************
changed: [localhost] => {"changed": true, "cmd": ["/tmp/wrapper.j2", "&"], "delta": "0:00:10.007971", "end": "2021-06-11 02:56:42.306926", "rc": 0, "start": "2021-06-11 02:56:32.298955", "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 [appthree : Copying the Startup Wrap-up script to the Domain_Home/bin directory] ******************************
changed: [localhost] => {"changed": true, "checksum": "876833744db44de5afaabe598a31341b6fc800ac", "dest": "/tmp/wrapper.j2", "gid": 64332, "group": "wladmin", "md5sum": "651fb5088564dc481b92401e28267f8e", "mode": "0744", "owner": "wladmin", "size": 92, "src": "/home/wladmin/.ansible/tmp/ansible-tmp-1623398205.43-12442-191618850932513/source", "state": "file", "uid": 600000008}
TASK [appthree : Executing sleep task] **************************************
changed: [localhost] => {"changed": true, "cmd": ["/tmp/wrapper.j2", "&"], "delta": "0:00:10.010776", "end": "2021-06-11 02:56:57.241882", "rc": 0, "start": "2021-06-11 02:56:47.231106", "stderr": "", "stderr_lines": [], "stdout": "", "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
All the three `/tmp/par_<appname>.log` does get generated.
I tried adding `&` to the ansible-playbook command in my shell script so each app is a new ansible-playbook task that runs parallelly but that generates only one `/tmp/par_<appname>.log` and not all three. Also, the execution seems to take same time as before.
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
Output with the `&` changes:
time /web/admin/playbooks/testpar/run_me_for_restarts.sh appone,apptwo,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, "checksum": "bc42fc98344e9de310dc618c6e79de5db7a59de1", "dest": "/tmp/wrapper.j2", "gid": 64332, "group": "wladmin", "md5sum": "2c151bf785a5f7b0fca4b58cf238d50e", "mode": "0744", "owner": "wladmin", "size": 100, "src": "/home/wladmin/.ansible/tmp/ansible-tmp-1623397469.29-10776-21357236583355/source", "state": "file", "uid": 600000008}
.....
.....
I have read about ansible role parallel execution here [https://stackoverflow.com/questions/53340687/how-to-run-couple-of-roles-in-parallel-in-ansible-playbook][1] however,
Can you please suggest how can I get the three app tasks to run in parallel so i take about 10 + seconds only? Tweak, workaround will also be appreciated.