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?