title: How to iterate over inventory group in ansible?
body: <p>I have looked at some of the SO post that are similar to my question: <a href="https://stackoverflow.com/questions/53683136/ansible-iterate-over-inventory-groups">Ansible : iterate over inventory groups</a>, <a href="https://stackoverflow.com/questions/47698210/ansible-iterate-over-hosts-in-inventory-group-set-by-variable">Ansible iterate over hosts in inventory group set by variable</a> and others but I still don't understand what I am doing wrong:</p>
  
  <p>Here is my inventory file:</p>
  
  <pre><code>[root@82c420275711 playbooks]# cat inventory.dev
  #
  #   - Comments begin with the '#' character
  #   - Blank lines are ignored
  #   - Groups of hosts are delimited by [header] elements
  #   - You can enter hostnames or ip addresses
  #   - A hostname/ip can be a member of multiple groups
  [localhost]
  127.0.0.1
  
  [virtual_centers]
  172.17.0.2
  172.17.0.5
  </code></pre>
  
  <p>Here is the part of my playbook that is having trouble:</p>
  
  <pre><code>- name: "allow {{ item }} to allow port 514"
    firewalld:
      immediate: yes
      rich_rule: "rule family=\"ipv4\" source address=\"{{ item }}\" port protocol=\"udp\" port=\"514\" accept"
      permanent: yes
      state: enabled
    with_items: groups['virtual_centers']
  </code></pre>
  
  <p>Here is the error message I get when I run the playbook with that inventory file:</p>
  
  <pre><code>[root@82c420275711 playbooks]# ansible-playbook -i inventory.dev ./configure_syslog-ng_server.yaml
  ...
      TASK [allow {{ item }} to allow port 514] *******************************************************************************************************************************************************************************************
  failed: [127.0.0.1] (item=groups['virtual_centers']) =&gt; {"changed": false, "item": "groups['virtual_centers']", "msg": "ERROR: Exception caught: INVALID_ADDR: groups['virtual_centers']"}
  </code></pre>
  
  <p>What am I doing wrong here? thanks</p>