在Ansible中,我如何在有限的时间内循环运行一个任务块?

时间:2019-03-05 12:56:38

标签: ansible

我有一组Ansible Playbook任务,我希望以最大限制(例如,如果不满足条件的话,最大3倍)循环运行。我尝试使用块,但不允许retriesuntil

如何在条件和最大迭代次数(如果不满足条件)下循环运行指令?

到目前为止,我已经尝试了以下方法。

- hosts: all
  name: runLinuxSystemUpdate
  gather_facts: false
  tasks:
    - name: Set Facts if not present in Host.
      include: checkLinuxSystemUpdateStatusTask.yml
      when: ansible_facts['Missing_Hotfix_Patches'] is not defined or ansible_facts['Missing_Hotfix_Patches'] == ""

    - name: Iteration Block
      block:

        - name: Install Patches Block
          block:
            - name: Run InstallLinuxPatchesTask.yml Task File
              include: InstallLinuxPatchesTask.yml

          when: ansible_facts['Missing_Hotfix_Patches'] != '0' or ansible_facts['Missing_Security_Patches'] != '0'  

        - name: Reboot Machines Block
          block:
            - name: Run RebootLinuxMachinesTask.yml Task File
              include: RebootLinuxMachinesTask.yml

          when: ansible_facts['Pending_Reboot'] == true

        - name: Run checkLinuxSystemUpdateStatusTask task file to re-validate Update and Reboot Status
          include: checkLinuxSystemUpdateStatusTask.yml
          #when: update_result.stdout.find("The deployment of patches and packages was successfully") == -1

      until: ansible_facts['Missing_Hotfix_Patches'] != '0' and ansible_facts['Missing_Security_Patches'] != '0'
      retries: 3

在这里,任务文件运行正常。在满足条件达到最大重试次数/迭代次数(以先到者为准)之前,我无法在- name: Iteration Block中找到一种执行指令的方法。

环境详细信息

Ansible版本:Ansible 2.7.4 AWX版本:Docker上的AWX 2.1.2.0

1 个答案:

答案 0 :(得分:0)

请检查有关Ansible中循环的文档
https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html

一种简单的循环方法是使用列表

- name: add several users
  user:
    name: "{{ item }}"
    state: present
    groups: "wheel"
  loop:
     - testuser1
     - testuser2

或使用with_items

- name: with_items
  debug:
    msg: "{{ item }}"
  with_items: "{{ items }}"