Ansible sudo_user not using the correct $HOME directory
I have a git directory that is owned by a user that I cannot ssh as. I’m currently using
sudo_user: user which is working but does not seem to be setting $HOME correctly. My user account has github in the ~/.ssh/known_hosts file but it is being added to my ~ssh_user/.ssh/known_hosts file (from the accept_hostkey=yes).
- hosts: myhost sudo_user: user tasks: - name: git git: firstname.lastname@example.org:user/repo.git dest=/home/user/repo update=no accept_hostkey=yes
Is there something I need to do to tell ansible to use $HOME correctly?
I’m running on Solaris off of Joyent but I don’t think that’s directly applicable to this problem.
3 Solutions collect form web for “Ansible sudo_user not using the correct $HOME directory”
add sudo_flags in your ansible.cfg file
[defaults] sudo_flags = -i
this will run you sudo user‘s login script to populate environemt variables like $HOME
sudo does some mangling with a bunch of environment variables, notably
HOME. You can check this with :
sudo -u user | grep HOME
If you get
/home/user, you’re fine, but you probably get
This is really something related to
sudo, not to ansible.
To circumvent this, you can add in
Defaults always_set_home Defaults set_home
sudo user | grep HOME, you should get proper result.
If not, check for a
Defaults env_keep sudo directive. It can also play a role here. Hunt for
Defaults env_keep += "HOME" in
/etc/sudoers, and remove it (this directive asks sudo to preserve
HOME from the user issuing sudo)
EDIT: These directives might also appear in
/etc/sudoers.d/. I don’t know about Solaris, so YMMV.
We use some joyent smart os machines. I get around the problem by setting the environment like below. That will work for some application.
- name : Test Envirinment hosts : all gather_facts : no tasks: - name: echo host shell: echo $HOME sudo_user: someuser environment: HOME: "/var/lib/dir"
or you can use the suggest solution by @leucos that would be a more permanent solution
Hope that helps cheers