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
    - name: git
      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.

  • Makefile error: Unexpected end of line seen
  • Migrating from SCCS to Git
  • git fsck error: “contains full pathnames”
  • expect utility is not working when executing from jenkins
  • Git Client package name for Solaris 10
  • GIT on Solaris 10: Version 'SUNW_1.2' not found because of .profile settings
  • How do I build Git on Solaris
  • git client on solaris 10 and dependencies
  • 3 Solutions collect form web for “Ansible sudo_user not using the correct $HOME directory”

    add sudo_flags in your ansible.cfg file

    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 /home/ssh_user.

    This is really something related to sudo, not to ansible.

    To circumvent this, you can add in /etc/sudoers :

    Defaults    always_set_home
    Defaults    set_home

    Try again 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)

    Good luck.

    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
        - name: echo host
          shell: echo $HOME
          sudo_user: someuser
               HOME: "/var/lib/dir"

    or you can use the suggest solution by @leucos that would be a more permanent solution

    Hope that helps cheers

    Git Baby is a git and github fan, let's start git clone.