简体   繁体   中英

How can I add 30 seconds time delay between hosts in this ansible script?

    - hosts: dr
      become: true
      become_user: root

      tasks:
        - name: yum
          shell: "hostname >> /tmp/ycu.txt; yum history | head -5 >> /tmp/ycu.txt"

        - name: Specifying a path directly
          fetch:
            src: /tmp/ycu.txt
            dest: /tmp/{{ inventory_hostname }}/
            flat: yes

How can I add a 30 seconds time delay between hosts in this ansible script?

By default, Ansible processes multiple hosts in parallel . If you really don't want to do that, the first thing you need to do is adjust the serial parameter for this play:

- hosts: dr
  become: true
  become_user: root
  serial: 1
  tasks:
  - name: yum
    shell: "hostname >> /tmp/ycu.txt; yum history | head -5 >> /tmp/ycu.txt"

  - name: Fetch remote file
    fetch:
      src: /tmp/ycu.txt
      dest: /tmp/{{ inventory_hostname }}/
      flat: yes

Now that you're running the task on one host at a time, you can introduce a 30 second delay using the pause module :

- hosts: dr
  become: true
  become_user: root
  serial: 1
  tasks:
  - name: yum
    shell: "hostname >> /tmp/ycu.txt; yum history | head -5 >> /tmp/ycu.txt"

  - name: Fetch remote file
    fetch:
      src: /tmp/ycu.txt
      dest: /tmp/{{ inventory_hostname }}/
      flat: yes

  - name: pause for 30 seconds
    pause:
      seconds: 30

The output of the above would look something like:

PLAY [all] **********************************************************************************************

TASK [Gathering Facts] **********************************************************************************
ok: [node1]

TASK [yum] **********************************************************************************************
changed: [node1]

TASK [Fetch remote file] ********************************************************************************
changed: [node1]

TASK [pause for 30 seconds] *****************************************************************************
Pausing for 30 seconds
(ctrl+C then 'C' = continue early, ctrl+C then 'A' = abort)
ok: [node1]

PLAY [all] **********************************************************************************************

TASK [Gathering Facts] **********************************************************************************
ok: [node2]

TASK [yum] **********************************************************************************************
changed: [node2]

TASK [Fetch remote file] ********************************************************************************
changed: [node2]

TASK [pause for 30 seconds] *****************************************************************************
Pausing for 30 seconds
(ctrl+C then 'C' = continue early, ctrl+C then 'A' = abort)
ok: [node2]

PLAY RECAP **********************************************************************************************
node1                      : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
node2                      : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

You can solve your issue in one task by adding a sleep to the command and use throttle :

- name: yum
  shell: "hostname >> /tmp/ycu.txt; yum history | head -5 >> /tmp/ycu.txt ; sleep 30"
  throttle: 1

This will also decrease the total time of the playbook run, since not all tasks will be executed host by host, but only that task...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM