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

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

Open
holms opened this issue Nov 19, 2017 · 1 comment

Comments

@holms
Copy link

holms commented Nov 19, 2017

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

@jtbonhomme
Copy link

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&"
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants