Question:
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.
1 2 3 4 5 6 7 8 9 |
- name: Install Perl Modules command: with_dict: perl_modules - name: Install PHP Modules command: with_dict: php_modules when: |
Host inventory file
1 2 3 4 5 6 7 8 9 10 11 12 |
[webs] web01 web02 web03 [perl] perl01 perl02 perl03 perl-batch01 perl-batch02 |
Answer:
Below should do the trick:
1 2 3 4 5 |
- name: Install PHP Modules command: with_dict: php_modules when: "'batch' in inventory_hostname" |
Note you’ll have a couple of skipped hosts during playbook run.
inventory_hostname
is one of Ansible’s “magic” variables:
Additionally, inventory_hostname is the name of the hostname as
configured in Ansible’s inventory host file. This can be useful for
when you don’t want to rely on the discovered hostname
ansible_hostname or for other mysterious reasons. If you have a long
FQDN, inventory_hostname_short also contains the part up to the first
period, without the rest of the domain.
Source: Ansible Docs – Magic variables and how to access information about other hosts