Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 293
LinServFaulter Avatar asked Oct 23 '25 14:10

LinServFaulter


2 Answers

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
like image 96
larsks Avatar answered Oct 26 '25 07:10

larsks


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...

like image 28
Kevin C Avatar answered Oct 26 '25 06:10

Kevin C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!