Adil Saidi
Adil Saidi

Reputation: 341

How to run ansible playbook in kmv created with terraform

After I created a vm with terraform, I want to run a ansible playbook which installs a lamp server in the vm created with kvm provider in the same .tf file, but I dont know the ip of the new vm until I run : virsh net-dhcp-leases default. I want to add in the end of the tf file a code like this one that executes playbook in the same vm created:

provisioner “local-exec” {
 command = “ansible-playbook -u ubuntu -i ‘variable that specify the ip of created vm’ main.yml”
 }

thank you

Upvotes: 0

Views: 529

Answers (1)

krasnosvar
krasnosvar

Reputation: 119

Easiest way: IP-adresses are given some time after creating the VM in KVM( if dhcp), so i recommend running everything in a shell script.

in TF-script add output module( fetch IPs)

output "ips" {
  # show IP, run 'terraform refresh' if not populated
  value = libvirt_domain.domain-ubuntu.*.network_interface.0.addresses
}

shell script:

terraform apply
#sleep to wait for IPs(if dhcp)
sleep 30
#fetch IPs
terraform refresh | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' >> hosts
ansible-playbook -u ubuntu -i hosts

Upvotes: 1

Related Questions