Git Ansible sudo_用户未使用正确的$HOME目录

Git Ansible sudo_用户未使用正确的$HOME目录,git,solaris,ansible,ansible-playbook,Git,Solaris,Ansible,Ansible Playbook,我有一个git目录,该目录由一个我不能作为ssh的用户拥有。我目前正在使用sudo\u user:user,它正在工作,但似乎没有正确设置$HOME。我的用户帐户在~/.ssh/known\u hosts文件中有github,但它正在添加到我的~ssh\u user/.ssh/known\u hosts文件中(从accept\u hostkey=yes) 我需要做些什么来告诉ansible正确使用$HOME吗 我在Joyent之外的Solaris上运行,但我认为这并不直接适用于此问题。sudo

我有一个git目录,该目录由一个我不能作为ssh的用户拥有。我目前正在使用
sudo\u user:user
,它正在工作,但似乎没有正确设置$HOME。我的用户帐户在~/.ssh/known\u hosts文件中有github,但它正在添加到我的~ssh\u user/.ssh/known\u hosts文件中(从accept\u hostkey=yes)

我需要做些什么来告诉ansible正确使用$HOME吗


我在Joyent之外的Solaris上运行,但我认为这并不直接适用于此问题。

sudo
对一系列环境变量,尤其是
HOME
造成了一些混乱。您可以通过以下方式进行检查:

sudo -u user | grep HOME
如果您得到了
/home/user
,您就很好了,但是您可能得到了
/home/ssh\u user

这确实与sudo有关,而不是ansible

为了避免这种情况,您可以添加
/etc/sudoers

Defaults    always_set_home
Defaults    set_home
请重试
sudo用户| grep HOME
,您应该会得到正确的结果

如果没有,请检查
Defaults env_keep
sudo指令。它也可以在这里发挥作用。搜索
/etc/sudoers
中的
默认环境保持+=“HOME”
,并删除它(此指令要求sudo从发出sudo的用户那里保留
HOME

祝你好运


编辑:这些指令也可能出现在
/etc/sudoers.d/
中。我不了解Solaris,所以我们使用了一些joyent智能操作系统机器。我通过如下设置环境来解决这个问题。这将适用于某些应用程序

- name         : Test Envirinment
  hosts        : all
  gather_facts : no
  tasks:
    - name: echo host
      shell: echo $HOME
      sudo_user: someuser
      environment:
           HOME: "/var/lib/dir"
或者您可以使用@leucos的建议解决方案,这将是一个更持久的解决方案


希望这有助于欢呼

在您的ansible.cfg文件中添加sudo_标志

[defaults]
sudo_flags = -i

这将运行sudo用户的登录脚本,以填充环境变量,如$HOME

- hosts: myhost
  become_user: user
  tasks:
    - name: git
      git: repo=git@github.com:user/repo.git dest=/home/user/repo update=no accept_hostkey=yes
      become_flags: '-E'
- hosts: myhost
  become_user: user
  tasks:
    - name: git
      git: repo=git@github.com:user/repo.git dest=/home/user/repo update=no accept_hostkey=yes
      become_flags: '-E'