I'm ansibilizing a couple servers for a python application, some of which are CentOS, others are debian.
Is there a convenient way to locate a program that could be installed in various places, without resorting to the example below, and without repeating the same command several times (with different when:
clauses)?
For example, on CentOS it's /sbin/nologin
, on debian it's /usr/sbin/nologin
.
I have been trying to find and register the path in a variable, but it seems silly:
- name: Find nologin
command: ls -1 /usr/sbin/nologin
ignore_errors: yes
register: nologin_command
- name: Find nologin
command: ls -1 /sbin/nologin
register: nologin_command
when: nologin_command.stdout == ""
# nologin_command.stdout will be the path to one or the other
In my particular case, I'm actually trying to find the correct virtualenv_command
for the pip module. I have to be specific or I'll get the wrong one (i.e., for Python 2) or a path that doesn't exist.
My systems will have one of pyvenv, pyvenv-3.4, virtualenv, or virtualenv-3.4. At least one will be present, but, depending on the operating system and how python was installed, they are in different places or don't exist at all.
If you knew the rules that you can use to infer the location it would be better to use them (CentOS with python2.7 should have it...)
Well you probably could do something like
IMHO a loop using
with_items
though more readable would make the result more difficult to use. Or maybe usefind
in the possible directories:Once you find it instead of registering the variable it wouldn't hurt to store it as a custom fact so it's available next time: http://serverascode.com/2015/01/27/ansible-custom-facts.html