Repository: ansible/ansible

Issue:
ansible_ssh_user is null in newer 2.0 versions
##### Issue Type:
- Bug Report
##### Ansible Version:

Issue was introduced in commit 226c7f4 and is present from there to HEAD (found via [bisecting](https://gist.github.com/alexwh/4399c41d39afdc58ce6c)).
##### Environment:

Running ansible on Arch Linux, managing a Debian Jessie box.
##### Summary:

When trying to reference the ansible_ssh_user variable, it returns null in newer versions of ansible (past the previously mentioned commit).

The [documentation](http://docs.ansible.com/ansible/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable) states that ansible_ssh_\* is deprecated, but doesn't seem to say it's removed. The alternative ansible_user returns "VARIABLE IS NOT DEFINED!", in  git HEAD (unless I'm referencing it wrong or something).
##### Steps To Reproduce:

```
ansible do -m debug -a "var=ansible_ssh_user"
```
##### Expected Results:

1.9 copy from Arch's repos:

```
do | success >> {
    "var": {
        "ansible_ssh_user": "alex"
    }
}
```

2.0 versions prior to 226c7f4:

```
do | SUCCESS => {
    "ansible_ssh_user": "alex", 
    "changed": false
}
```
##### Actual Results:

```
do | SUCCESS => {
    "ansible_ssh_user": null
}
```

From a [playbook](https://github.com/alexwh/ansible-playbooks/blob/8705ad4c252942e36d8fff260ef2a0d6cbded2bd/debian/roles/ssh/tasks/main.yml#L3)

```
$ ansible-playbook playbook.yml

PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [do]

TASK [ssh : add ssh keys to target] ********************************************
failed: [do] => (item=/home/alex/.ssh/id_ed25519.pub) => {"failed": true, "item": "/home/alex/.ssh/id_ed25519.pub", "msg": "Failed to lookup user None: 'getpwnam(): name not found: None'"}

PLAY RECAP *********************************************************************
do                         : ok=1    changed=0    unreachable=0    failed=1   
```



Discussion:
this was done on purpose, the only case in which it is 'null' is when it is not specified, before it would insert the 'current user executing ansible' but that created issues when relying on .ssh/config. 

If you set the remote user in any way, you will get a 'non null' response.

Are there any alternatives to this variable that act in a similar way? ansible_user_id seems to be okay but it behaves differently when using become (https://gist.github.com/ramondelafuente/9819526), or is this not the case anymore? ansible_user is always not defined for me, even when specifying ansible_ssh_user in hosts or using -u.

it should be defined in those cases

A simple tests shows it being populated

```
#>ansible -m debug -a "var=ansible_ssh_user" -u test_user nyx                                                                                                      
nyx | SUCCESS => {                                                                                                                                                                                                   
    "ansible_ssh_user": "test_user"                                                                                                                                                                                  
}    
```

And null when there is no user

```
#>ansible -m debug -a "var=ansible_ssh_user" nyx                                                                                                      
nyx | SUCCESS => {                                                                                                                                                                                                   
    "ansible_ssh_user": null                                                                                                                                                                                         
}  
```

ansible_ssh_user is okay, but ansible_user is undefined even when specifying -u. ansible_user is listed as the replacement for ansible_ssh_user in the documentation, which is why I was looking for a proper replacement (simply a variable that holds the current user on the system and is not affected by sudo/become).

```
$ ansible debvm -u aaaa -m debug -a "var=ansible_user"    
debvm | SUCCESS => {
    "ansible_user": "VARIABLE IS NOT DEFINED!"
}
```

check if #13683 fixes this for you

```
#>ansible -m debug -a 'var=ansible_user' nyx -u lalala
nyx | SUCCESS => {
    "ansible_user": "lalala"
}
```


Resolving patch:
diff --git a/lib/ansible/playbook/play_context.py b/lib/ansible/playbook/play_context.py
--- a/lib/ansible/playbook/play_context.py
+++ b/lib/ansible/playbook/play_context.py
@@ -125,6 +125,18 @@
     'remote_user',
 )
 
+RESET_VARS = (
+    'ansible_connection',
+    'ansible_ssh_host',
+    'ansible_ssh_pass',
+    'ansible_ssh_port',
+    'ansible_ssh_user',
+    'ansible_ssh_private_key_file',
+    'ansible_ssh_pipelining',
+    'ansible_user',
+    'ansible_host',
+    'ansible_port',
+)
 
 class PlayContext(Base):
 
@@ -505,7 +517,8 @@ def update_vars(self, variables):
 
         # TODO: should we be setting the more generic values here rather than
         #       the more specific _ssh_ ones?
-        for special_var in  ['ansible_connection', 'ansible_ssh_host', 'ansible_ssh_pass', 'ansible_ssh_port', 'ansible_ssh_user', 'ansible_ssh_private_key_file', 'ansible_ssh_pipelining']:
+        for special_var in RESET_VARS:
+
             if special_var not in variables:
                 for prop, varnames in MAGIC_VARIABLE_MAPPING.items():
                     if special_var in varnames:


Test patch: