# Execute command with items from inventory group

**URL:** <https://forum.ansible.com/t/execute-command-with-items-from-inventory-group/13925>\
**Category:** Ansible Project\
**Created:** [November 18, 2013, 12:03pm UTC](https://forum.ansible.com/t/execute-command-with-items-from-inventory-group/13925 "2013-11-18T12:03:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Bilbie](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Alex\_Bilbie](https://forum.ansible.com/u/Alex_Bilbie)\
**Post date:** [November 18, 2013, 12:03pm UTC](https://forum.ansible.com/t/execute-command-with-items-from-inventory-group/13925/1 "2013-11-18T12:03:32Z")

</div>

Hi everyone,

I’m looking to execute a command for each items in an inventory group - in this case for each item in a “staging-webservers” group, I want to allow access to it in the firewall.

This doesn’t seem to work but here is kind of what I want to do:

```auto

- name: Update firewall rules
command: sudo ufw allow proto tcp from $item to port 9200
with_items:
- [staging-webservers]

```

In addition if I wanted to write a list of hosts from a group into a template how can I do that?

Thanks in advance,

Alex

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [November 18, 2013, 4:01pm UTC](https://forum.ansible.com/t/execute-command-with-items-from-inventory-group/13925/2 "2013-11-18T16:01:01Z")

</div>

I think you want:

with\_items: groups[‘staging-webservers’]

same var is available for templates to use:

{% for host in groups[‘staging-webservers’]%}  
{{host}}  
{%endfor%}

---

<div class="post-metadata">

**Author:** ![Alex\_Bilbie](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Alex\_Bilbie](https://forum.ansible.com/u/Alex_Bilbie)\
**Post date:** [November 18, 2013, 7:32pm UTC](https://forum.ansible.com/t/execute-command-with-items-from-inventory-group/13925/3 "2013-11-18T19:32:58Z")

</div>

Thank you.

This worked for me:

- name: Allow webservers access  
action: command /usr/sbin/ufw allow proto tcp from $item to any port 7474  
with\_items:
- ${groups.staging\_webservers}

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 20, 2013, 3:49am UTC](https://forum.ansible.com/t/execute-command-with-items-from-inventory-group/13925/4 "2013-11-20T03:49:15Z")

</div>

Also the above syntax is overkill 🙂

with\_items: groups.staging\_webservers

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [November 20, 2013, 3:49am UTC](https://forum.ansible.com/t/execute-command-with-items-from-inventory-group/13925/5 "2013-11-20T03:49:40Z")

</div>

Legacy variables are also bad!

action: command /usr/sbin/ufw allow proto tcp from $item to any port 7474

Should be

{{ item }}
