Had a somewhat curious situation: need to run a task in Ansible in different user accounts, but I don’t have sudo
or 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:
|
|
The task Try user 1
will login as user user1
, and Try user 2
will open a new connection and login as user2
.
This can even be used in a loop:
|
|
Every whoami
command will be executed as a different login user.
Excuse me while I rewrite my Playbook.
Categories:
[Ansible]