You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
}
}
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
The text was updated successfully, but these errors were encountered:
ghost
changed the title
ansible_host does not register ip_address/hosts under groups argument during first terraform apply
ansible_host does not register ip_address/hosts under groups argument during first terraform apply, empty inventory
Jan 18, 2024
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
hosts: webservers
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
The text was updated successfully, but these errors were encountered: