Search code examples
azureansibleterraformdevopsinfrastructure-as-code

How to connect Terraform and Ansible together?


I have written a Terraform script to spin-up an infrastructure on Azure. I have also written an Ansible script to patch the VMs launched on Azure with latest updates. But when I am not able to automate the process of patching the VMs once they get launched.


Solution

  • You can use Provisioners in Terraform to execute Ansible Playbooks on Provisioned VM. I'm not sure about your terraform Version. But below code might help. Keep in mind Provisioners are to be used as last resort

      provisioner "local-exec" {
        command = "ansible-playbook -u user -i '${self.public_ip},' --private-key ${var.ssh_key_private} provision.yml" 
      }
    

    https://www.terraform.io/docs/language/resources/provisioners/syntax.html