Hello,
Below you will an excerpt of my Ansible playbook. The goal is to cycle through a list of prefix list entries, which will be deleted on the switch.
I appreciate any help/comment.
Thanks.
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’ }