According Patterns: targeting hosts and groups it should be possible to apply a pattern
to choose which managed nodes or groups you want to execute against
Even if there are some limitations of patterns, it might be possible to use advanced pattern options in example using regexes in patterns.
You can specify a pattern as a regular expression by starting the pattern with ~
Please take note that according patterns and ad-hoc commands
single quotes MUST be used to prevent bash interpolation.
Regarding your question:
Is there any other better solution is there to do so? How can I ignore the group name when I use the limit flag and only take the hostnames?
and based on your given sample inventory file hosts.yml
, a playbook called with
ansible-playbook -i hosts.yml sample.yml --limit='~.*[\d]{1}(1|3|5|7|9)$'
would execute on
PLAY RECAP *******************************************************************************************************
web_host01 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web_host03 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web_host05 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web_host07 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
web_host09 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
weblogic_host01 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
weblogic_host03 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
weblogic_host05 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
weblogic_host07 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
weblogic_host09 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
As you can see hosts of the group weblogic-oel7
are catched but not all of them.
Summary
The pattern to catch odd numbers
--limit='~.*[\d]{1}(1|3|5|7|9)$'
and to catch even numbers
--limit='~.*[\d]{1}(0|2|4|6|8)$'
which can have a one leading digit but not a letter.
Thanks to
How to proceed further?
Regarding
The actual playbook takes the group names and then limits them with odd or even ...
you may have a look into How to build your inventory, hosts in multiple groups and inheriting variable values: group variables for groups of groups since it might be possible to introduce
groups of groups using the :children
suffix in INI or the children:
entry in YAML.
With this you create groups like weblogic_odd
and weblogic_even
. This approach is in example used to address infrastructure (and) locations.
Further Documentation
Can be found in the thread of Ansible Issue #79822 where an "Incorrect behavior of regular expression in ansible-playbook --limit
option" is assumed but it turned out it is not. It is just
... constructing the host pattern, not selecting/excluding the hosts.
in other words, the parameter limit
and his regex capability is to define sets, but it is not mentioned as regex filter for hosts and as I understand from there.