Ansible Task Looping Directives: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
=External=
=External=
* https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html
* https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html
* https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#loops-and-conditionals
=Internal=
=Internal=
* [[Ansible Concepts#Looping_Directives|Ansible Concepts]]
* [[Ansible Concepts#Looping_Directives|Ansible Concepts]]
=Overview=
=Overview=
=Playground=
{{External|https://github.com/ovidiuf/playground/tree/master/ansible/modules/loops}}
=Loops=
=Loops=
==<tt>with_items</tt>==
==<tt>loop</tt>==
An iterator whose elements can be accessed with <code>item.<element></code>. The content to iterate over may came from in-line declaration or from a variable.
Takes a list for the task to iterate over, saving each list element into the <code>item</code> variable (configurable via <code>[[#loop_control|loop_control]]</code>)
 
===In-Line Declaration===
In-line declaration:
<syntaxhighlight lang='yaml'>
<syntaxhighlight lang='yaml'>
- name: debug
- name: debug
   debug:
   debug:
     msg: "{{ item }}"
     msg: "{{ item }}"
   with_items:
   loop:
     - A
     - A
     - B
     - B
     - C
     - C
</syntaxhighlight>
</syntaxhighlight>
Variable:
===Variable===
<syntaxhighlight lang='yaml'>
- name: Find some files
  find:
    paths: '/some/dir'
    patterns: '*.txt'
  register: find_result
- name: debug
  debug:
    msg: "{{ item }}"
  with_items: "{{ find_result.files | json_query('[*].path') }}"
</syntaxhighlight>
 
==<tt>loop</tt>==
Takes a list for the task to iterate over, saving each list element into the <code>item</code> variable (configurable via <code>[[#loop_control|loop_control]]</code>)
<syntaxhighlight lang='yaml'>
<syntaxhighlight lang='yaml'>
jdks:
jdks:
Line 47: Line 37:
An existing variable's field that reference a list can be used:
An existing variable's field that reference a list can be used:
<syntaxhighlight lang='yaml'>
<syntaxhighlight lang='yaml'>
- name: Debug
- name: find matching directories
  find:
    paths: /Users/ovidiu/tmp/x
    patterns: '*'
    use_regex: no
    recurse: no
    file_type: directory
  register: find_result
- name: debug
  debug:
    msg: "{{ item }}"
  loop: "{{ find_result.files | json_query('[*].path') }}"
</syntaxhighlight>
 
==<tt>with_items</tt>==
An iterator whose elements can be accessed with <code>item.<element></code>. The content to iterate over may came from in-line declaration or from a variable.
===In-line Declaration===
<syntaxhighlight lang='yaml'>
- name: debug
   debug:
   debug:
     var: item.path
     msg: "{{ item }}"
     loop: "{{ some_task_result.files }}"
  with_items:
    - A
    - B
    - C 
</syntaxhighlight>
===Variable===
<syntaxhighlight lang='yaml'>
- name: Find some files
  find:
    paths: '/some/dir'
    patterns: '*.txt'
  register: find_result
- name: debug
  debug:
     msg: "{{ item }}"
  with_items: "{{ find_result.files | json_query('[*].path') }}"
</syntaxhighlight>
</syntaxhighlight>



Latest revision as of 00:27, 17 December 2021

External

Internal

Overview

Playground

https://github.com/ovidiuf/playground/tree/master/ansible/modules/loops

Loops

loop

Takes a list for the task to iterate over, saving each list element into the item variable (configurable via loop_control)

In-Line Declaration

- name: debug
  debug:
    msg: "{{ item }}"
  loop:
    - A
    - B
    - C

Variable

jdks:
  - corretto8
  - corretto11
...
- name: Multiple items in a loop
  homebrew_cask:
    name: "{{ item }}"
    state: present
  loop: "{{ jdks }}"

An existing variable's field that reference a list can be used:

- name: find matching directories
  find:
    paths: /Users/ovidiu/tmp/x
    patterns: '*'
    use_regex: no
    recurse: no
    file_type: directory
  register: find_result
- name: debug
  debug:
    msg: "{{ item }}"
  loop: "{{ find_result.files | json_query('[*].path') }}"

with_items

An iterator whose elements can be accessed with item.<element>. The content to iterate over may came from in-line declaration or from a variable.

In-line Declaration

- name: debug
  debug:
    msg: "{{ item }}"
  with_items:
    - A
    - B
    - C

Variable

- name: Find some files
  find:
    paths: '/some/dir'
    patterns: '*.txt'
  register: find_result
- name: debug
  debug:
    msg: "{{ item }}"
  with_items: "{{ find_result.files | json_query('[*].path') }}"

loop_control

with_<lookup_plugin>

The same as loop but adds the output of any lookup plugin to generate the item list.