Answers: <p>If you're able to connect to the <em>host</em></p>
  
  <pre><code>$ ssh ansible@&lt;host&gt;
  </code></pre>
  
  <p>then try</p>
  
  <pre><code>ansible &lt;hosts&gt; -u ansible -m ping
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-05-25T09:35:57.023Z
Answers: This doesn't work as the ansible attempts to use the master_user's keys, was the first thing i tried.
Answers: Aeseir
Answers: CC BY-SA 4.0
Answers: 2019-05-24T12:42:05.157Z
Answers: 2019-05-24T12:42:05.157Z
Answers: Vladimir Botka
Answers: <p>Also, you need to point to the ssh user's certificates created. It should like this:</p>
  <pre><code>sudo /usr/local/bin/ansible-playbook \
    example.yml \
    -i example_hosts.ini\
    --private-key=/home/example/.ssh/id_rsa
  </code></pre>
  <p>This worked for me.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-12-20T21:44:28.127Z
Answers: 2022-12-21T05:22:20.593Z
Answers: 2022-12-21T05:22:20.593Z
Answers: Steven Starling
Answers: <p><code>become_user</code> will tell which user to become to run a particular ansible module <strong>on the remote server</strong>.</p>
  
  <p>If you want to run the <code>ansible</code> command on your control machine as an other user, you need to use <code>sudo</code> or <code>su</code> on this machine e.g.</p>
  
  <p><code>sudo -s -u ansible ansible all -m ping</code></p>
  
  <p>The <code>-s</code> option will load the user's shell an make sure homedir and all other envs are correctly set prior to running the command.</p>
  
  <p>An other possible solution would be to use the ssh key you created for the ansible user from your account</p>
  
  <p><code>ansible all --private-key=/home/ansible/.ssh/id_rsa -m ping</code></p>
  
  <p>This will potentially work only if your current user has read access to the given key.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-05-26T07:50:17.807Z
Answers: Option 1 works, option 2 doesn't. However when i do "sudo ansible all --private-key=... -m ping" it works. Almost there
Answers: Aeseir
Answers: CC BY-SA 4.0
Answers: 2019-05-26T15:19:10.583Z
Answers: Option 2 will only work if your user can actually read the key of the other user which is rarely the case out of the box for security reasons. For option 1, I  think I know where your problem stands. Edit on the way.
Answers: Zeitounator
Answers: CC BY-SA 4.0
Answers: 2019-05-25T13:43:58.593Z
Answers: 2019-06-02T13:22:28.543Z
Answers: 2019-06-02T13:22:28.543Z
Answers: Zeitounator
Body: <p>i am trying to figure out how to run an ansible ping command as another user, using their ssh key.</p>
  
  <p>User setup i have essentially:</p>
  
  <ul>
  <li><p>master_user (me)</p></li>
  <li><p>ansible (sudo user, pwdless sign in)</p></li>
  </ul>
  
  <p>I logged in as ansible user and did:</p>
  
  <ul>
  <li>ssh-keygen (generate key)</li>
  <li>ssh-copy-id XXXX (copy the ssh key to corresponding server)</li>
  </ul>
  
  <p>Tested it all and it works fine (each server has ansible user already fyi).
  I updated the hosts file to have all my hosts as well.</p>
  
  <p>Now when i remote into master_user, i want to run a ansible command but as ansible user something like this:</p>
  
  <pre><code>ansible all --become-user=ansible -m ping
  </code></pre>
  
  <p>This fails because it is trying to ssh into the servers using the master_users ssh key.</p>
  
  <p>Error message: <code>Failed to connect to the host via ssh</code></p>
  
  <p>When i run -vvvv flag i can see it trying to user master_user ssh by looks of it</p>
  
  <pre><code>Offending key for IP in /home/master_user/.ssh/
  </code></pre>
  
  <p>How can configure it so that it uses ansible user always and use ansible users ssh key?</p>
  
