Complex host patterns

65 views
Skip to first unread message

Gonzalo Servat

unread,
Oct 23, 2018, 2:24:59 AM10/23/18
to ansible...@googlegroups.com
Hi All

When working with host patterns, is it possible to craft one that says "must be in group X *and* must be in either group Y, W and Z"?

e.g. host must be in group "webserver" but it *must* also be in either group dc1, dc2 or dc3.

I can see how to check that webserver is part of dc1 (webserver,&dc1) but not sure how to build it with multiple "OR" on the "AND" (something like "webserver,&(dc1,dc2,dc3)" is what I'm looking for but it doesn't work)

Cheers
Gonz

Vladimir Botka

unread,
Oct 23, 2018, 4:41:35 AM10/23/18
to Ansible Project
There is an example in the doc (you posted the link).

    webservers:dbservers:&staging
    The above configuration means “all machines in the groups ‘webservers’ and ‘dbservers’ are to be managed if they are in the group ‘staging’ also, ...

Gonzalo Servat

unread,
Oct 23, 2018, 6:31:03 AM10/23/18
to ansible...@googlegroups.com
Not quite. How about "all machines in the groups webservers and dbservers are to be managed only if they are part of staging OR qa"?

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/fd716817-e0a0-49f3-9be1-59a07fbf1887%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kai Stian Olstad

unread,
Oct 23, 2018, 10:13:52 AM10/23/18
to ansible...@googlegroups.com
On Tuesday, 23 October 2018 08:24:37 CEST Gonzalo Servat wrote:
> Hi All
>
> When working with host patterns
> <https://docs.ansible.com/ansible/2.7/user_guide/intro_patterns.html>, is
> it possible to craft one that says "must be in group X *and* must be in
> either group Y, W and Z"?
>
> e.g. host must be in group "webserver" but it *must* also be in either
> group dc1, dc2 or dc3.
>
> I can see how to check that webserver is part of dc1 (webserver,&dc1) but
> not sure how to build it with multiple "OR" on the "AND" (something like
> "webserver,&(dc1,dc2,dc3)" is what I'm looking for but it doesn't work)

Put the dc1, dc2 and dc3 group in a group called dc and then you can use

webserver:&dc

--
Kai Stian Olstad


flowerysong

unread,
Oct 23, 2018, 11:29:35 AM10/23/18
to Ansible Project
You're trying to do it backwards. Build the union of your other groups and do the intersection with webserver.

&webserver,dc1,dc2,dc3

Gonzalo Servat

unread,
Oct 23, 2018, 8:25:12 PM10/23/18
to ansible...@googlegroups.com
Thanks, good suggestion. I suppose there's no way to say "must be webserver OR dbserver AND in (dc1 OR dc2 OR dc3)"?

- Gonz

Gonzalo Servat

unread,
Oct 23, 2018, 8:26:39 PM10/23/18
to ansible...@googlegroups.com
On Wed, Oct 24, 2018 at 1:13 AM Kai Stian Olstad <ansible-pr...@olstad.com> wrote:
[..snip..] 
 
> I can see how to check that webserver is part of dc1 (webserver,&dc1) but
> not sure how to build it with multiple "OR" on the "AND" (something like
> "webserver,&(dc1,dc2,dc3)" is what I'm looking for but it doesn't work)

Put the dc1, dc2 and dc3 group in a group called dc and then you can use

  webserver:&dc

I'd like to be able to do this on the fly so creating a custom group like that is not ideal.

- Gonz

flowerysong

unread,
Oct 23, 2018, 9:17:40 PM10/23/18
to Ansible Project
Not in a host pattern. no. If you can't do it in the inventory, you can do it with a dynamic group:

- hosts: localhost
  gather_facts: false
  tasks:
    - add_host:
        name: "{{ item }}"
        groups: target_hosts
      when: hostvars[item].group_names | intersect(['dc1', 'dc2', 'dc3'])
      loop: "{{ groups.webserver | union(groups.dbserver) }}"

- hosts: target_hosts
  tasks:
    - command: /bin/true


Gonzalo Servat

unread,
Oct 23, 2018, 9:28:20 PM10/23/18
to ansible...@googlegroups.com
On Wed, Oct 24, 2018 at 12:17 PM flowerysong <ezek...@umich.edu> wrote:

Not in a host pattern. no. If you can't do it in the inventory, you can do it with a dynamic group:

- hosts: localhost
  gather_facts: false
  tasks:
    - add_host:
        name: "{{ item }}"
        groups: target_hosts
      when: hostvars[item].group_names | intersect(['dc1', 'dc2', 'dc3'])
      loop: "{{ groups.webserver | union(groups.dbserver) }}"

- hosts: target_hosts
  tasks:
    - command: /bin/true


Thanks for that! Very useful.

- Gonz
Reply all
Reply to author
Forward
0 new messages