Terraform + Ansible

Hi team,

I’m using terraform to provision some servers and using ansible to deploy software on those servers as:

resource "null_resource" "ansible-deploy" {

  provisioner "local-exec" {
    command = <<EOT
    ansible-playbook -i inventory/hosts.ini playbook.yaml
    EOT
  }
}

So far my playbook is working, but anytime I perform a change on my ansible playbook terraform did not “catch” it as a new change.

How can I do to keep track of the ansible changes on my terraform plan?

hi, its not supposed to catch changes in arbitrary things, it is tracking changes at “terraform resource” level.
in your case the “null_resource” named “ansible-deploy”.

terraform can be made to “re-run” the “null_resource” by adding a “trigger” section.
you can configure a trigger using the checksum (say “sha256sum” of the file “playbook.yaml”)

So, anytime the file “playbook.yaml” changes, and you run “terraform apply”, the trigger will “detect” a change, and the “null_resource” will be re-created (execute scripts in your case)

HTH,
Shantanu

1 Like

Thank you for the reply!

Works like a charm on the playbook file.
Also I need it to use it on a roles directory, but seems like any function regarding files/crypto (https://www.terraform.io/docs/configuration/functions.html) does not work on directories .

I will appreciate any light.

What comes to mind is a function like stat which could indicate the last modified time of the directory. As far I could search, there isn’t such a function, though some quick googling revealed:

… which in turn links to:

Not sure, but if you could verify the checksums of all the files inside a directory, I think it would achieve the same result.

Maybe you could file a feature request with the Terraform core for a “stat” equivalent function?!? :slight_smile:

Silly me, just realized something as I finished clicking “Reply”. Could you not write a simple shell script which returns the stat output of the directory in question?

Maybe with: https://www.terraform.io/docs/providers/external/data_source.html

That’s all that is coming to mind with the little coffee that I have had so far!