Ansible is an Infrastructure-as-Code tool that can automate almost any task on a Linux server. Learn how to use it manage your cloud computing resources like a pro.
- What is Ansible? - Ansible basics tutorial - What is Ansible used for? - How do I automate cloud servers? - Best Linux automation tools - Ansible vs Terraform
AI Generated·May contain errors
Ansible, an open-source automation tool that can describe your IT infrastructure with simple declarative code.
It was created by Michael DeHaan in 2012, then acquired by Red Hat a few years later,
and today is one of the most popular configuration management tools.
Most importantly, it uses declarative push-based YAML code that allows DevOps teams to easily automate their Linux servers,
increasing efficiency while reducing the likelihood of human error.
It works by making at least one machine a control node.
It then connects to multiple manage nodes and sends them an Ansible module over SSH,
which can configure that machine's dependencies, update network settings, provision databases,
or really any job that you might need to do more than once.
Developers write playbooks that contain a series of jobs called plays.
Each play is a set of instructions that can be executed on one or more target hosts.
It will execute each task in order against all machines in parallel.
In addition, a playbook is item-potent, which means it won't actually make any changes unless it has to.
An excellent real-world use case for Ansible is to manage your Linux servers on a platform like Linode.
We start by provisioning a server running Ubuntu as the control node, then shh, into it from the terminal.
From there, we can install Ansible on this machine.
This machine needs permission to create and modify other servers,
so we'll get an access token for the Linode API and store it somewhere securely.
Now, what's awesome about Ansible is that it has a massive ecosystem of pre-built playbooks that can be accessed on Ansible Galaxy.
Install the collection for Linode using the ansible-galaxy command.
From there, it's a good practice to use Ansible Vault to store sensitive information, like a password, securely,
so it's not required to be directly in a YAML file.
And we'll want to encrypt our personal access token here.
And now we're ready to write our first playbook.
Create a file in the development directory ending in .yml, then create the first play by giving it a name, like create-linode-instance.
Then the host property is a pattern that defines one or more machines that will run this play,
or they can be listed in a dedicated inventory file.
Next, we can define reusable variables or point to files that contain multiple variables.
And then we can create a task list that contains instructions that actually do something.
Here, we're using a fully qualified collection name of linode.cloud.instance,
which is a special namespace that tells Linode to create a new server instance.
From there, we describe the configuration details, like our API token, region, disk image, and so on.
And now all we have to do is run the ansible playbook command to create a new server automatically.
And that's how you automate infrastructure with code using Ansible.
Shoutout to Linode, cloud computing from Akamai, for sponsoring this awesome tool.
Thanks for watching, and I will see you in the next one.