0

我正在使用最新版本的 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", "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

这三个/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,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}
.....
.....

我在这里阅读了有关 ansible 角色并行执行的内容如何在 Ansible 剧本中并行运行几个角色?然而,

您能否建议我如何让三个应用程序任务并行运行,这样我只需要大约 10 多秒?调整,解决方法也将受到赞赏。

如果此解决方案对我永远不起作用,请通过示例提出替代方法。

4

0 回答 0