繁体   English   中英

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

[英]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

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

默认情况下,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

现在您一次在一台主机上运行任务,您可以使用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

上面的 output 看起来像这样:

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

您可以通过向命令添加 sleep 并使用throttle来解决一项任务中的问题:

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

这也将减少剧本运行的总时间,因为并非所有任务都会逐个主机执行,但只有那个任务......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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