Ignore case sensitivity in Ansible when condition?

Michael picture Michael · Jul 15, 2016 · Viewed 14.4k times · Source

I am trying to create an ansible role that depends on other roles only if the hostname of the machine it's being run on contains a certain string. For example:

  dependencies:
- { role: example-role, when: "'hostname' in ansible_hostname" }

As it is, the role will run when the hostname of the machine contains the phrase "hostname". However, many of the machines in our building have hostnames that are upper case, for example, it may contain "HOSTNAME". In this case, the above statement will not work. How can I make this work (aside from duplicating the line and putting "hostname" in all caps) to ignore letter cases?

Answer

Dean Fenster picture Dean Fenster · Jul 15, 2016

Convert the string to lowercase and only check that. Like this:

  dependencies:
- { role: example-role, when: "'hostname' in ansible_hostname|lower" }