0
votes

I am trying to print , private ip of the newly created instances into a host file locally where i run terraform from, to use it for ansible,but terraform is throwing errors, each time I modify my escape characters in the local-exec echo command.

here is the snippet where i am getting error:

 provisioner "local-exec"
{
command = " echo \"[servers]\["${aws_instance.seeds.*.private_ip}"\]\" >> /ec2-ansible/hosts"
}

expected output in the hosts file:

[servers]
ip1
ip2
ip3

errors I am getting:

Failed to load root config module: Error parsing /root/ec2-ansible/main.tf: At 33:1: expected: IDENT | STRING | ASSIGN | LBRACE got: RBRACE
Failed to load root config module: Error parsing /root/ec2-ansible/main.tf: At 29:25: illegal char

Please help me out.

1
After [servers] you have a backslash escape on the [ but not one on the quote mark that follows. - Martin Atkins

1 Answers

0
votes

I think you go in the wrong direction for using both terraform and ansible.

In ansible, you can use Dynamic Inventory to get the host information such as private IPs easily from their tags, size, ami id, etc.

In terraform, if you need the state details and feed to ansible, recommend using terraform output to collect the information.