2

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!"
}
tariver
  • 41
  • 6
  • How and where exactly did you declare `ansible_ssh_user` or all other varialbles you tried ? – Zeitounator Mar 18 '21 at 09:39
  • As far as I could understand it's defined in config file. See [here](https://stackoverflow.com/a/24121223/10842470), for example. – tariver Mar 18 '21 at 09:44
  • Added P.P.S. with a test playbook to show that `{{ ansible_user }}` is indeed defined. – tariver Mar 18 '21 at 10:05

1 Answers1

2

Wrote this question to the Ansible mailing list and got an answer from developer. https://groups.google.com/g/ansible-project/c/fY5lAHCEHfA

Short summary - it's not supposed to work this way. In the future there will be a special component to query plugin settings.

Update 20.06.2023 - The following works now: ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q {{ ansible_ssh_user }}@10.0.0.1"'

Ansible version 2.12.10

tariver
  • 41
  • 6