Comments: CC BY-SA 4.0
Comments: 2019-05-24T12:22:19.573Z
Comments: "man ansible" synopsis says "ansible <host-pattern> [options]". There is no <host-pattern> in the command you posted. What is the error message? Please follow: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve).
Comments: Vladimir Botka
Comments: CC BY-SA 4.0
Comments: 2019-05-24T12:34:09.430Z
Comments: See [Understanding Privilege Escalation](https://docs.ansible.com/ansible/latest/user_guide/become.html#understanding-privilege-escalation) . Make sure you understand the differences among *ansible_user*, *remote_user*, and *become_user*.
Comments: Vladimir Botka
Comments: CC BY-SA 4.0
Comments: 2019-05-25T09:29:36.423Z
Comments: @VladimirBotka my updated question didn't get posted hence the lack of additional info
Comments: Aeseir
Tags: <ansible>
Title: run ansible command as another user using their ssh key
UserDisplayName: Aeseir
ThreadText: # run ansible command as another user using their ssh key
  
  (Asked by: username_0 on 2019-05-24)
  
  i am trying to figure out how to run an ansible ping command as another user, using their ssh key.
  
  User setup i have essentially:
  
  * master\_user (me)
  * ansible (sudo user, pwdless sign in)
  
  I logged in as ansible user and did:
  
  * ssh-keygen (generate key)
  * ssh-copy-id XXXX (copy the ssh key to corresponding server)
  
  Tested it all and it works fine (each server has ansible user already fyi).
  I updated the hosts file to have all my hosts as well.
  
  Now when i remote into master\_user, i want to run a ansible command but as ansible user something like this:
  
  ```
  ansible all --become-user=ansible -m ping
  
  ```
  
  This fails because it is trying to ssh into the servers using the master\_users ssh key.
  
  Error message: `Failed to connect to the host via ssh`
  
  When i run -vvvv flag i can see it trying to user master\_user ssh by looks of it
  
  ```
  Offending key for IP in /home/master_user/.ssh/
  
  ```
  
  How can configure it so that it uses ansible user always and use ansible users ssh key?
  
  **Tags:** ansible
  
  > **Comments:**
  > **username_1**: "man ansible" synopsis says "ansible <host-pattern> [options]". There is no <host-pattern> in the command you posted. What is the error message? Please follow: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve).  
  > **username_1**: See [Understanding Privilege Escalation](https://docs.ansible.com/ansible/latest/user_guide/become.html#understanding-privilege-escalation) . Make sure you understand the differences among *ansible_user*, *remote_user*, and *become_user*.  
  > **username_0**: @username_1 my updated question didn't get posted hence the lack of additional info  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_3 on 2019-05-25. Score: 4 | Accepted answer
  
  `become_user` will tell which user to become to run a particular ansible module **on the remote server**.
  
  If you want to run the `ansible` command on your control machine as an other user, you need to use `sudo` or `su` on this machine e.g.
  
  `sudo -s -u ansible ansible all -m ping`
  
  The `-s` option will load the user's shell an make sure homedir and all other envs are correctly set prior to running the command.
  
  An other possible solution would be to use the ssh key you created for the ansible user from your account
  
  `ansible all --private-key=/home/ansible/.ssh/id_rsa -m ping`
  
  This will potentially work only if your current user has read access to the given key.
  
  > **Comments:**
  > **username_0**: Option 1 works, option 2 doesn't. However when i do "sudo ansible all --private-key=... -m ping" it works. Almost there  
  > **username_3**: Option 2 will only work if your user can actually read the key of the other user which is rarely the case out of the box for security reasons. For option 1, I  think I know where your problem stands. Edit on the way.  
  
  ---
  
  ### Answer by username_1 on 2019-05-24. Score: 1
  
  If you're able to connect to the *host*
  
  ```
  $ ssh ansible@<host>
  
  ```
  
  then try
  
  ```
  ansible <hosts> -u ansible -m ping
  
  ```
  
  > **Comments:**
  > **username_0**: This doesn't work as the ansible attempts to use the master_user's keys, was the first thing i tried.  
  
  ---
  
  ### Answer by username_2 on 2022-12-20. Score: 0
  
  Also, you need to point to the ssh user's certificates created. It should like this:
  
  ```
  sudo /usr/local/bin/ansible-playbook \
    example.yml \
    -i example_hosts.ini\
    --private-key=/home/example/.ssh/id_rsa
  
  ```
  
  This worked for me.
  
  
  ---
  
  