2

I am working on a playbook (for use in AWX) to handle some backend processing for one of our web apps. One of the extra variables passed to the playbook via a REST call to AWX is used to pass the hosts
to the playbook

hosts: {{target}}

target can be a single server or a list of servers.
Question: how can I use patterns to skip a host if it is not a member of an inventory group
e.g if I want the playbook to skip a server if it's in the staging group in inventory
I have tried the following:

hosts: "{{target}}:!staging"

this only works if only one server is sent as target var, however it fails if called with a list.

  • 2
    What does your `target` looks like when you have multiple hosts? – β.εηοιτ.βε Sep 30 '20 at 20:19
  • 1
    @Benoit thanks for your answer. changing the list of hosts to use colons instead of commas worked. I did update my playbook to replace the commas in the host `hosts: "{{target|replace(',', ':')}}:!staging"` – Tony Okusanya Sep 30 '20 at 21:45
  • Update: I simply had to change my delimiter to all colons or all commas This now works if my playbook is called with a list of servers separated by commas `{{ target }},!staging)` Thanks to @Benoit and @Zeitounator for your help on this – Tony Okusanya Oct 01 '20 at 04:16

1 Answers1

3

This should work if you do use : as delimiter for your hosts and not ,.


The syntax host1:host2:host3:!staging works, but host1,host2,host3:!staging, on the other hand, does generates a warning

[WARNING]: Could not match supplied host pattern, ignoring: host3:!staging

and this could well be the issue you are facing too.
The two syntaxes are documented here

Given the inventory:

all:
  hosts:
    host1:
    host2:
    host3:

  children:
    staging:
      hosts:
        host2:

And the playbook:

- hosts: host1:host2:host3:!staging
  gather_facts: no
      
  tasks:
    - debug:
        msg: "{{ inventory_hostname  }}"

This yields the recap:

PLAY [host1:host2:host3:!staging] *********************************************************************************

TASK [debug] ******************************************************************************************************
ok: [host1] => {
    "msg": "host1"
}
ok: [host3] => {
    "msg": "host3"
}

PLAY RECAP ********************************************************************************************************
host1                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
host3                      : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

And it gives the exact same recap when using the playbook:

- hosts: "{{ target }}:!staging"
  gather_facts: no
      
  tasks:
    - debug:
        msg: "{{ inventory_hostname  }}"

Run via:

ansible-playbook play.yml -i inventory.yml -e "target=host1:host2:host3"
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • 4
    It's absolutely not a bug: you either use coma or colon as a separator but you cannot mix both. Note that using coma everywhere fixes your issue as well (i.e. `{{ target }},!staging`), and that mixing delimiters the other way around breaks everything again (i.e. `{{target|replace(',', ':')}},!staging`) – Zeitounator Sep 30 '20 at 21:55
  • Thanks @Zeitounator I changed it all to use ',' (`{{ target }},!staging`)and that worked great – Tony Okusanya Oct 01 '20 at 04:08