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

50 views
Skip to first unread message

t goto

unread,
Nov 19, 2014, 3:28:26 AM11/19/14
to ansible...@googlegroups.com
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?

Tomasz Kontusz

unread,
Nov 19, 2014, 3:45:22 AM11/19/14
to ansible...@googlegroups.com
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 <tomoy...@gmail.com> napisał:

--
Wysłane za pomocą K-9 Mail.

t goto

unread,
Nov 19, 2014, 4:09:55 AM11/19/14
to ansible...@googlegroups.com
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.

Bohdan Sydor

unread,
Nov 19, 2014, 4:14:35 AM11/19/14
to ansible...@googlegroups.com
On Wed, Nov 19, 2014 at 9:28 AM, t goto <tomoy...@gmail.com> wrote:


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
> hosts: webservers[1:last]
> tasks:

hosts: webservers:!web1

--
Regards,
Bohdan

James Martin

unread,
Nov 19, 2014, 9:48:15 AM11/19/14
to ansible...@googlegroups.com
- hosts: webservers:!webservers[0]

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

- James
Reply all
Reply to author
Forward
0 new messages