Ansible: How to globally set PATH for solaris

2020-07-18 04:53发布

问题:

I am writing Ansible playbooks to setup and install our applications on Solaris servers.

The problem is that the (bash) scripts which I need to execute all assume that a certain directory lies on the PATH, namely /data/bin - which would normally not be a problem were it not for Ansible ignoring all the .profile and .bashrc config.

Now, I know that you can specify the environment for shell tasks via the environment flag, for example like this:

- shell: printenv
  environment:
    PATH: /usr/bin:/usr/sbin:/data/bin

This will properly path the /data/bin folder, and the printenv command will correctly display (or my bash scripts would correctly run).

But. There are two problems however:

  • First of all it is very annoying to have to specify the environment over and over again. I know that you can define the environment in some playbook base file variable and the reference that, but you still have to set environment: ... on every single shell task.
  • Secondly, the above example does not allow me to specify the path dynamically, e.g. as PATH: $PATH:/data/bin - because Ansible executes this in a way which does not resolve $PATH, thus the command fails catastrophically. So essentially this will override any other changes to PATH.

I am looking for a solution where

  • the additional PATH entry should only be added once
  • the additional PATH entry should not override entries added by other tasks

P.S. I found this nice explanation on how to do this on Linux, but it makes use of /etc/environment which does not exist on Solaris. (And /etc/profile is once again ignored by Ansible.)

回答1:

try adding -o SendEnv=PATH to ssh_args in ansible.cfg. Requires that

  • the shell in which you run ansible has /data/bin in PATH. Or however ansible allows you to modify the current/local PATH variable.
  • remote machine has AcceptEnv set correctly.