# Cisco IOS Module - Loop not working

**URL:** <https://forum.ansible.com/t/cisco-ios-module-loop-not-working/6851>\
**Category:** Get Help\
**Created:** [June 30, 2024, 5:49am UTC](https://forum.ansible.com/t/cisco-ios-module-loop-not-working/6851 "2024-06-30T05:49:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![netmart](https://avatars.discourse-cdn.com/v4/letter/n/9e8a1a/32.png) [@netmart](https://forum.ansible.com/u/netmart)\
**Post date:** [June 30, 2024, 5:49am UTC](https://forum.ansible.com/t/cisco-ios-module-loop-not-working/6851/1 "2024-06-30T05:49:34Z")

</div>

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](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_loops.html#id6)[](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_loops.html#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’ }

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [July 1, 2024, 1:30pm UTC](https://forum.ansible.com/t/cisco-ios-module-loop-not-working/6851/2 "2024-07-01T13:30:20Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![netmart](https://avatars.discourse-cdn.com/v4/letter/n/9e8a1a/32.png) [@netmart](https://forum.ansible.com/u/netmart)\
**Post date:** [July 5, 2024, 3:51am UTC](https://forum.ansible.com/t/cisco-ios-module-loop-not-working/6851/3 "2024-07-05T03:51:21Z")

</div>

Thank you Shertel for pointing out the YAML syntax issue.

After fixing it, it was working expected.

![image003.jpg](https://us1.discourse-cdn.com/ansible/original/2X/f/f464dd1c7c382c015b3fc53eebe1cd1aadffee61.jpeg)
