Ansible:考虑使用'become','become_method'和'become_user'而不是运行sudo

时间:2018-09-22 12:26:39

标签: ansible ansible-ad-hoc

运行此命令时:

ansible host -u myuser -a "sudo su - otheruser -s /bin/bash"

我得到警告:

[WARNING]: Consider using 'become', 'become_method', and 'become_user' rather
than running sudo

host | SUCCESS | rc=0 >>

但是将become用于sudo su - otheruser -s /bin/bash会等同于什么?

我尝试过:

ansible host -u myuser --become --become-user otheruser -a "/bin/echo hello"

但是我得到了

host | FAILED! => {
    "changed": false,
    "module_stderr": "Shared connection to host closed.\r\n",
    "module_stdout": "sudo: a password is required\r\n",
    "msg": "MODULE FAILURE",
    "rc": 1
}

P.S。 Ansible版本:2.6.4

1 个答案:

答案 0 :(得分:4)

我必须指定--become_method su。或更具体地说,ansible host -u myuser --become-method su --become-user otheruser -a "/bin/echo hello"

P.S。我正在传递-a "/bin/echo hello",因为ansible希望在更改用户后运行一些命令。