Introduction
Did you hear the one about âcattle versus petsâ? You know, the idea that infrastructure is now immutable and short-lived. Yes, we chuckled about reality, too.
This is an ideal state and one weâd recommend trying to get to. Itâs also the driver behind Terraform. However, with almost every customer we talk to, we find that reality is quite different. Theyâve moved to infrastructure-as-code, but are still required to maintain servers as we have always done. We define the infrastructure as code, in Terraform, but the resulting virtual machines still need configuration changing, and often maintaining.
Enter Ansible. Weâre sure youâve at least read about, and are probably already very familiar with. So we thought weâd show you how easy it is to bring Ansible into play, along with Terraform, right in the env0 interface, to give the best of all worlds. Deployment, configuration and even maintenance as code, all in the same source control and all glued neatly together in env0.
So stop thinking Terraform vs Ansible and start thinking - better together!
The Setup
For the example here, weâll keep it simple. The beauty of both Terraform and Ansible is extending a small start into something more detailed is easy. Weâre going to deploy an ec2 instance on AWS, using Terraform, then weâll hand over to Ansible using a Custom Flow.
Weâre sure youâve already spotted the great thing about this flow is you can run it again and again, and all that will happen is the Ansible play will update the existing host. Coupling up the environment with a redeploy on git push will fully automate any later Ansible changes too, neat!
The key to this custom flow is an env0.yml file. Hereâs our example:
Before we even run terraform plan, we do a little forward planning and get the name of the SSH key. Since Ansible is going to connect to our new machine via SSH, weâll use the ec2 key already on our account.
Next, weâll install Ansible using pip[1]. Although it is available via apk (the deployment image is based on Alpine) itâs an older version. We opted to put the Ansible install after terraform apply because it gives the ec2 instance time to start up whilst weâre running pip. If you âlook at the Ansible play, youâll notice we also take the approach of checking for SSH connectivity before doing anything too. This is an additional safety net to ensure our whole flow works.
Before we run the Ansible play, we create an inventory file with the host name for our ec2 instance. If we were starting multiple machines, weâd populate the inventory with all the hosts to connect to.
The last step is to run the play. Our SSH key is automatically picked up from $ENV0_ENV, so we just point to the environment variable here.
Et voilĂ ! Our host is up and running, fully configured.
Conclusion
This is a simple example, but the fundamentals would stay the âsame for something more complex. Key to it all is the env0.yml file for the custom flow. If youâd like to see a version of this blog as a video instead, you can view it here:
If youâre managing an infrastructure using Terraform and Ansible today, weâd love to hear about your experiences. Drop us a message on Twitter!
Footnotes
[1] You can make your own custom deployment image when using the self-hosted agent.
Introduction
Did you hear the one about âcattle versus petsâ? You know, the idea that infrastructure is now immutable and short-lived. Yes, we chuckled about reality, too.
This is an ideal state and one weâd recommend trying to get to. Itâs also the driver behind Terraform. However, with almost every customer we talk to, we find that reality is quite different. Theyâve moved to infrastructure-as-code, but are still required to maintain servers as we have always done. We define the infrastructure as code, in Terraform, but the resulting virtual machines still need configuration changing, and often maintaining.
Enter Ansible. Weâre sure youâve at least read about, and are probably already very familiar with. So we thought weâd show you how easy it is to bring Ansible into play, along with Terraform, right in the env0 interface, to give the best of all worlds. Deployment, configuration and even maintenance as code, all in the same source control and all glued neatly together in env0.
So stop thinking Terraform vs Ansible and start thinking - better together!
The Setup
For the example here, weâll keep it simple. The beauty of both Terraform and Ansible is extending a small start into something more detailed is easy. Weâre going to deploy an ec2 instance on AWS, using Terraform, then weâll hand over to Ansible using a Custom Flow.
Weâre sure youâve already spotted the great thing about this flow is you can run it again and again, and all that will happen is the Ansible play will update the existing host. Coupling up the environment with a redeploy on git push will fully automate any later Ansible changes too, neat!
The key to this custom flow is an env0.yml file. Hereâs our example:
Before we even run terraform plan, we do a little forward planning and get the name of the SSH key. Since Ansible is going to connect to our new machine via SSH, weâll use the ec2 key already on our account.
Next, weâll install Ansible using pip[1]. Although it is available via apk (the deployment image is based on Alpine) itâs an older version. We opted to put the Ansible install after terraform apply because it gives the ec2 instance time to start up whilst weâre running pip. If you âlook at the Ansible play, youâll notice we also take the approach of checking for SSH connectivity before doing anything too. This is an additional safety net to ensure our whole flow works.
Before we run the Ansible play, we create an inventory file with the host name for our ec2 instance. If we were starting multiple machines, weâd populate the inventory with all the hosts to connect to.
The last step is to run the play. Our SSH key is automatically picked up from $ENV0_ENV, so we just point to the environment variable here.
Et voilĂ ! Our host is up and running, fully configured.
Conclusion
This is a simple example, but the fundamentals would stay the âsame for something more complex. Key to it all is the env0.yml file for the custom flow. If youâd like to see a version of this blog as a video instead, you can view it here:
If youâre managing an infrastructure using Terraform and Ansible today, weâd love to hear about your experiences. Drop us a message on Twitter!
Footnotes
[1] You can make your own custom deployment image when using the self-hosted agent.