# Using \`remote\_user\` in \`ansible\_ssh\_common\_args\` in inventory file

**URL:** <https://forum.ansible.com/t/using-remote-user-in-ansible-ssh-common-args-in-inventory-file/34948>\
**Category:** Ansible Project\
**Created:** [April 9, 2021, 6:45am UTC](https://forum.ansible.com/t/using-remote-user-in-ansible-ssh-common-args-in-inventory-file/34948 "2021-04-09T06:45:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tariver\_16](https://avatars.discourse-cdn.com/v4/letter/t/3d9bf3/32.png) [@tariver\_16](https://forum.ansible.com/u/tariver_16)\
**Post date:** [April 9, 2021, 6:45am UTC](https://forum.ansible.com/t/using-remote-user-in-ansible-ssh-common-args-in-inventory-file/34948/1 "2021-04-09T06:45:05Z")

</div>

Greetings!  
Asked this [on stackoverflow](https://stackoverflow.com/questions/66687962/using-remote-user-in-ansible-ssh-common-args-in-inventory-file) but received no answer, so trying my luck here.

In my network there is one server (server2) that can’t be accessed directly. First you have to ssh to server1 and from there to server2.

My config file has the following line:

remote\_user = foo.bar

My inventory:

hosts:  
server1:  
ansible\_host: 10.0.0.1  
server2:  
ansible\_host: 10.0.0.2 ansible\_ssh\_common\_args: ‘-o ProxyCommand=“ssh -W %h:%p -q [foo.bar@10.0.0.1](mailto:foo.bar@10.0.0.1)”’

This works fine, but I don’t want the user name to be hardcoded. [Here](https://stackoverflow.com/a/49468833/10842470) and [here](https://stackoverflow.com/a/57395833) there are examples that remote\_user can be passed via {{ ansible\_ssh\_user }} but I can’t get it to work.

What I tried:

ansible\_ssh\_common\_args: ‘-o ProxyCommand=“ssh -W %h:%p -q {{ ansible\_ssh\_user }}@10.0.0.1”’

Also tried {{ ansible\_user }} and {{ user }}. But when I run ansible server2 -m ping I get an error:

“The field ‘ssh\_common\_args’ has an invalid value, which includes an undefined variable. The error was: ‘ansible\_user’ is undefined”

Is it possible to use user defined by remote\_user in ansible\_ssh\_common\_args?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 9, 2021, 4:03pm UTC](https://forum.ansible.com/t/using-remote-user-in-ansible-ssh-common-args-in-inventory-file/34948/2 "2021-04-09T16:03:59Z")

</div>

Only if you set ansible\_ssh\_user previously, the connection  
information won't be 'recursive' to itself.

That said, in future you could use this lookup to get the 'resolved'  
remote\_user for the connection plugin  
[https://github.com/ansible/ansible/pull/74186](https://github.com/ansible/ansible/pull/74186)

---

<div class="post-metadata">

**Author:** ![tariver\_16](https://avatars.discourse-cdn.com/v4/letter/t/3d9bf3/32.png) [@tariver\_16](https://forum.ansible.com/u/tariver_16)\
**Post date:** [April 9, 2021, 4:21pm UTC](https://forum.ansible.com/t/using-remote-user-in-ansible-ssh-common-args-in-inventory-file/34948/3 "2021-04-09T16:21:31Z")

</div>

Thank you for your answer.

Yes it works if I set ansible\_ssh\_user as variable in the inventory file, but for my purpose it’s the same as hardcoding it. What I want to do is to share this with my colleagues, so they can also use it without making any changes.

You can use this global variable in the playbook. As an example:

- name: Playbook to test default user  
hosts: all  
gather\_facts: false  
tasks:
- name: Print default users  
debug:  
var: ansible\_user

When I run it with ansible-playbook test-default-user.yml --limit server1 I get

ok: [server1] =\> {  
“ansible\_user”: “foo.bar”  
}

Also works with ansible\_ssh\_user. So I thought it’s the same with the inventory.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 12, 2021, 4:33pm UTC](https://forum.ansible.com/t/using-remote-user-in-ansible-ssh-common-args-in-inventory-file/34948/4 "2021-04-12T16:33:01Z")

</div>

It 'works' in certain cases and debug is a bad example of that, once  
the PR I linked is merged we will probably deprecate any updates to  
ansible\_\* vars as they are currently inconsistent and many times wrong  
(especially when you consider loops).

---

<div class="post-metadata">

**Author:** ![tariver\_16](https://avatars.discourse-cdn.com/v4/letter/t/3d9bf3/32.png) [@tariver\_16](https://forum.ansible.com/u/tariver_16)\
**Post date:** [April 13, 2021, 6:09am UTC](https://forum.ansible.com/t/using-remote-user-in-ansible-ssh-common-args-in-inventory-file/34948/5 "2021-04-13T06:09:52Z")

</div>

Thanks for your answer. I’ll wait for the new version then and rewrite my inventory according to it.
