how can i loop through multiple lists of dictionaries using ‘loop’ syntax?

You are looping over one list with two items here and the items are your initial lists. What you need to do is to join those two lists with {{ allow_list + deny_list }}. Check out this post.

Your code fixed:

---
- hosts: localhost
  vars:
     allow_list:
      - { name: user1, uid: 1001 }
      - { name: user2, uid: 1002 }
      - { name: user3, uid: 1003 }
      - { name: user4, uid: 1004 }
      
     deny_list: 
      - { name: user11, uid: 1011 }
      - { name: user12, uid: 1012 }
      - { name: user13, uid: 1013 }
      - { name: user14, uid: 1014 }

  tasks:
     - name: debug all users
       debug:
         msg: "{{ item.name }} {{ item.uid }}"
       loop: "{{ allow_list + deny_list }}"

If you wanted to handle it in a set_fact block:

---
- hosts: localhost
  vars:
     allow_list:
      - { name: user1, uid: 1001 }
      - { name: user2, uid: 1002 }
      - { name: user3, uid: 1003 }
      - { name: user4, uid: 1004 }
      
     deny_list: 
      - { name: user11, uid: 1011 }
      - { name: user12, uid: 1012 }
      - { name: user13, uid: 1013 }
      - { name: user14, uid: 1014 }

     some_var: 42

  tasks:
    - name: set fact on condition
      set_fact:
        userlist: "{{ allow_list }}"
      when: some_var <= 5

    - name: set fact on negated condition
      set_fact:
        userlist: "{{ allow_list + deny_list }}"
      when: some_var > 5

    - name: debug all users
      debug:
        msg: "{{ item.name }} {{ item.uid }}"
      loop: "{{ userlist }}"

You need to make sure that exactly one of your set_fact blocks runs every time, otherwise you will end up with errors or unexpected results.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top