HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

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

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
ansibleyouuserhowsimulatesudo

Problem

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: 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.

Solution

I met the same situation. I have login1 for my machine, passwordless sudo and login2, under which I should perform some actions. I did not solve it by ansible means. But I made such a workaround:

- name: "Install nvm"
  shell: sudo -u buildkite-agent bash -c ""
  become: true


Such a case:

- name: "Install nvm"
  shell: sudo -u buildkite-agent 
  become: true


do not change home to buildkite-agent's home, i.e. uses /root as home.

Code Snippets

- name: "Install nvm"
  shell: sudo -u buildkite-agent bash -c "<my commands to be performed on behalf of buildkite-agent>"
  become: true
- name: "Install nvm"
  shell: sudo -u buildkite-agent <my commands to be performed on behalf of buildkite-agent>
  become: true

Context

StackExchange DevOps Q#3588, answer score: 2

Revisions (0)

No revisions yet.