Reputation: 143
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.
Upvotes: 2
Views: 3105
Reputation: 39079
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"
Upvotes: 3