Does -u set ansible_ssh_user (Ansible 1.2)

Hi all,

I would like to have access to the remote user value when the -u option is specified at the command line of ansible-playbook. I am testing with the release of 1.2. When I use the following playbook test (ssh-test), I always get back my username (“jeremy”), regardless of what I set at -u.

You are using connection local in your playbook, so it is always running as you, and not using SSH.

Hi Michael,

Thank you for the prompt reply. Aside from using --extra-vars to explicitly set the variable, are there any other mechanisms I could use? Ideally I would like to use -u to set ansible_ssh_user (or some other variable).

Thank you for your help!
– Jeremy

It doesn’t seem to make sense in this case as you are running locally, and as I understand it, your modules will always be running locally. You could definitely set any variable you like with --extra-vars, but it seems like you don’t want to set ansible_ssh_user when you aren’t SSH’ing to anything, and that would just be confusing.

If you just want to print the user you are running ansible as (that seems like it might be the use case), you can do

{{ lookup(‘env’,‘USER’) }}

Hi Michael,

Ok, I understand what you are saying. I’ll fall back to using --extra-vars for this purpose.

Thank you.
– Jeremy

Ok, its a little silly, but I’ve done this before using 2 plays and the set fact module:

  • hosts: all
    sudo: true
    tasks:

  • name: store the ssh user name for next play
    set_fact: ssh_user={{ ansible_ssh_user}}

  • hosts: all
    sudo: false
    tasks:

  • local_action: echo {{ ssh_user }}