Excluding subset of hosts in Ansible inventory
Asked Answered
M

4

5

I have a set of hosts named vm2-prod through vm14-prod. These are grouped in the Ansible hosts inventory as vmware_hosts. I want to exclude a subset of hosts like this:

- name: testing play
  hosts: vmware_hosts:!vm[8-10]-prod

According to the Ansible docs, it seems this should exclude vm7-prod through vm9-prod. However, I get the "could not match host pattern, ignoring vm[8-10]-prod" like it's reading the literal host name "vm[8-10]-prod" rather than an array.

I also tried vm[8:10]-prod, but it gives the same error.

What is wrong?

Misti answered 3/12, 2020 at 15:35 Comment(0)
H
5

According to the Ansible docs

=> which one exactly ?

I perfectly remember seeing this notation documented for adding ranges in inventories but not at all for hosts patterns.

Maybe you are confusing with the use of regexps in host patterns which should apparently fulfill your requirement (and will look very similar in this case):

hosts: vmware_hosts:!~vm[8-10]-prod

Meanwhile, although the above can come handy to specify a limit on the command line for a particular playbook run, for a more regular use, I would recommend to add those machines to an other group that would be much easier to exclude and to maintain on the long run.

Hemianopsia answered 3/12, 2020 at 16:33 Comment(0)
W
4

According to the section "Commonn Patterns" shown in the documentation, the pattern :! only allows you to exclude groups

As mentioned in other answers, you can use a regular expressión pattern like:

hosts: vmware_hosts:!~vm(8|9|10)-prod

The problem with this approach is that if you need to exclude those hosts in several places inside your playbooks you will end up copy and pasting the regular expression all over the place. Then you will have a lot of fun adding or removing hosts to the excluded list.

Another option is to exclude groups. I always use this one when I need to exclude the same hosts in several places in my playbooks.

Create a group called excluded_vms:

[exluded_vms]
vm8-prod
vm9-prod
vm10-prod

And exclude that group using

hosts: vmware_hosts:!excluded_vms

If tomorrow I need to exclude another host, I just need to add it to the excluded_vms. Piece of cake!

Wheatley answered 4/12, 2020 at 6:37 Comment(2)
...the pattern ':!' only allows you to exclude groups. => The documentation is listing "common" patterns. I'm actually not sure I agree with the authors it's "uncommon", but excluding a single host (or several as you later show in your answer) is totally possible. This statement is hence wrong.Hemianopsia
The regular expression pattern works! I instinctively used [] for arrays without realizing, in this context, it should be the pipe symbol.Misti
W
0

I think you can only exclude groups of hosts. Like in Ansible docu there are only examples of pattern with the * symbol, so vm*-prod is possible. With your syntax you can only select lines of a definded group e.g. Using group position in patterns.

Wilkens answered 3/12, 2020 at 16:10 Comment(1)
You can definitely exclude individual hosts in a pattern.Hemianopsia
M
0

This was resolved through a simple syntax change as quoted from a previous answer:

hosts: vmware_hosts:!~vm(8|9|10)-prod

In coming from a different CM background (xCAT in my case), it's not immediately apparent from the Ansible docs that the host array should use pipes instead of square brackets. I was able to exclude multiple individual hosts in my playbooks using this syntax.

Misti answered 14/12, 2020 at 20:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.