Sumonta Naskar
Sumonta Naskar

Reputation: 131

Please explain usage of "item" in Ansible

I found some AWS Ansible code using word "{{ item.id }}" or {{ item.sg_name }}.

I do not understand how "item" command works.

Upvotes: 12

Views: 17541

Answers (1)

techraf
techraf

Reputation: 68589

item is not a command, but a variable automatically created and populated by Ansible in tasks which use loops.

In the following example:

- debug:
    msg: "{{ item }}"
  with_items:
    - first
    - second

the task will be run twice: first time with the variable item set to first, the second time with second.

Further, if elements of the loop were dictionaries, you could refer to their keys using the dot notation as in your example:

- debug:
    msg: "{{ item.my_value }}"
  with_items:
    - ny_element: first
      my_value: 1
    - my_element: second
      my_value: 2

Upvotes: 19

Related Questions