Repository: ansible/ansible

Issue:
host_vars for new hosts via add_host are not loaded anymore
Currently we are using `add_host`, when we automatically build templates and it uses the variables provided in a `host_vars` file. This works up to 1.7.2, but in 1.8.x the variables from the `host_vars` file are gone.

This is what 1.7.2 delivers:

```
$ ansible-playbook --version; ansible-playbook -i inventory test.yml
ansible-playbook 1.7.2

PLAY [localhost] **************************************************************

TASK: [add_host name=test groups=test] ****************************************
ok: [localhost]

TASK: [debug var=hostvars['test']] ********************************************
ok: [localhost] => {
    "hostvars['test']": {
        "group_names": [
            "test"
        ],
        "inventory_hostname": "test",
        "inventory_hostname_short": "test",
        "test_group_vars": "group test value",
        "test_host_vars": "host test value"
    }
}

PLAY RECAP ********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0
```

In 1.8.x the variables defined in `host_vars` are gone:

```
$ ansible-playbook --version; ansible-playbook -i inventory test.yml
ansible-playbook 1.8.4
  configured module search path = None

PLAY [localhost] **************************************************************

TASK: [add_host name=test groups=test] ****************************************
ok: [localhost]

TASK: [debug var=hostvars['test']] ********************************************
ok: [localhost] => {
    "hostvars['test']": {
        "group_names": [
            "test"
        ],
        "inventory_hostname": "test",
        "inventory_hostname_short": "test",
        "test_group_vars": "group test value"
    }
}

PLAY RECAP ********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0
```

This is the example used to show the problem:

```
$ tree
.
├── inventory
│   ├── group_vars
│   │   └── test.yml
│   ├── host_vars
│   │   └── test.yml
│   └── hosts
└── test.yml
```

``` yaml
# inventory/group_vars/test.yml:

---
test_group_vars: group test value
```

``` yaml
# inventory/host_vars/test.yml:

---
test_host_vars: host test value
```

``` ini
# inventory/hosts:
localhost
```

``` yaml
# test.yml:

---
- hosts: localhost
  gather_facts: False
  tasks:
    - add_host: name=test
                groups=test
    - debug: var=hostvars['test']
```



Discussion:
have you tested against current development?

bisect found the issue was introduced at a2cfe8770b9a93ed2a1372396fa7a1e648bd658e

@srvg could you take a look? you probably know this code better than i do


Resolving patch:
diff --git a/lib/ansible/runner/action_plugins/add_host.py b/lib/ansible/runner/action_plugins/add_host.py
--- a/lib/ansible/runner/action_plugins/add_host.py
+++ b/lib/ansible/runner/action_plugins/add_host.py
@@ -55,7 +55,7 @@ def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **
         if ":" in new_name:
             new_name, new_port = new_name.split(":")
             args['ansible_ssh_port'] = new_port
-        
+
         # redefine inventory and get group "all"
         inventory = self.runner.inventory
         allgroup = inventory.get_group('all')
@@ -72,10 +72,10 @@ def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **
         # Add any variables to the new_host
         for k in args.keys():
             if not k in [ 'name', 'hostname', 'groupname', 'groups' ]:
-                new_host.set_variable(k, args[k]) 
-                
-        
-        groupnames = args.get('groupname', args.get('groups', args.get('group', ''))) 
+                new_host.set_variable(k, args[k])
+
+
+        groupnames = args.get('groupname', args.get('groups', args.get('group', '')))
         # add it to the group if that was specified
         if groupnames:
             for group_name in groupnames.split(","):
@@ -95,13 +95,17 @@ def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **
 
                 vv("added host to group via add_host module: %s" % group_name)
             result['new_groups'] = groupnames.split(",")
-            
+
+
+        # actually load host vars
+        new_host.vars = inventory.get_host_variables(new_name, update_cached=True, vault_password=inventory._vault_password)
+
         result['new_host'] = new_name
 
         # clear pattern caching completely since it's unpredictable what
         # patterns may have referenced the group
         inventory.clear_pattern_cache()
-        
+
         return ReturnData(conn=conn, comm_ok=True, result=result)
 
 


Test patch: