I have an Ansible task based on https://github.com/al3x/sovereign/blob/master/roles/tarsnap/tasks/tarsnap.yml#L2 that keeps failing when it should succeed.
I used to run the role with ansible_ssh_user=root
, but recently switched to using a non-root user that has passwordless sudo rights and then calling become
in my playbooks.
However, now the Ansible task fails even though I've specified become=true
. Tarsnap is already installed, but the task still returns stderr: /bin/sh: tarsnap: command not found
. I think it's because of something around sudo
that I don't quite understand.
When I manually ssh into the server as this non-root user and run sudo tarsnap --version | grep 1.0.35 --color=never
, I get sudo: tarsnap: command not found
. But if I SSH in as root, that same command gives me tarsnap 1.0.35
.
Similarly, when I run sudo -i tarsnap --version | grep 1.0.35 --color=never
(note the -i), I get tarsnap 1.0.35
.
I am using CentOS 7.
1) Why are the results different for sudo
vs sudo -i
?
2) How do I fix my Ansible task?
Point to tarsnap with a fully-qualified path, or set your PATH explicitly so it can find tarsnap. You can find the missing path element by logging in as root and executing
which tarsnap
You don't mention what flavour of Linux you are using, so bear in mind that /bin/sh may very well not be the same as /bin/bash (eg. ~/.bashrc and ~/.bash_profile may not be read).
sudo -i
will give you an 'interactive' shell. You should see the manual page for bash (if indeed you are using bash) to find out what 'interactivemeans (ie. which files are consulted). I think you'll find that
sudo -iwill read in ~/.bashrc, while just
sudo(without
-i`) will not.