ansible / terraform-provider-ansible

community terraform provider for ansible
https://registry.terraform.io/providers/ansible/ansible/latest
GNU General Public License v3.0
183 stars 42 forks source link

ansible_host does not register ip_address/hosts under groups argument during first terraform apply, empty inventory #75

Open ghost opened 6 months ago

ghost commented 6 months ago

I am trying to build a dynamic inventory using the tfstate file and run ansible playbook once my vsphere virtual machine is up. The issue is when I run terraform apply, the ansible playbook is not executed and the logs read => skipping: no hosts matched.

However, when I rerun the terraform apply, the ansible playbook is executed successfully

I tried adding a sleep resource between null_resource.inventory and null_resource.playbook, yet the inventory file did not register the hosts for the first time.

Versions used: ansible 2.16 and terraform 1.3.6

Code: resource "ansible_host" "webservers" { name = "vsphere_virtual_machine.vm.id groups = [webservers] variables = { ansible_host = vsphere_virtual_machine.main.default_ip_address ansible_user = user ansible_ssh_private_key_file = private_key } }

resource "null_resource" "inventory" { provisioner "local-exec" { command = ansible-inventory -i inventory.yml --graph --vars } }

resource "null_resource" "playbook" { provisioner "local-exec" { command = ansible-playbook -i inventory.yml playbook.yml } depends_on = [null_resource.inventory] }

inventory.yml plugin: cloud.terraform.terraform_provider

playbook.yml

inventory file when during first terraform apply ansible-inventory - inventory.yml --graph --vars @all |--@ungrouped

inventory file when during second terraform apply ansible-inventory - inventory.yml --graph --vars @all |--@ungrouped |--@webservers | |--ip address