I am trying to use shell to run the modinfo binary, and also the ip binary.
I can run the command I want manually by ssh-ing into the machine.
But when I do it with Ansible, shell says "command not found", because the $PATH is different between SSH and Ansible shell.
[centos@dell03 ~]$ which modinfo /usr/sbin/modinfo [centos@dell03 ~]$ echo $PATH /usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/centos/.local/bin:/home/centos/bin [centos@dell03 ~]$ modinfo ext4
That last command prints out a lot of text, and returns 0.
Then I try with ansible:
--- - hosts: localhost tasks: - name: check modinfo shell: | echo $PATH which modinfo modinfo ext4 args: executable: /bin/bash
Expected results:
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/centos/.local/bin:/home/centos/bin
followed by a lot of text about the ext4 module
i.e. shell can find the modinfo binary because the PATH is the same as before.
Actual Resultfatal: [10.58.2.103]: FAILED! => { "changed": true, "cmd": "echo $PATH\nwhich modinfo\nmodinfo ext4 \n", "delta": "0:00:00.003893", "end": "2019-05-03 16:50:23.683406", "invocation": { "module_args": { "_raw_params": "echo $PATH\nwhich modinfo\nmodinfo ext4 \n", "_uses_shell": true, "argv": null, "chdir": null, "creates": null, "executable": "/bin/bash", "removes": null, "stdin": null, "stdin_add_newline": true, "strip_empty_ends": true, "warn": true } }, "msg": "non-zero return code", "rc": 127, "start": "2019-05-03 16:50:23.679513", "stderr": "which: no modinfo in (/usr/local/bin:/usr/bin)\n/bin/bash: line 2: modinfo: command not found", "stderr_lines": [ "which: no modinfo in (/usr/local/bin:/usr/bin)", "/bin/bash: line 2: modinfo: command not found" ], "stdout": "/usr/local/bin:/usr/bin", "stdout_lines": [ "/usr/local/bin:/usr/bin" ] }
Reading the results:
echo $PATH -> /usr/local/bin:/usr/bin , which is not the path from before
So bash could not find the modinfo binary.
As a workaround, I am able to manually look up where the binary I want is, then prepend "$PATH=$PATH:/usr/sbin " to my shell command. That works, but is not scalable.
tldr, how can I make the shell task use the same $PATH as a normal ssh login?
Thanks,
Matt
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/90f676db-e2e8-4fba-9943-2cbdfa2126b6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
PATH="" PS1="" . /home/centos/.bashrc ; echo $PATHTo unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/90f676db-e2e8-4fba-9943-2cbdfa2126b6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
vars:
paths_extras: ['/usr/local/sbin', '/usr/local/bin', '/sbin', '/bin', '/usr/sbin', '/usr/bin']
environment:
PATH: |-
{% set new_path = ansible_env.PATH.split( ':' ) %}
{% for path in paths_extras %}
{% if path not in new_path %}
{{ new_path.append(path) }}
{% endif %}
{% endfor %}
{{ new_path | join( ':' ) }}
ssh centos@10.1.1.2 ./yourCommand here