I have multiple tasks in a role as follows. I do not want to create another yml
file to handle this task. I already have an include for the web servers, but a couple of our Perl servers require some web packages to be installed.
- name: Install Perl Modules
command: <command>
with_dict: perl_modules
- name: Install PHP Modules
command: <command>
with_dict: php_modules
when: <Install php modules only if hostname contains the word "batch">
Host inventory file
[webs]
web01
web02
web03
[perl]
perl01
perl02
perl03
perl-batch01
perl-batch02
Below should do the trick:
Note you'll have a couple of skipped hosts during playbook run.
inventory_hostname
is one of Ansible's "magic" variables:Source: Ansible Docs - Magic variables and how to access information about other hosts
You can even filter tasks for hosts of a specific group (not the whole inventory).
The following condition will execute your task - only if an inventory group [perl], has a host name, which includes the sub-string "batch":
So if that group does not have a host with that sub-string, then the list is empty, which means false condition.