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).

  • What user will Ansible run my commands as?
  • GIT over SSH in Ansible hangs, eventhough ssh-agent forwarding is set up
  • Get ansible-playbook run information
  • How can I include the development branch of Ansible with my playbooks when checked out via Git?
  • SSH Agent Forwarding with Ansible
  • Git Ansible Clone issue
  • - 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.

  • safely delete a local branch after rebase - replace of git branch --merged
  • How to make Git “forget” about a file that was tracked but is now in .gitignore?
  • Smallest, toy, Distributed version control system to study
  • How to change repo with magit emacs?
  • How does visual studio 2013 remember the mapping between a remote git/tfs repository and a local path?
  • how to create a gist in github that returns json data
  • 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.