Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ghost opened this issue Jan 18, 2024 · 0 comments
Labels
type/enhancement New feature or request

Comments

@ghost
Copy link

ghost commented 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

  • name: Run Playbook
    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

@ghost 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
@gravesm gravesm added the type/enhancement New feature or request label Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant