This is a project where the terraform script will provision the resources and execute the ansible script to configure the servers with the required configuration needed.
The terraform file is the usual script to provision the resources required but there is one change made to actually automate the process of provisioning and configuring the servers. Now before I show you the changes let us see why we need to automate this process. Well first normally we would execute the terrafrom file and ansible script separately to provision and configure the server respectively. Instead of us manually executing separatly we can automate the whole process by letting terrafrom execute the ansible script in the server while it provisions , this improves the workflow and efficiency of the process.
provisioner "local-exec" {
working_dir = "./ansible"
command = "ansible-playbook --inventory '${self.public_ip_address},' deploy-docker.yaml --private-key ${var.ssh_key_path} --user azureuser"
}
This block of code is inside the resource block of the virtual machine. This is responsible for executing a local file in the host machine. As you can see I told the script to go to the directory where the ansible scripts are present and then told the script to execute the ansible-playbook
command to run the playbook and configure the server. I could have used a null_resource from terraform to execute this separatly instead of it running inside the vm resource block.
As you can see it also configured the server with the help of ansible and in the last image you can find that docker has been successfully installed in the server
Thank you for reading this.