3
votes

I want to chain Terraform and Ansible using the local-exec provisioner;

However since this requires input to Ansible from Terraform I am stuck with the following complex command:

provisioner "local-exec" {
        command = 'sleep 60; ansible-playbook -i ../ansible/inventory/ ../ansible/playbooks/site.yml --extra-vars "rancher_server_rds_endpoint="${aws_db_instance.my-server-rds.endpoint}" rancher_server_elastic_ip="${aws_eip.my-server-eip.public_ip}""'
    }

which keeps returning

illegal char

error;

any suggestion about escaping correctly?

If the ansible-playbook command was to run directly in the shell it would be:

ansible-playbook -i inventory playbooks/site.yml --extra-vars "my_server_rds_endpoint=my-server-db.d30ikkj222.us-west-1.rds.amazonaws.com rancher_server_elastic_ip=88.148.17.236"

(paths differ)

1

1 Answers

4
votes

Terraform syntax states that:

Strings are in double-quotes.

So you need to replace single quotes with double ones, and then escape quotes inside, for example:

provisioner "local-exec" {
  command = "sleep 60; ansible-playbook -i ../ansible/inventory/ ../ansible/playbooks/site.yml --extra-vars \"rancher_server_rds_endpoint='${aws_db_instance.my-server-rds.endpoint}' rancher_server_elastic_ip='${aws_eip.my-server-eip.public_ip}'\""
}