【问题标题】:Ansible: Run as a different user on localhostAnsible:在 localhost 上以不同用户身份运行
【发布时间】:2018-01-26 02:52:14
【问题描述】:

我想在本地主机上运行一个命令,使用ansible_connection=local,但也作为一个特定的用户,即ansible_user=my_other_user。有趣的是,这两个选项似乎是相互排斥的:

  • /etc/ansible/hostslocalhost ansible_user=my_other_user
    • ansible localhost -a "whoami" localhost | SUCCESS | rc=0 >> my_other_user
  • /etc/ansible/hostslocalhost ansible_user=my_other_user ansible_connection=local
    • ansible localhost -a "whoami" localhost | SUCCESS | rc=0 >> myself

这是错误还是功能?

如何使用配置 Ansible 的 Inventory 以使用 my_other_user 登录 localhost 而无需 ssh?

编辑:这不应该通过本地用户myself 使用 sudo。用户myselfmy_other_user 的密码不同,Ansible 不会知道myself 的密码。

EDIT2:更改为方法su(即localhost ansible_connection=local ansible_become_user=my_other_user ansible_become_method=su ansible_become=true)确实不起作用,因为在不知道root密码的情况下无法以root身份运行命令。

【问题讨论】:

    标签: linux ansible localhost ansible-inventory


    【解决方案1】:

    become

    ubuntu@test:/$ ansible all -i 'localhost,' -c local -m shell -a 'whoami'
    localhost | SUCCESS | rc=0 >>
    ubuntu
    
    ubuntu@test:/$ ansible all -i 'localhost,' -c local -b --become-user=www-data -m shell -a 'whoami'
    localhost | SUCCESS | rc=0 >>
    www-data
    

    【讨论】:

    • 这个解决方案对我不起作用,因为它需要用户 ubuntu 的密码,但我需要 Ansible 使用 www-data 的 sudo 密码。编辑问题以提高准确性。
    猜你喜欢
    • 2016-12-04
    • 2019-05-17
    • 2019-07-16
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多