# Loop through a group of hosts with pattern

**URL:** <https://forum.ansible.com/t/loop-through-a-group-of-hosts-with-pattern/21501>\
**Category:** Ansible Project\
**Created:** [May 20, 2016, 2:29am UTC](https://forum.ansible.com/t/loop-through-a-group-of-hosts-with-pattern/21501 "2016-05-20T02:29:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzarzoso](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jzarzoso](https://forum.ansible.com/u/jzarzoso)\
**Post date:** [May 20, 2016, 2:29am UTC](https://forum.ansible.com/t/loop-through-a-group-of-hosts-with-pattern/21501/1 "2016-05-20T02:29:50Z")

</div>

Hi everyone!

I’m thinking of looping through all the hosts from a playbook with a pattern. I have this play:

`

- hosts: localhost # Select all hosts  
tasks:
- name: Debug the list  
debug:  
msg: “host: {{ item }}”  
with\_items: “{{groups.all}}”

`

I wanted something like this:

`

- hosts: localhost # Select all hosts EXCEPT localhost  
tasks:
- name: Debug the list  
debug:  
msg: “host: {{ item }}”  
with\_items: “{{ groups.all:!localhost }}”

`

I tried but I got an error:

`

fatal: [localhost]: FAILED! =\> {“failed”: true, “msg”: “template error while templating string: expected token ‘end of print statement’, got ‘:’. String: {{groups.all:!localhost}}”}

`

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [May 20, 2016, 2:48am UTC](https://forum.ansible.com/t/loop-through-a-group-of-hosts-with-pattern/21501/2 "2016-05-20T02:48:36Z")

</div>

Check out with\_inventory\_hostnames

[http://docs.ansible.com/ansible/playbooks\_loops.html#looping-over-the-inventory](http://docs.ansible.com/ansible/playbooks_loops.html#looping-over-the-inventory)

---

<div class="post-metadata">

**Author:** ![jzarzoso](https://avatars.discourse-cdn.com/v4/letter/j/b19c9b/32.png) [@jzarzoso](https://forum.ansible.com/u/jzarzoso)\
**Post date:** [May 20, 2016, 2:56am UTC](https://forum.ansible.com/t/loop-through-a-group-of-hosts-with-pattern/21501/3 "2016-05-20T02:56:42Z")

</div>

SWEET! Thanks mate!
