Ansible using Loop with registered variables

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2024 04:22 PM
Hello,
I was wondering, whether it is possible instead of creating three separate tasks, one processing one element of an array, to actually loop through an array.
Example:
vars:
ansible_become_pass: "{{ vault_sudo_password }}"
ansible_python_interpreter: /usr/bin/python3
prefix:
- 10.0.1.0/25
- 10.0.3.0/25
- 10.0.5.0/25
- name: Task1- remove prefix
ios_config:
commands:
- no ip prefix-list MTDC-nets permit {{prefix[0]}}
when: output2.stdout is search ~prefix[0]
become: true
register: output1
- name: Task2- remove prefix
ios_config:
commands:
- no ip prefix-list cisco-nets permit {{prefix[1]}}
when: output2.stdout is search ~prefix[1]
become: true
register: output2
- name: Task3- remove prefix
ios_config:
commands:
- no ip prefix-list MTDC-nets permit {{prefix[2]}}
when: output2.stdout is search ~prefix[2]
become: true
register: output3
Is it possible to create only one task and looping through the elements of an array:
vars:
ansible_become_pass: "{{ vault_sudo_password }}"
ansible_python_interpreter: /usr/bin/python3
loop:
- 10.0.1.0/25
- 10.0.3.0/25
- 10.0.5.0/25
register: prefix
- name: Task1- remove prefix
ios_config:
commands:
- no ip prefix-list Cisco-nets permit {{prefix}}
when: output2.stdout is search ~prefix
become: true
register: output1
- Labels:
-
Code Exchange Platform

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2024 02:54 PM
Sorry Torbjoern, this has been already tried without success.
Is there no example available illustrating looping through an array as in my case.
The material I"ve found so far in Ansible is not really matching what I am looking for.
The closest I was able to find has been:
Iterating over a list of hashes
If you have a list of hashes, you can reference subkeys in a loop. For example:
- name: Add several users ansible.builtin.user: name: "{{ item.name }}" state: present groups: "{{ item.groups }}" loop: - { name: 'testuser1', groups: 'wheel' } - { name: 'testuser2', groups: 'root' }
https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_loops.html

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2024 10:54 PM
Hi Torbjorn,
I am sorry, it is still failing.
I also tried the following:
- name: Task3- remove prefix
ios_config:
commands:
- "no ip prefix-list {{ item.network }} permit {{ item.prefix }}"
loop:
- network:"Test-nets",prefix:"10.0.0.0/25"
- network:"Test-nets",prefix:"10.0.2.0/25"
- network:"Test-nets",prefix:"10.0.5.0/25"
However, Ansible is still complaining that has no attribute network and prefix. And following the Ansible documentation, the loop is configured underneath the task.
fatal: [switch: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.parsing.yaml.objects.AnsibleUnicode object' has no attribute 'network'. 'ansible.parsing.yaml.objects.AnsibleUnicode object' has no attribute 'network'\n\nThe error appears to be in '/home/cisco/Ansible/library/lib-ios-xe-staging/site-ios-xe-CE-bgp-prefix-list-update-loop2.yml': line 34, column 8, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Task3- remove prefix\n ^ here\n"}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2024 11:28 PM - edited 06-28-2024 11:34 PM
Sorry, still the same.
I also tried the following:
Extract Playbook:
- name: Task3- remove prefix
ios_config:
commands:
- "no ip prefix-list {{ item.network }} permit {{ item.prefix }}"
loop:
- { network:"Test-nets",prefix:"10.0.0.0/25" }
- { network:"Test-nets",prefix:"10.0.2.0/25" }
- { network:"Test-nets",prefix:"10.0.5.0/25" }
fatal: [switch: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.parsing.yaml.objects.AnsibleUnicode object' has no attribute 'network'. 'ansible.parsing.yaml.objects.AnsibleUnicode object' has no attribute 'network'\n\nThe error appears to be in '/home/cisco/Ansible/library/lib-ios-xe-staging/site-ios-xe-CE-bgp-prefix-list-update-loop2.yml': line 34, column 8, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Task3- remove prefix\n ^ here\n"}
The previous setup follows the example as illustrated in the Ansible documentation. The only difference that I am using the ios_config command to iterate a command with a loop variable.
Iterating over a list of hashes
If you have a list of hashes, you can reference subkeys in a loop. For example:
- name: Add several users ansible.builtin.user: name: "{{ item.name }}" state: present groups: "{{ item.groups }}" loop: - { name: 'testuser1', groups: 'wheel' } - { name: 'testuser2', groups: 'root' }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 10:55 PM - edited 07-03-2024 10:56 PM
After reviewing it together with someone from Ansible Community, it turned out that the syntax for loop was not correct:
A space is needed after each ‘:’ and ‘,’ in the dictionary. Tip: use ``` before and after a block of YAML/code to make it easier to read.
It is working as followed:
- ios_config:
commands:
- "no ip prefix-list {{ item.network }} permit {{ item.prefix }}"
loop:
- { network: Test-nets, prefix: "10.0.0.0/25" }
- { network: Test-nets, prefix: "10.0.2.0/25" }
- { network: Test-nets, prefix: "10.0.5.0/25" }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 10:57 PM
The same is illustrated in Ansible documetation:
Iterating over a list of hashes
If you have a list of hashes, you can reference subkeys in a loop. For example:
- name: Add several users
ansible.builtin.user:
name: "{{ item.name }}"
state: present
groups: "{{ item.groups }}"
loop:
- { name: 'testuser1', groups: 'wheel' }
- { name: 'testuser2', groups: 'root' }
https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_loops.html

- « Previous
-
- 1
- 2
- Next »