How do you simulate sudo su <user> in Ansible?

HOW TO -️ October 18, 2021

I have a server where my user is able to sudo to the root user without a password, and then the root user can sudo to a third user without a password. However, my user cannot sudo directly to the third user without a password.

Ansible's become directive uses sudo in the traditional manner, i.e. the one that requires prompting for my password.

I've tried putting become_user: root on a block and become_user: <other-user> on a task inside that block, but it appears Ansible overrides the block's definition, rather than nesting the sudo calls as I hoped.

Note: this question has also been asked on StackOverflow, but since there's no solution I figured I would repost it here, which I think is the better site for it.

Answer

my user cannot sudo directly to the third user without a password - is that under ansible or directly in the shell? @DanCornilescu In general as per sudoers configuration. Maybe sudo "sudo -u target\_user command" as command ? @Xiong Chiamiov, did you find a workaround that worked for you ? I have the exact same problem :(

Initializing...