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
}
}
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