Ansible Basics in 3 Minutes
Ansible helps you to configure the desired state of a large number of servers or other remote hosts. It is helpful for installing packages or other types of tools you want. At my current employer, we use Ansible to configure the state of the servers that run our Kafka brokers, zookeepers, and the proxy server. FULL DISCLAIMER: Most of the information in this guide was sourced from Wes Higbee’s PluralSight course “Getting Started Ansible.” I definitely recommend you watch the full course for a deep dive, but to get a shortened synopsis, continue reading this!
One of the key features of Ansible is that it allows you to focus on deploying a “desired” state and allows you not to have to worry about “how” you got to that state. Most Ansible modules are idempotent which allows you to safely re-run modules. This is convenient because instead of having to write code to handle specific situations like in a bash script or something similar, you can just deploy the desired state you want.
Note: Ansible has some great documentation and some very handy modules already built for you.
Downloading Ansible
Make sure you have python installed. One thing to note is that Mac comes pre-installed with Python 2.7 but you should have Python 3.7 since it is the most up to date.
brew install python
Next update your pip (Python’s package manager) to make sure you have the latest version. And then install Ansible.
pip install -U pip pip install ansible
If you’re having a difficult time specifying which python version you want to use to install ansible with, you can specify the version like this example.
python3 -m pip install ansible
To make sure you’ve downloaded Ansible run the following command.
ansible --version
Here are some other helpful commands to get you started:
This command lists out additional commands that you can use. ansible-doc --help
Ansible Ad-hoc command
You usually don’t run Ansible commands on the fly because the whole point of using Ansible is to be able to deploy the same configs across multiple servers. However, for learning purposes and for quick commands, you can use the Ansible ad-hoc command. Ansible is primarily used to manage a network of nodes or machines but for simplicity’s sake we will use Ansible to configure our localhost or our dev environment. Let’s install the bat tool. It’s just like cat but a nicer format. To check out bat, just type
bat <some-file-you-want-to-see>
But before you do that. Install it with the command below.
ansible -m <module-name> -a <arguments> <managed-node-or-target-host> ansible -m homebrew -a "name=bat state=latest" localhost
Playbooks
Playbooks are collections of Ansible commands but written out into a Yaml file so you can have them in source control. Take ad-hoc Ansible commands that you are frequently running and turn them into a playbook or a combination of commands that you want executed. The Yaml files are fairly self explanatory.
// playbook.yml file
- name: Ensure homebrew packages are installed.
hosts: localhost
tasks:
- homebrew:
name: bat
state: latest
To run this playbook and install these tools onto your local machine, use the following command:
ansible-playbook playbook.yml
Inventory
Ansible inventory is a folder for your that you would add if you have multiple servers/hosts to configure. But that will be a topic for another article!
But What About Terraform?
For many people, myself included, I was confused why we needed something like Ansible when Terraform already exists. So taking a deeper dive I discovered that although there are many similarities, the two can work together. Terraform handles all of your “provisioning infrastructure” written out in terraform files. This data defines all things related to your VM. This would be things like your database networks, the size of the disk, how much memory it holds, what region it lives, the IP address, etc. Terraform does not typically managed the applications and its configurations though. With Ansible, you define what tools or specific applications you want on your servers. These could be things like Docker, VSCode, Vim, etc. In summary, Terraform provisions the VM, Ansible tells the VM what applications and tools to install. And Docker installs your container.
As always, if you have any questions feel free to leave a comment!