# Ansible-playbook does not working with dictionary and with\_items loop!

**URL:** <https://forum.ansible.com/t/ansible-playbook-does-not-working-with-dictionary-and-with-items-loop/44511>\
**Category:** Get Help\
**Tags:** ansible\
**Created:** [September 17, 2025, 10:22pm UTC](https://forum.ansible.com/t/ansible-playbook-does-not-working-with-dictionary-and-with-items-loop/44511 "2025-09-17T22:22:43Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [September 18, 2025, 1:36am UTC](https://forum.ansible.com/t/ansible-playbook-does-not-working-with-dictionary-and-with-items-loop/44511/2 "2025-09-18T01:36:42Z")

</div>

It’s a bit of a stretch to say “it works” about the above tasks. First of all,

```yaml
with_list: global_groups

```

Doesn’t do anything useful. `with_list:` expects to be given a list. You’ve given it a string which, by irrelevant coincidence, happens to be the name of a dict.

If you want to iterate over the items in that dict, I recommend using

```yaml
loop: '{{ global_groups | dict2items }}'

```

That will loop over your `global_groups` dict, making `item` look like this on respective iterations:

```yaml
(item={'key': 'production_ansible', 'value': {'gid': 10001}})
(item={'key': 'staging_ansible', 'value': {'gid': 20001}})
(item={'key': 'grafana', 'value': {'gid': 10501}})
(item={'key': 'prometheus', 'value': {'gid': 10502}})
(item={'key': 'zookeeper', 'value': {'gid': 10503}})
(item={'key': 'rundeck', 'value': {'gid': 10504}})
(item={'key': 'nginx', 'value': {'gid': 10505}})
(item={'key': 'gnupgserver', 'value': {'gid': 10506}})
(item={'key': 'sequoia', 'value': {'gid': 10507}})

```

To get your task to work, you could try something like this:

```yaml
    - name: User management set global groups macOS
      tags:
        - user_mgmt
        - admin_accounts
      ansible.builtin.group:
        name: "{{ item.key }}"
        gid: "{{ item.value.gid | mandatory }}"
        state: "{{ item.value.state | default('present') }}"
      loop: '{{ global_groups | dict2items }}'
      when:
        - (item.value.state | default('present')) == 'present'

```

Working your `custom_groups` and `skip_groups` back in is left as an exercise for the reader. 🙂  
Good luck!

---

_[View the full topic](https://forum.ansible.com/t/ansible-playbook-does-not-working-with-dictionary-and-with-items-loop/44511)._
