I started hearing and seeing Ansible everywhere during my last job search. Ansible kept coming up in interviews and in job descriptions. Problem was at the time, I had little experience with it. So I could only talk about it from a high level.
I needed to learn more about Ansible and WHY SysAdmins are so keen on knowing this tool.
Initial research or should I say a few google searches said Ansible makes your IT job a lot easier.
but how?
Well, Ansible can save SysAdmins time by automating repetative tasks, like:
Installing and configuring packages / software
System Administration tasks like patching or uprgrades
Monitoring
Ansible is also idempotent.
(An operation is idempotent if the result of performing it once is exactly the same as the result of performing it repeatedly without any intervening actions.)
Idempotency is especially beneficial in large organizations with multiple servers being managed.
Ansible is also agentless. Meaning Ansible can manage nodes without being installed on those servers. Ansible will bring those machines to the desired state with just a few lines of code.
“With SSH and Ansible I can send commands to 500 servers without having even used the servers before.”
Mark Maas - Linux System Admin, Binck Bank
Ansible is very powerful and has a large community behind it. There are so many benefits to getting started with Ansible. Agentless Architecture - Ansible PDF
Getting Started
The node with ansible installed on it is called the control node. The control node manages the other devices remotely using SSH. Ansible requires the control node to use a Linux operating system.
Step 1: Installation
Ansible has great documentation and for this step please refer to this link to install Ansible in your environment. —> Installing Ansible — Ansible Documentation
Ansible can be installed using Python’s pip module:
pip install ansible
Step 2: Configuration
Ansible’s config file lives in /etc/ansible/ansible.cfg
The config file is pretty straightforward and divded into sections. Here is a short snippet of the defaults section.
[defaults]
inventory = /etc/ansible/hosts
log_path = /var/log/ansible.log
# ssh timeout
[inventory]
enable_plugins = host_lists, virtualbox, yaml
[privilege_escalation]
Step 3: Inventory or Hosts
Ansible uses a list of machines or hosts to manage your infrastructure. The inventory can be dynamic or static depending on your needs. The most basic version of an inventory is an INI file. Here is an example taken from the Ansible Documentation:
mail.example.com
[webservers]
foo.example.com
bar.example.com
[dbservers]
one.example.com
two.example.com
three.example.com
There are 6 servers in that inventory. FQDN or IP Addresses are used to identify hosts. Hosts can be grouped using [group_name]
.
Step 4: Playbooks or Ad-Hoc
Generally Ansible “plays” or tasks are carried out from a playbook. The playbook holds the blueprint of what tasks need to be deployed.
The playbooks are written in YAML format. If you’re not familiar with YAML, Ansbile offers documentation on YAML Syntax .
Here’s an example playbook that will unarchive a file and move it to a location on all the hosts .
# extract archive and remove file afterwards
---
- name: extract archive and remove file
hosts: all
tasks:
- name: extract
unarchive:
src: /root/data.tar.gz
dest: /srv
remote_src: yes
- name: remove archive
file:
path: /root/data.tar.gz
state: absent
To execute the playbook use this command:
ansible-playbook /path/to/playbook.yml -i /path/to/inventory-file
Ansible can also use Ad-Hoc commands to carry out tasks outside of the playbook:
To ping all hosts using an ad-hoc command:
ansible -m ping -i /path/to/inventory_file all
What’s next?
If this has sparked your interest to learn more about Ansible here are some resources:
KodeKloud offers two ansible courses. I’ve taken them and they include lab environments to practice with.
Opensource.com has MANY blog posts on Ansible. Here’s a few I found interesting
LinkedIn Learning offers an entire DevOps track that includes a section on Ansible
How I use Ansible
I have been using Ansible in my lab environment. I configured a control node on my XCP-ng server. That I use to deploy and configure my virtual machines.
My next projects are developing playbooks for every machine in my environment.