Had a somewhat curious situation: need to run a task in Ansible in different user accounts, but I don’t have
su access, only ssh access for every account. Took me a moment to figure out how to do that.
In Ansible one can use the $remote_user variable which specifies the account used on the remote system. Usually this is used on a Play(book) level:
Then the entire Play will use
someremoteuser as login. So far, so good. But I need to change
remote_user for single tasks. That is possible as well:
Try user 1 will login as user
Try user 2 will open a new connection and login as
This can even be used in a loop:
whoami command will be executed as a different login user.
Excuse me while I rewrite my Playbook.