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 thatsudo -iwill read in ~/.bashrc, while justsudo(without-i`) will not.

| improve this answer | |
  • Thanks, fully qualified fixed it. Using Centos 7. What's weird is if I do sudo -i which tarsnap I get /usr/local/bin/tarsnap; but then I run sudo echo $PATH and :/usr/local/bin: is part of the path so I don't get why a normal non-interactive sudo can't find tarsnap since it should be checking that path. – Jeff Widman May 30 '15 at 18:49
  • Please show a verbatim transcript of how you determined what PATH was. – Cameron Kerr May 30 '15 at 20:33
  • 1) SSH in as my non-root user. Using the default CentOS bash shell. 2) Run sudo echo $PATH 3) Result: /usr/lib64/ccache:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/non-root-user/.local/bin:/home/non-root-user/bin <-- result contains /usr/local/bin – Jeff Widman May 31 '15 at 5:34
  • 1
    Thought so. The shell is interpreting sudo echo $PATH before sudo is run. Change the $PATH to \$PATH – Cameron Kerr May 31 '15 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.