如何 运行 ansible playbook in kmv created with terraform

How to run ansible playbook in kmv created with terraform

在我用 terraform 创建了一个虚拟机之后,我想 运行 一个 ansible 剧本,它在同一个 .tf 文件中使用 kvm 提供程序创建的虚拟机中安装一个 lamp 服务器,但我没有在我 运行 : virsh net-dhcp-leases default 之前知道新 vm 的 ip。 我想在 tf 文件的末尾添加这样的代码,它在创建的同一虚拟机中执行剧本:

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

谢谢

最简单的方法: 在 KVM 中创建 VM(如果是 dhcp)后,IP 地址会在一段时间后给出,所以我建议 运行 一切都在 shell 脚本中。

在 TF 脚本中添加输出模块(获取 IP)

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

shell 脚本:

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