How can Ansible loop over a sequence of tasks?

随声附和 提交于 2019-12-04 07:30:14

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 }}"

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.

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"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!