Reputation: 25
I'm trying to call the IP (ipv4_address) to execute Ansible. When I run plan/apply, I receive the error: "Invalid index: The given key does not identify an element in this collection value."
My configuration:
resource "vsphere_virtual_machine" "vm" {
resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
datastore_id = data.vsphere_datastore.datastore.id
network_interface { network_id = data.vsphere_network.network.id }
name= "terraform-test"
num_cpus = 2
memory = 4096
guest_id = "centos8_64Guest"
memory_hot_add_enabled = true
cpu_hot_add_enabled = true
cpu_hot_remove_enabled = true
disk {
label = "disk0"
size = 20
}
clone {
template_uuid = data.vsphere_virtual_machine.template.id
customize {
linux_options {
host_name = "terraform-test"
domain = "local"
time_zone = "Europe/Madrid"
}
network_interface {
ipv4_address = "10.20.30.25
ipv4_netmask = 22
dns_server_list = ["8.8.8.8"]
}
ipv4_gateway = "10.20.30.1"
}
}
provisioner "local-exec" {
command = "sleep 60; ansible-playbook configure.yml -i '${self.clone[4]}',"
}
}
My objective is to run the Playbook on the server you just created with the IP 10.20.30.25.
How do I get the IP of a vsphere_virtual_machine to run ansible-playbook?
Upvotes: 0
Views: 2215
Reputation: 21686
Consolidating @Helder Sapulveda's guidance from comments on the question to an answer for future reference:
provisioner "local-exec" {
command = "sleep 60; ansible-playbook configure.yml -i '${self.default_ip_address}',"
}
You can use the default_ip_address attribute of vsphere_virtual_machine to get the IP address for the ansible-playbook command.
Upvotes: 2