1

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.

2
  • Maybe I did not get your question ... What about registering the command with which unix command? which nologin should return the path to the nologin command. Dec 3, 2015 at 8:46
  • In the case where the program is not in the path already (/usr/local/bin is not on CentOS), which won't find anything. Also, the programs have different possible names on different platforms. I need to look through a few paths to find out which one exists.
    – Seth
    Dec 3, 2015 at 18:05

1 Answer 1

0

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

- name: Find nologin
  command: ls -1 /usr/sbin/nologin /sbin/nologin /usr/local/sbin/nologin
  ignore_errors: yes
  register: nologin_command

IMHO a loop using with_items though more readable would make the result more difficult to use. Or maybe use find in the possible directories:

 - name: Find venv
   command: find /usr/bin /usr/local/bin /opt -executable -type f -name pyvenv -o -name pyvenv-3.4 -o -name virtualenv -o name virtualenv-3.4
   ignore_errors: yes
   register: nologin_command

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

1
  • 1
    You missed a : in the command line of the second code part Dec 20, 2015 at 10:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .