# How to define "all hosts but first in a group"?

**URL:** <https://forum.ansible.com/t/how-to-define-all-hosts-but-first-in-a-group/16820>\
**Category:** Ansible Project\
**Created:** [November 19, 2014, 8:28am UTC](https://forum.ansible.com/t/how-to-define-all-hosts-but-first-in-a-group/16820 "2014-11-19T08:28:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![t\_goto](https://avatars.discourse-cdn.com/v4/letter/t/8491ac/32.png) [@t\_goto](https://forum.ansible.com/u/t_goto)\
**Post date:** [November 19, 2014, 8:28am UTC](https://forum.ansible.com/t/how-to-define-all-hosts-but-first-in-a-group/16820/1 "2014-11-19T08:28:26Z")

</div>

Hello, Ansible experts.  
I’m stuck with this and I need help.

I have several servers in a group in inventory file.  
`[webservers] web1 web2 web3 web4 . .`

And I want to do some tasks to all hosts except first one in webservers group.  
Unworkable pattern follows. ☹

`

- name: do something all hosts but first in a webservers group  
hosts: webservers[1:last]  
tasks:  
(snip)  
`

Since I can’t fix how many hosts webservers group has, I want to make it rather dynamic.  
Any suggestion?

---

<div class="post-metadata">

**Author:** ![Tomasz\_Kontusz](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@Tomasz\_Kontusz](https://forum.ansible.com/u/Tomasz_Kontusz)\
**Post date:** [November 19, 2014, 8:45am UTC](https://forum.ansible.com/t/how-to-define-all-hosts-but-first-in-a-group/16820/2 "2014-11-19T08:45:12Z")

</div>

Can’t check if it works, but Python’s way to say “all but first” is `some_list[1:]`. So I’d guess you want `with_items: groups.webservers[1:]`

t goto [tomoyan777@gmail.com](mailto:tomoyan777@gmail.com) napisał:

---

<div class="post-metadata">

**Author:** ![t\_goto](https://avatars.discourse-cdn.com/v4/letter/t/8491ac/32.png) [@t\_goto](https://forum.ansible.com/u/t_goto)\
**Post date:** [November 19, 2014, 9:09am UTC](https://forum.ansible.com/t/how-to-define-all-hosts-but-first-in-a-group/16820/3 "2014-11-19T09:09:55Z")

</div>

Thanks Mr.Tomasz  
I tried with following target, but it didn’t work ☹

`

- name: do something all hosts but first in a webservers group  
hosts: webservers[1:]  
tasks:  
(snip)  
`

Just “no host matched” and skipped.

---

<div class="post-metadata">

**Author:** ![Bohdan\_Sydor](https://avatars.discourse-cdn.com/v4/letter/b/f475e1/32.png) [@Bohdan\_Sydor](https://forum.ansible.com/u/Bohdan_Sydor)\
**Post date:** [November 19, 2014, 9:14am UTC](https://forum.ansible.com/t/how-to-define-all-hosts-but-first-in-a-group/16820/4 "2014-11-19T09:14:26Z")

</div>

Hello,

> I have several servers in a group in inventory file.  
> [webservers]  
> web1  
> web2  
> web3  
> web4  
> .

Please, try this way:

> - name: do something all hosts but first in a webservers group  
> &nbsp;&nbsp;hosts: webservers[1:last]  
> &nbsp;&nbsp;tasks:

hosts: webservers:!web1

---

<div class="post-metadata">

**Author:** ![James\_Martin1](https://avatars.discourse-cdn.com/v4/letter/j/0ea827/32.png) [@James\_Martin1](https://forum.ansible.com/u/James_Martin1)\
**Post date:** [November 19, 2014, 2:48pm UTC](https://forum.ansible.com/t/how-to-define-all-hosts-but-first-in-a-group/16820/5 "2014-11-19T14:48:15Z")

</div>

`

- hosts: webservers:!webservers[0]  
`

Should do the trick. This means include all the webservers except the first one.

- James
