Repository: ansible/ansible

Issue:
 delegated tasks do not use delegated host's ansible_ssh_user variable #8224 
The very same issue reappeared in ansible > 1.9
for some reason when I delegate task to an existing host , it provides no username 
but only a key which results in

---

root@client0:/vagrant/test# ansible --version
ansible 1.9.0.1

root@client0:/vagrant/test# ansible-playbook -vvv -i inventory/vagrant/swift_dynamic_inventory.py swift-create-and-distribute-rings.yml 

PLAY [add storage devices to ring builders] **********************************\* 

TASK: [swift-storage-add-devices-to-rings | add object devices to rings] *****\* 
<192.168.50.2> ESTABLISH CONNECTION FOR USER: root
<192.168.50.2> REMOTE_MODULE swift_ring_builder op=add type=object dev=sdb region=1 ip=192.168.50.3 weight=100 zone=1
<192.168.50.2> EXEC ssh -C -tt -v -o ControlMaster=auto -o ControlPersist=60s -o ControlPath="/home/vagrant/.ansible/cp/ansible-ssh-%h-%p-%r" -o StrictHostKeyChecking=no -o IdentityFile="/vagrant/test/insecure_private_key" -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o ConnectTimeout=10 192.168.50.2 /bin/sh -c 'mkdir -p /tmp/ansible-tmp-1429015129.77-112957892190368 && chmod a+rx /tmp/ansible-tmp-1429015129.77-112957892190368 && echo /tmp/ansible-tmp-1429015129.77-112957892190368'
fatal: [storage0 -> swift-ring-builder] => SSH Error: Permission denied (publickey,password).
    while connecting to 192.168.50.2:22
It is sometimes useful to re-run the command using -vvvv, which prints SSH debug output to help diagnose the issue.
## FATAL: all hosts have already failed -- aborting

as you see  in ssh no username is provided for swift-ring-builder even though
root@client0:/vagrant/test# inventory/vagrant/swift_dynamic_inventory.py --host swift-ring-builder | python -mjson.tool
{
    "ansible_ssh_host": "192.168.50.2",
    "ansible_ssh_private_key_file": "/vagrant/test/insecure_private_key",
    "ansible_ssh_user": "vagrant"
}



Discussion:
https://github.com/ansible/ansible/commit/61a30e5f49c14319c43f9321631a7c3f6f8b6554

this commit lead to a bug please undo

there was a bad indent left with the else: clause, which can be causing this

try the PR i created


Resolving patch:
diff --git a/lib/ansible/runner/__init__.py b/lib/ansible/runner/__init__.py
--- a/lib/ansible/runner/__init__.py
+++ b/lib/ansible/runner/__init__.py
@@ -394,11 +394,11 @@ def _compute_delegate_user(self, host, inject):
                 if inject['hostvars'][host].get('ansible_ssh_user'):
                     # user for delegate host in inventory
                     thisuser = inject['hostvars'][host].get('ansible_ssh_user')
-                else:
-                    # look up the variables for the host directly from inventory
-                    host_vars = self.inventory.get_variables(host, vault_password=self.vault_pass)
-                    if 'ansible_ssh_user' in host_vars:
-                        thisuser = host_vars['ansible_ssh_user']
+            else:
+                # look up the variables for the host directly from inventory
+                host_vars = self.inventory.get_variables(host, vault_password=self.vault_pass)
+                if 'ansible_ssh_user' in host_vars:
+                    thisuser = host_vars['ansible_ssh_user']
         except errors.AnsibleError, e:
             # the hostname was not found in the inventory, so
             # we just ignore this and try the next method


Test patch: