Merge "Use username from node information if available" into feature/zuulv3
diff --git a/tests/base.py b/tests/base.py
index 176c535..036515d 100755
--- a/tests/base.py
+++ b/tests/base.py
@@ -1713,6 +1713,8 @@
                     image_id=None,
                     host_keys=["fake-key1", "fake-key2"],
                     executor='fake-nodepool')
+        if 'fakeuser' in node_type:
+            data['username'] = 'fakeuser'
         data = json.dumps(data).encode('utf8')
         path = self.client.create(path, data,
                                   makepath=True,
diff --git a/tests/fixtures/config/inventory/git/common-config/playbooks/hostvars-inventory.yaml b/tests/fixtures/config/inventory/git/common-config/playbooks/hostvars-inventory.yaml
new file mode 100644
index 0000000..f679dce
--- /dev/null
+++ b/tests/fixtures/config/inventory/git/common-config/playbooks/hostvars-inventory.yaml
@@ -0,0 +1,2 @@
+- hosts: all
+  tasks: []
diff --git a/tests/fixtures/config/inventory/git/common-config/zuul.yaml b/tests/fixtures/config/inventory/git/common-config/zuul.yaml
index 900abd6..74ddf2d 100644
--- a/tests/fixtures/config/inventory/git/common-config/zuul.yaml
+++ b/tests/fixtures/config/inventory/git/common-config/zuul.yaml
@@ -31,6 +31,14 @@
           - compute1
           - compute2
 
+- nodeset:
+    name: nodeset2
+    nodes:
+      - name: default
+        label: default-label
+      - name: fakeuser
+        label: fakeuser-label
+
 - job:
     name: base
     parent: null
@@ -47,3 +55,8 @@
     name: group-inventory
     nodeset: nodeset1
     run: playbooks/group-inventory.yaml
+
+- job:
+    name: hostvars-inventory
+    run: playbooks/hostvars-inventory.yaml
+    nodeset: nodeset2
diff --git a/tests/fixtures/config/inventory/git/org_project/.zuul.yaml b/tests/fixtures/config/inventory/git/org_project/.zuul.yaml
index 26310a0..1a8bf5d 100644
--- a/tests/fixtures/config/inventory/git/org_project/.zuul.yaml
+++ b/tests/fixtures/config/inventory/git/org_project/.zuul.yaml
@@ -4,3 +4,4 @@
       jobs:
         - single-inventory
         - group-inventory
+        - hostvars-inventory
diff --git a/tests/unit/test_inventory.py b/tests/unit/test_inventory.py
index 2835d30..04dcb05 100644
--- a/tests/unit/test_inventory.py
+++ b/tests/unit/test_inventory.py
@@ -80,3 +80,24 @@
 
         self.executor_server.release()
         self.waitUntilSettled()
+
+    def test_hostvars_inventory(self):
+
+        inventory = self._get_build_inventory('hostvars-inventory')
+
+        all_nodes = ('default', 'fakeuser')
+        self.assertIn('all', inventory)
+        self.assertIn('hosts', inventory['all'])
+        self.assertIn('vars', inventory['all'])
+        for node_name in all_nodes:
+            self.assertIn(node_name, inventory['all']['hosts'])
+            # check if the nodes use the correct username
+            if node_name == 'fakeuser':
+                username = 'fakeuser'
+            else:
+                username = 'zuul'
+            self.assertEqual(
+                inventory['all']['hosts'][node_name]['ansible_user'], username)
+
+        self.executor_server.release()
+        self.waitUntilSettled()
diff --git a/zuul/executor/server.py b/zuul/executor/server.py
index 469d6f3..79fa91e 100644
--- a/zuul/executor/server.py
+++ b/zuul/executor/server.py
@@ -920,6 +920,10 @@
                     private_ipv4=node.get('private_ipv4'),
                     public_ipv6=node.get('public_ipv6')))
 
+            username = node.get('username')
+            if username:
+                host_vars['ansible_user'] = username
+
             host_keys = []
             for key in node.get('host_keys'):
                 if port != 22:
diff --git a/zuul/model.py b/zuul/model.py
index 5d8d271..081d165 100644
--- a/zuul/model.py
+++ b/zuul/model.py
@@ -388,6 +388,7 @@
         self.az = None
         self.provider = None
         self.region = None
+        self.username = None
 
     @property
     def state(self):