6

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

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

4

4 回答 4

7

在你的ansible.cfg文件中添加 sudo_flags

[defaults]
sudo_flags = -i

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

于 2015-02-24T16:03:45.473 回答
3

sudo对一堆环境变量进行了一些修改,特别是HOME. 您可以通过以下方式检查:

sudo -u user | grep HOME

如果你得到/home/user,你很好,但你可能得到/home/ssh_user

这确实与 相关sudo,与 ansible 无关。

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

Defaults    always_set_home
Defaults    set_home

再试一次sudo user | grep HOME,你应该得到正确的结果。

如果没有,请检查Defaults env_keepsudo 指令。它也可以在这里发挥作用。寻找Defaults env_keep += "HOME"in/etc/sudoers并将其删除HOME(此指令要求 sudo 对发出 sudo 的用户进行保留)

祝你好运。

编辑:这些指令也可能出现在/etc/sudoers.d/. 我不了解 Solaris,所以 YMMV。

于 2014-04-04T23:49:18.580 回答
1

我们使用一些joyent smart os机器。我通过设置如下环境来解决这个问题。这将适用于某些应用程序。

- name         : Test Envirinment
  hosts        : all
  gather_facts : no
  tasks:
    - name: echo host
      shell: echo $HOME
      sudo_user: someuser
      environment:
           HOME: "/var/lib/dir"

或者您可以使用@leucos 的建议解决方案,这将是一个更持久的解决方案

希望对加油有帮助

于 2014-04-06T12:41:56.073 回答
0

become_flags在任务级别使用

- 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'
于 2020-11-27T02:04:01.683 回答