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

http://docs.ansible.com/git_module.html

  • How to pull while deployment in ansible
  • creating jenkins jobs with ansible
  • Ansible git module not checking out a branch
  • GIT over SSH in Ansible hangs, eventhough ssh-agent forwarding is set up
  • What user will Ansible run my commands as?
  • SSH Agent Forwarding with Ansible
  • - hosts: myhost
      sudo_user: user
      tasks:
        - name: git
          git: repo=git@github.com: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.

  • Git looking for my SSH key in the wrong location
  • How to use submodules publicly, but symlinks to a single clone locally?
  • git repository with folders in different locations
  • How do you resolve git conflicts in yarn.lock
  • Running .sh scripts in Git bash
  • Git: Merging only branch specific commits
  • 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 /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
      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

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