Ashi
Ashi

Reputation: 21

ansible switch users in ansible 2.4.2

I am using ansible 2.4.2.0 and want to use ansible_ssh_user as user1 and then run the commands in reote box as user2. How can we achieve this. I have tried using:

become: yes become_user: user2

But this is not working ' saying user1 doesnot have provileges to execute commands on remote machine as user2'.

Can someone please help?

Upvotes: 0

Views: 52

Answers (1)

Boxrick
Boxrick

Reputation: 21

I have continuing problems with this and it always ends up being a struggle due to nonsense with permissions

1) First of all refer to this specific bit:

https://docs.ansible.com/ansible/2.5/user_guide/become.html#becoming-an-unprivileged-user

And usually this line in config:

allow_world_readable_tmpfiles = true 

should get you around your problem

2) The next option is a bit of a hack and is to simply run it directly as a user:

"sudo -u USER bash -c 'COMMAND TO RUN HERE'"

3) Finally the last option is, do you actually need to run that command as that user? Can it simply be done using sudo: true using things like owner: otheruser

Hope this helps

Upvotes: 0

Related Questions