Override Ansible SSH login user with SUDO user
<p>I have a situation wherein it is required to override the default SSH ansible connection. We use Jenkins agents as the ansible masters and we have the SSH setup between the "JenkinsUser" (Jenkins) and the "TargetUser1" (Targets) and has been defined in the inventory file</p>

<pre><code>[dev:vars]
ansible_ssh_user=TargetUser1
</code></pre>

<p>Now, in the same target machine, I have another set of tasks which are owned by "TargetUser2" and "TargetUser1" cannot have permissions to run those. But, the same "JenkinsUser" has the SUDO permission to "TargetUser2". My playbook looks like,</p>

<pre><code>- hosts: dev
  remote_user: JenkinsUser
  become_method: sudo
  become_user: TargetUser2
  tasks:
    - command: whoami
</code></pre>

<p>Expected Result: JenkinsUser would SSH to target host and sudo to TargetUser2, overriding the default SSH setup to TargetUser1.</p>

<p>Runtime Result: SSH connection is happening as TargetUser1 instead of JenkinsUser whereas the remote_user: JenkinsUser should be overriding the default ansible_ssh_user</p>

<p>Ansible-playbook -vvv output:</p>

<pre><code>TASK [setup]     
&lt;xyz.somehost.com&gt; ESTABLISH CONNECTION FOR USER: TargetUser1 on PORT
22 TO xyz.somehost.com
</code></pre>