Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run an Ansible task only when the hostname contains a string

Tags:

ansible

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
like image 687
sdot257 Avatar asked May 29 '15 15:05

sdot257


People also ask

How do I run a specific part of my playbook in Ansible?

The easiest way to run only one task in Ansible Playbook is using the tags statement parameter of the “ansible-playbook” command. The default behavior is to execute all the tags in your Playbook with --tags all .

What is Inventory_hostname in Ansible?

The inventory_hostname is the hostname of the current host, as known by Ansible. If you have defined an alias for a host, this is the alias name. For example, if your inventory contains a line like this: server1 ansible_host=192.168.4.10. then inventory_hostname would be server1 .

Which keyword do you use to create a conditional task in Ansible?

To implement conditions in Ansible, we use the when keyword. The keyword takes Boolean expressions based on a value or a variable from previous tasks or facts gathered from the remote hosts. This guide will teach you how to implement conditions in Ansible playbooks using the when keyword.

How does Ansible Group_vars work?

The group_vars in Ansible are a convenient way to apply variables to multiple hosts at once. Group_vars is an Ansible-specific folder as part of the repository structure. This folder contains YAML files created to have data models, and these data models apply to all the devices listed in the hosts. ini file.


2 Answers

Below should do the trick:

- name: Install PHP Modules
  command: <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

like image 95
Michal Gasek Avatar answered Oct 01 '22 20:10

Michal Gasek


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":

 when: "groups['perl'] | select('search','batch') | list"

So if that group does not have a host with that sub-string, then the list is empty, which means false condition.

like image 41
Noam Manos Avatar answered Oct 01 '22 20:10

Noam Manos