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"'
This works fine, but I don't want the user name to be hardcoded. Here and here 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
?
P.S. ansible server1 -m ping
works fine.
P.P.S. Wrote a playbook for test purposes:
- name: Playbook to test default user
hosts: all
gather_facts: false
tasks:
- name: Print default users
debug:
var: ansible_user
When I run ansible-playbook test-default-user.yml --limit server1
I get
ok: [server1] => {
"ansible_user": "foo.bar"
}
Also works with ansible_ssh_user
, but for just {{ user }}
I get
ok: [server1] => {
"user": "VARIABLE IS NOT DEFINED!"
}