ansibleansible-ad-hoc

Ansible: Consider using 'become', 'become_method', and 'become_user' rather than running sudo


When I run this:

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

I get warning:

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

host | SUCCESS | rc=0 >>

But what would be equivalent using become for sudo su - otheruser -s /bin/bash?

I tried:

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

But I get:

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 version: 2.6.4


Solution

  • I had to specify --become_method su. Or more specifically ansible host -u myuser --become-method su --become-user otheruser -a "/bin/echo hello"

    P.S. I'm passing -a "/bin/echo hello", because ansible expect to run some command after changing user.