Cisco IOS Module - Loop not working

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’ }

It doesn’t look like the loop items are valid YAML. You need a space after each ‘:’ and ‘,’ in the dictionary. Tip: use ``` before and after a block of YAML/code to make it easier to read.

Try this:

  - 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" }

Thank you Shertel for pointing out the YAML syntax issue.

After fixing it, it was working expected.

image003.jpg

1 Like