How can Ansible loop over a sequence of tasks?

2020-07-11 07:40发布

问题:

How can an Ansible playbook loop over a sequence of tasks? I wish to implement a polling loop that executes a task sequence until the task is successful. When it fails, an exception handler will attempt to fix the condition and then the loop will repeat the task sequence.

Consider the following imaginary example:

- action:
    - block:
        - debug: msg='i execute normally'
        - command: /bin/foo
      rescue:
        - debug: msg='I caught an error'
        - command: /bin/fixfoo
      always:
        - debug: msg="this always executes"
  register: result
  until: result
  retries: 5
  delay: 10

回答1:

As of Ansible 2.5, loop is recommended over with_items. Furthermore, since you don't want to assume your sub-task won't have any loops, you can use a more descriptive name than "item". Here's an example which uses a loop within a loop, slightly truncated but still working if you define the appropriate config:

# terminate-instances-main.yml:
---
- hosts: local
  connection: local
  vars:
    regions:
      - ap-southeast-1
      - us-west-1
  tasks:
    - include_tasks: "terminate-instance-tasks.yml"
      loop: "{{ regions }}"
      loop_control:
        loop_var: region

# terminate-instance-tasks.yml:
---
- name: Gather EC2 facts
  ec2_instance_facts:
    region: "{{ region }}"
    filters:
      "tag:temporary": "true"
    aws_access_key: "{{ aws_access_key }}"
    aws_secret_key: "{{ aws_secret_key }}"
  register: ec2

- name: Terminate Temp EC2 Instance(s)
  ec2:
    instance_ids: '{{ item.instance_id }}'
    state: absent
    region: "{{ region }}"
    aws_access_key: "{{ aws_access_key }}"
    aws_secret_key: "{{ aws_secret_key }}"
  loop: "{{ ec2.instances }}"


回答2:

In Ansible 1.x this simply can't be done. It's just not designed that way.

Ansible 2.0 supports looping over include files, so you could put all your tasks in one file then do something like this:

- include: test.yml
  with_items:
    - 1
    - 2
    - 3

However I don't believe any of the other constructs you mention (register, until, retries, delay, etc) will work with this. While some of those could theoretically be applied to all tasks in an include file others like register and until are explicitly bound to individual tasks. It makes no sense to have multiple tasks try to register the same output variable.



回答3:

I needed something similar based on the JSON response from a URL. Here is my attempt: https://gist.github.com/ParagDoke/5ddfc3d5647ce9b0110d1b9790090092

Idea is to include another task list yaml file recursively. If the includes file name is foobar.yml:

- task1
- task2
- task3
- include_tasks: foobar.yml
  until: "some condition"