Pedro Gordo
Pedro Gordo

Reputation: 1865

How to make Ansible read the path variables?

I have a few scripts in my ~/bin that are loaded when I start a shell because this has been included in my $PATH. However, if I have a task in an Ansible playbook that tries to execute one of those scripts, it fails with:

fatal: [node3]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"}, "changed": true, "cmd": "nodetool status", "delta": "0:00:00.002779", "end": "2019-12-03 00:17:18.595695", "msg": "non-zero return code", "rc": 127, "start": "2019-12-03 00:17:18.592916", "stderr": "/bin/sh: 1: nodetool: not found", "stderr_lines": ["/bin/sh: 1: nodetool: not found"], "stdout": "", "stdout_lines": []}

In this case the script is ~/bin/nodetool. If I give the absolute path to Ansible it works, so I guess Ansible is not loading $PATH.

How can I force Ansible to load the $PATH environment variables so I can access those scripts without having to provide full path?

Upvotes: 1

Views: 1027

Answers (2)

texasdave
texasdave

Reputation: 736

For anyone looking this up, I found this info that was somewhat discouraging. I needed to have ansible use the conda binary inside the miniconda folder in my "demo" users account. It was not able to, constantly giving the "command not found error". My only solution was to give it the full path to the demo user's conda binary.. Doing this for several tasks is annoying, but it was the only way.. Then I read this:

bcoca commented on Apr 21, 2016 This is expected and by design, Ansible is a batch system and avoid interactive logins if possible, which means it won't source certain user/shell specific files.

from this file:

https://github.com/ansible/ansible/issues/15518

Upvotes: 0

Zeitounator
Zeitounator

Reputation: 44675

I guess your path is set in your .bashrc which is not loaded by ansible when running shell.

There is a discussion in a closed (won't fix) ticket explaining why this is the case. You need to install those commands globally, or source .bashrc && command, or set the path globally....

Upvotes: 2

Related Questions