Hi everyone. Today, let's talk about Terraform versus Ansible. Now in some of our videos past
we've talked about Terraform in detail, Ansible, and even Infrastructure as Code, but I think a lot
of you might be wondering when to use Terraform and when to use Ansible. And in my view these
are actually two very complementary tools. So I believe you can use them together when you're
doing kind of your infrastructure automation. Now I think the first thing people think about
when they think about infrastructure automation is actually the step of provisioning, right. So
how do we actually get something up and running so that we can actually start you know doing our
application development and that kind of thing. That's one thing Terraform is really good at. And
one of the reasons it's so good at it is because Terraform is declarative in nature. And this
is actually one of the key differences between Terraform and Ansible, but we'll get to that in
a minute. So let's see what a kind of a Terraform configuration would look like. So we want to
provision some resources let's say on a public cloud, we're going to do so in a declarative
way. So we'll start by saying maybe we want a database service. So we'll say "Create DB" and
we'll have some configuration to go with it like you know what kind of class it is and what size
that kind of thing. Maybe we also want to create a Kubernetes Cluster and maybe even some VMs
in a VPC. So we'll first create the VPC and maybe some more configuration to create the VMs. So
we'll kind of have our Terraform Config there. Now I mentioned it's declarative and this is awesome
because you know in these steps over here we've created some DB services and some other things. So
let's say we want to actually bind that database service to our Kubernetes Cluster, so in in this
step right here we'll say "Bind to Kubernetes". Now in a procedural language everything would
go top to bottom. So it would start with the DB and realize hey I can't bind it to something that
doesn't exist, but since Terraform is declarative it'll actually say okay we can't do this one yet
let's create the Kubernetes Cluster first, also create the VPC Cluster kind of simultaneously
and then once that's ready then we'll create the database because it's dependent on the Kubernetes
Cluster to bind to. This is essentially what's called an implicit dependency. You can also
do explicit dependencies where you can just tell a resource to wait until another one is
up and ready to go. So that's one of the key advantages of Terraform is that it takes kind
of a declarative approach. So we've talked about Terraform, let's kind of switch gears
here a little bit and talk about what Ansible is really good at. And now Ansible's main kind of
purpose in my opinion is configuration management, this includes things like application deployment,
but it can actually also do infrastructure provisioning. Now one of the key things I'll
say about Ansible is that it's not necessarily declarative, but it supports declarative
resources, and it's not necessarily procedural either it's kind of, let's call it a hybrid.
Now in this sense, I basically mean that you know Ansible supports much like Terraform kind
of configuration files to do your automation. So for example, configuration management task
can be something like first you would start up a VM, maybe it was stopped, maybe you need to
restart it, or stop it, or something like that. And some configuration so just telling it you
know just to start up. Then maybe you install some application into the VM and some drivers,
maybe you need to install the runtimes and so you kind of have some configuration steps,
maybe you utilize the open source community where they you know build modules and collections of
these things that make you allow you to get started quickly. And then maybe finally you
start the actual application. So these are all config management tasks, application deployment,
you're working with resources that are already created. Now the reason I say it's hybrid is
because although the resources are written kind of in a declarative fashion they're actually executed
top to bottom. So this is an Ansible playbook and Ansible would first start with the VM creation
and then installing the app and then starting the app. So if you had these in the wrong order things
wouldn't work correctly, so that's my view that's why I think you know Ansible is declarative and
procedural in a sense, it's kind of a hybrid of both depending on how your modules are written.
So we've got our infrastructure, we've got our kind of configuration, now let's talk about you
know going back to Terraform what it looks like to actually start executing this configuration.
So first thing we'll do is you know generally you want to do something called a Terraform Plan
which will enable you to see what will actually come up before you actually execute the real API
calls. We'll do a Terraform Plan and you know it'll spin up some of this,
a view of what's going to actually get created. So the database, cluster, the VPC, and when it all
looks good you'll do a Terraform Apply that'll use your API key and run the actual API calls
against your cloud provider, you know using your API key and spin up all of that infrastructure
and this is what Terraform is really good at. And this brings me to my third thing about Terraform,
is that it's very much managed on state for life cycle management. So one of the really
cool things here is for example, you know, once you've created this infrastructure
if you go back and, you know, remove an entire piece of config for example to create VPC block,
if you remove that and you run a Terraform Apply again, it'll actually remember from last time,
it'll say "hey last time you had this, now you've removed it" and it'll go ahead and
remove that VPC for you. Now the difference with Ansible is that there is no kind of life cycle
management. Now essentially what this means is that if you wanted to delete
some step of this flow you can't just remove it you would need to kind of mark it for deletion,
so you would need to kind of do a remove app step. Now that's that doesn't go to say
that you couldn't necessarily create an Ansible playbook that automatically recognizes when you
remove some configuration and run the steps. The point is with Terraform you get it automatically,
and with Ansible you kind of have to implement that life cycle management into your actual code.
Another key point I want to make here is item potency, so that's essentially the ability to
run something multiple times and if nothing has changed it won't actually run anything
run any new actions. So with Terraform you get that automatically, so you can run this a thousand
times and the first time it'll create everything and the remaining 999 times it would actually just
say "hey you already have it, nothing's changed" and it wouldn't do anything. Now with Ansible
you could technically create item potent modules, but it's not a requirement so that's something
that you as a developer have to consider when using Ansible is, you know, see the community
modules you're using, the cloud provider modules and collections you're using, and make sure
they're idempotent and if they're not you know you might have to do some additional work to get
them going. Now I think I've made a case for both capabilities here but I want to talk a little bit
about the similarities between the two as well. For one they both support heavy templating.
So this is great. So you can have kind of one source of truth and then different configuration
files. So for example, if you wanted a dev, and a test, and a prod environment, you could have
one source of truth and then kind of just switch out the variables that you're using to spin up
the different environments. So that's something neat that both environments support. Number two,
they're both open source as I mentioned, but the key thing I want to mention is that, you know,
they've got a big kind of community as well. So for Terraform they have the Terraform Registry
with modules being developed, providers to work with the cloud providers, and and so they've got
this rich community to help, you get started very quickly. And Ansible's the same, they've
got Ansible Galaxy with collections of modules so you can get started very quickly with existing
playbooks and that kind of thing. So they're similar in that sense that they've got this big
kind of community. And the last thing I'll say is agentless which is kind of more relevant in the
Ansible context than Terraform since they're it's doing configuration management. You don't have to
install an agent on a VM somewhere, it's both of these tools are kind of plug and play, easy to get
started whether on your local machine or somewhere in the cloud, these tools are both very similar in
that they're easy and quick to get started with. Now you might be wondering, you know, in the
beginning I said these tools are complementary, so how would you use Ansible and Terraform together.
Well, there's a couple of approaches. You could start with Terraform. Now as I mentioned Terraform
is really good at provisioning. So you could have a Terraform configuration that spins up some
infrastructure and then once that infrastructure is ready you can actually use Terraform to call
Ansible to do configuration management on the resources you spun up. So starting the VM,
and installing an app, and that kind of thing. You can also take the other approach and start with
Ansible, and in the first step of your Ansible playbook call Terraform to spin up infrastructure
and then pick up where Terraform leaves off, take that infrastructure which is, you know,
in some inventory, an Ansible inventory, and run the Ansible actions on that.
So in essence Ansible and Terraform can work really well together because Terraform is
very good at infrastructure provisioning but falls short where Ansible comes in
with configuration management. And I think together, you know, both open source world-class
infrastructure automation tools, I think together you can really create a complete infrastructure
automation story. I want to thank you for joining me for this video today. If you want to see
what IBM Cloud is doing in the infrastructure automation space be sure to check out IBM Cloud
Schematics. If you have any questions or you like the video, be sure to drop a comment below and a
like. Be sure to subscribe and stay tuned for more videos like this in the future. Thank you.