terraform-aws-labs icon indicating copy to clipboard operation
terraform-aws-labs copied to clipboard

How do you automatically populate hosts file with ip of created nodes by terraform?

Open holms opened this issue 7 years ago • 1 comments

Doing this manually is very counter-productive every time. What are the variants to implement this?

holms avatar Nov 19 '17 20:11 holms

This can be achieved with the terraform "local_exec" plugin like this taking advantage of the .public_ip variable. The idea is to create a text file during provisionning (ie inventory) to be used later by ansible:

resource "null_resource" "ansible-provision" {

  depends_on = ["aws_instance.ec2-master"]

  ##Create Masters Inventory
  provisioner "local-exec" {
    command =  "echo \"[ec2-master]\" > ansible/inventories/iv1"
  }
  provisioner "local-exec" {
    command =  "echo \"\n${format("%s ansible_ssh_host=%s", aws_instance.ec2-master.tags.Name, aws_instance.ec2-master.public_ip)}\" >> ansible/inventories/iv&"
  }
}

jtbonhomme avatar Jan 15 '18 13:01 jtbonhomme