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
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:
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
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"}
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.
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' }
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" }
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
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide