Ansible: ansible_user in inventory vs remote_user in playbook
Asked Answered
D

2

26

I am trying to run an Ansible playbook against a server using an account other than the one I am logged on the control machine. I tried to specify an ansible_user in the inventory file according to the documentation on Inventory:

[srv1]
192.168.1.146 ansible_connection=ssh ansible_user=user1

However Ansible called with ansible-playbook -i inventory playbook.yml -vvvv prints the following:

GATHERING FACTS ***************************************************************
<192.168.1.146> ESTABLISH CONNECTION FOR USER: techraf

What worked for me was adding the remote_user argument to the playbook:

- hosts: srv1
  remote_user: user1

Now the same Ansible command connects as user1:

GATHERING FACTS ***************************************************************
<192.168.1.146> ESTABLISH CONNECTION FOR USER: user1

Also adding remote_user variable to ansible.cfg makes Ansible use the intended user instead of the logged-on one.

Are the ansible_user in inventory file and remote_user in playbook/ansible.cfg for different purposes?

What is the ansible_user used for? Or why doesn't Ansible observe the setting in the inventory?

Dozier answered 11/10, 2015 at 4:31 Comment(1)
Related: How to set a default ssh user for all hosts in Ansible?Countrybred
T
26

You're likely running into a common issue: the published ansible docs are for the development version (2.0 right now), and we don't keep the old ones around. It's a big point of contention... Assuming you're using something pre-2.0, the inventory var name you need is ansible_ssh_user. ansible_user works in 2.0 (as does ansible_ssh_user- it gets aliased in).

Tades answered 14/10, 2015 at 7:30 Comment(1)
Changing ansible_user to ansible_ssh_user worked for me with ansible 1.9.4.Bucko
F
7

I usually add my remote username in /etc/ansible/ansible.cfg as follows:

remote_user = MY_REMOTE_USERNAME

This way it is not required to configure ansible_user in the inventory file for each host entry.

Farris answered 16/5, 2017 at 16:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.