<p>To be evaluated, a variable must be closed in <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#when-to-quote-variables-a-yaml-gotcha" rel="nofollow noreferrer">quoted double curly braces</a>, e.g.</p>
<pre class="lang-yaml prettyprint-override"><code>- hosts: &quot;{{ hostvars.localhost.aws_instance_host_group }}&quot;
</code></pre>
<p>The <a href="https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#referencing-key-value-dictionary-variables" rel="nofollow noreferrer">dot notation</a> simplifies the code.</p>
<hr>
<sup>
<p><strong><a href="https://stackoverflow.com/help/minimal-reproducible-example">Minimal reproducible example</a></strong></p>
<pre class="lang-yaml prettyprint-override"><code>shell&gt; cat pb.yml
---
- hosts: localhost
  gather_facts: false
  tasks:
    - set_fact:
        aws_instance_host_group: aws_instance
    - add_host:
        name: 10.1.0.61
        groups: &quot;{{ aws_instance_host_group }}&quot;

- hosts: &quot;{{ hostvars.localhost.aws_instance_host_group }}&quot;
  gather_facts: false
  tasks:
    - debug:
        var: groups
    - debug:
        var: inventory_hostname
</code></pre>
<pre class="lang-sh prettyprint-override"><code>shell&gt; cat hosts
localhost
</code></pre>
<pre class="lang-yaml prettyprint-override"><code>shell&gt; ansible-playbook -i hosts pb.yml 

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

TASK [set_fact] ******************************************************************************
ok: [localhost]

TASK [add_host] ******************************************************************************
changed: [localhost]

PLAY [aws_instance] **************************************************************************

TASK [debug] *********************************************************************************
ok: [10.1.0.61] =&gt; 
  groups:
    all:
    - 10.1.0.61
    - localhost
    aws_instance:
    - 10.1.0.61
    ungrouped:
    - localhost

TASK [debug] *********************************************************************************
ok: [10.1.0.61] =&gt; 
  inventory_hostname: 10.1.0.61

PLAY RECAP ***********************************************************************************
10.1.0.61: ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
localhost: ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
</code></pre>
</sup>

CC BY-SA 4.0
2022-05-08T13:46:04.897Z
2022-05-08T13:59:04.000Z
2022-05-08T13:59:04.000Z
Vladimir Botka
<p>The directory of my Ansible project has the following structure:</p>
<pre><code>project/
  playbook.yml
  host_vars/
    localhsot.yml
  roles/
    ...
    terraform-provision/
      ...
  terraform/
      ...
</code></pre>
<p>Here's <em>project/playbook.yml</em>:</p>
<pre><code>- hosts: localhost
  tasks:
  - name: trigger role
    include_role:
      name: terraform-provision
- hosts: hostvars['localhost'].aws_instance_host_group
  tasks:
  - name: debugging
    debug:
      var: hostvars['localhost'].aws_instance_host_group
</code></pre>
<p>Here's the relevant part of <em>project/host_vars/localhost.yml</em>:</p>
<pre><code>hostvars['localhost'].aws_instance_host_group
aws_instance_host_group: &quot;aws_instance&quot;
</code></pre>
<p>Here's the relevant part of <em>project/roles/terraform-provision/tasks/main.yml</em>:</p>
<pre><code>- name: Add the provisioned AWS instance to the inventory
  add_host:
    name: &quot;{{ aws_instance_public_ip.value }}&quot; # set using set_fact elsewhere in file
    ansible_user: &quot;{{ aws_instance_user_name }}&quot;
    groups: &quot;{{ aws_instance_host_group }}&quot;
</code></pre>
<p>When I execute <em>project/playbook.yml</em>, it throws the following error when trying to connect to the host created by the <em>terraform-provision</em> role:</p>
<pre><code>[WARNING]: Could not match supplied host pattern, ignoring: hostvars['localhost'].aws_instance_host_group
</code></pre>
<p>However, the debugging task of <em>project/playbook.yml</em> prints the group's name (aws_instance) successfully.</p>
<p>What am I missing? Is there a better approach to achieve the same thing?</p>

<ansible>
In Ansible, how do I assign a hostvar to a playbook's host?
Omar Shazly
# In Ansible, how do I assign a hostvar to a playbook's host?

(Asked by: username_0 on 2022-05-08)

The directory of my Ansible project has the following structure:
project/
  playbook.yml
  host_vars/
    localhsot.yml
  roles/
    ...
    terraform-provision/
      ...
  terraform/
      ...

Here's project/playbook.yml:
- hosts: localhost
  tasks:
  - name: trigger role
    include_role:
      name: terraform-provision
- hosts: hostvars['localhost'].aws_instance_host_group
  tasks:
  - name: debugging
    debug:
      var: hostvars['localhost'].aws_instance_host_group

Here's the relevant part of project/host_vars/localhost.yml:
hostvars['localhost'].aws_instance_host_group
aws_instance_host_group: "aws_instance"

Here's the relevant part of project/roles/terraform-provision/tasks/main.yml:
- name: Add the provisioned AWS instance to the inventory
  add_host:
    name: "{{ aws_instance_public_ip.value }}" # set using set_fact elsewhere in file
    ansible_user: "{{ aws_instance_user_name }}"
    groups: "{{ aws_instance_host_group }}"

When I execute project/playbook.yml, it throws the following error when trying to connect to the host created by the terraform-provision role:
[WARNING]: Could not match supplied host pattern, ignoring: hostvars['localhost'].aws_instance_host_group

However, the debugging task of project/playbook.yml prints the group's name (aws_instance) successfully.
What am I missing? Is there a better approach to achieve the same thing?

**Tags:** ansible


---

## 1 answers

### Answer by username_1 on 2022-05-08. Score: 2 | Accepted answer

To be evaluated, a variable must be closed in quoted double curly braces, e.g.
- hosts: "{{ hostvars.localhost.aws_instance_host_group }}"

The dot notation simplifies the code.


Minimal reproducible example
shell> cat pb.yml
---
- hosts: localhost
  gather_facts: false
  tasks:
    - set_fact:
        aws_instance_host_group: aws_instance
    - add_host:
        name: 10.1.0.61
        groups: "{{ aws_instance_host_group }}"

- hosts: "{{ hostvars.localhost.aws_instance_host_group }}"
  gather_facts: false
  tasks:
    - debug:
        var: groups
    - debug:
        var: inventory_hostname

shell> cat hosts
localhost

shell> ansible-playbook -i hosts pb.yml 

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

TASK [set_fact] ******************************************************************************
ok: [localhost]

TASK [add_host] ******************************************************************************
changed: [localhost]

PLAY [aws_instance] **************************************************************************

TASK [debug] *********************************************************************************
ok: [10.1.0.61] => 
  groups:
    all:
    - 10.1.0.61
    - localhost
    aws_instance:
    - 10.1.0.61
    ungrouped:
    - localhost

TASK [debug] *********************************************************************************
ok: [10.1.0.61] => 
  inventory_hostname: 10.1.0.61

PLAY RECAP ***********************************************************************************
10.1.0.61: ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
localhost: ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0


---

