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

CC BY-SA 4.0
2019-05-25T09:35:57.023Z
This doesn't work as the ansible attempts to use the master_user's keys, was the first thing i tried.
Aeseir
CC BY-SA 4.0
2019-05-24T12:42:05.157Z
2019-05-24T12:42:05.157Z
Vladimir Botka
<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>

CC BY-SA 4.0
2022-12-20T21:44:28.127Z
2022-12-21T05:22:20.593Z
2022-12-21T05:22:20.593Z
Steven Starling
<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>

CC BY-SA 4.0
2019-05-26T07:50:17.807Z
Option 1 works, option 2 doesn't. However when i do "sudo ansible all --private-key=... -m ping" it works. Almost there
Aeseir
CC BY-SA 4.0
2019-05-26T15:19:10.583Z
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.
Zeitounator
CC BY-SA 4.0
2019-05-25T13:43:58.593Z
2019-06-02T13:22:28.543Z
2019-06-02T13:22:28.543Z
Zeitounator
<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>

CC BY-SA 4.0
2019-05-24T12:22:19.573Z
"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).
Vladimir Botka
CC BY-SA 4.0
2019-05-24T12:34:09.430Z
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*.
Vladimir Botka
CC BY-SA 4.0
2019-05-25T09:29:36.423Z
@VladimirBotka my updated question didn't get posted hence the lack of additional info
Aeseir
<ansible>
run ansible command as another user using their ssh key
Aeseir
# 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**: @VladimirBotka 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@

then try
ansible  -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.


---

