如何在此 ansible 脚本中的主机之间添加 30 秒的时间延迟?

Lin*_*ter 2 sleep delay ansible

    - 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
Run Code Online (Sandbox Code Playgroud)

如何在此 ansible 脚本中的主机之间添加 30 秒的时间延迟?

lar*_*sks 5

默认情况下,Ansible并行处理多个主机。如果你真的不想这样做,你需要做的第一件事就是调整这个播放的serial参数:

- 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
Run Code Online (Sandbox Code Playgroud)

现在您一次在一台主机上运行该任务,您可以使用该pause模块引入 30 秒的延迟:

- 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
Run Code Online (Sandbox Code Playgroud)

上面的输出看起来像这样:

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
Run Code Online (Sandbox Code Playgroud)