0
votes

I have a terraform project that provisions a complex infrastrucure that comprehends a node that acts as a ansible control node.

Is there a way to trigger the execution of an ansible playbook on the remote control node, after that the apply command has finished executing?

I watched the Terraform talk where they talk about integrating ansible in the terraform flow but they use remote-exec provisioner to trigger the playbook in the remote machine itself, which is a useless use case in a serious prod environment.

I want to execute the playbook after the terraform apply for two reasons:

  • I want to make sure that the whole infrastructure has been completely deployed otherwise it could fail for obivious reasons. It looks like they didn't think about that, since the remote-exec gets executed as soon as the instance gets created.
  • since I'm inferring the ansible inventory from the terraform state I need the environment to be completely deployed (btw I'm inferring the state in a very hacky way)

Also I'd like some advice on how to load the ansible playbooks into the remote control node after the apply, in there a best practice? Thank you!

1

1 Answers

0
votes

May be you can output the data of the remote control node (like dns or public or private ip) to connect to that remote control node and use any script language like python to get the output and then remotely execute your ansible script on the control node.

import os
import json
stream = os.popen('terraform output')
output = json.load(stream.read())

And then parse the json output to get ssh connection to the remote controller.