Linux automation with Ansible

What is Ansible?⌗
Ansible is a tool that makes use of SSH and Python to run code on another machine - leaving no trace of the code that was run on the server. It allows your host machine (with Ansible installed) to connect to other machines (without Ansible installed) and runs your code on these. The only installation requirement on the remote nodes is Python. The remote nodes can be managed in so called inventories. These hold all your server specific information like host, ssh port and ssh password. (you can also use a ssh key to connect to remote machines with Ansible)
What are the benefits of using Ansible?⌗
-
Ansible can help you automate tasks, saving you time and effort.
-
Ansible can help you manage complex environments more easily, by providing a consistent way to provision and configure systems.
-
Ansible can help you ensure that your systems are configured consistently and correctly, by providing a way to test and validate your configurations.
-
Ansible can help you save money, by reducing the need for manual labor or custom scripts to automate tasks.
How can Ansible help to manage your infrastructure?⌗
Ansible can help to manage your infrastructure by providing a consistent way to provision and configure systems. Ansible can also help to ensure that your systems are configured consistently and correctly, by providing a way to test and validate your configurations.
What is idempotency?⌗
Idempotency is the property of an operation that results in the same output each time it is performed, regardless of how many times it is performed. Ansible is idempotent because it ensures that the desired state of a system is always achieved, regardless of how many times the Ansible playbook is run. This means that if a task is already completed, Ansible will not attempt to perform the task again.
How does Ansible do it’s magic?⌗
Ansible will generate Python code which it will copy to the remote node using ssh. The python code will gather information about the current state and will find a way to apply the desired state on the remote node. After everything is done, the Python code will be deleted leaving no trace of Ansible.
Isn’t that just some kind of glorified bash script?⌗
Bash scripts are typically used to automate tasks that are performed on a single system. Ansible can also be used to automate tasks on a single system, but it is designed to manage complex environments that span multiple systems. Additionally, Ansible provides a way to test and validate configurations, which is not possible with bash scripts.
What are some usecases?⌗
Imagine your organization manages a few servers. Maybe you have 10 Servers that are in a Kubernetes cluster. They all need some software to be pre-installed like Docker, kubelet and a few other things may be required. You could configure this once using ansible and apply that desired state to all the remote nodes you want. Now if you scale up your cluster - no need to worry because you can just apply your desired state to those machines as well!
Creating a Ansible project⌗
An Ansible project can be created using the ‘ansible-galaxy init’ command. This command will create a directory structure that includes a main.yml file and other supporting files.
Installation⌗
In order to use Ansible, you need to install it onto a host machine. This machine is the only one in need for an ansible installation. Prerequisite: You need to have Python 3.8 or newer installed.
Now use your package manager to download & install ansible:
Debian/Ubuntu
sudo apt install ansible
Arch based
sudo pacman -S ansible
You now have successfully installed ansible onto your so called “control-node”.
Directory layout⌗
This is an example that should give you a general overview of the Ansible project directory structure. It may look messy but theres order to the chaos! So lets go through each folder/element and group them.
General .gitignore (Used for version control) ansible.cfg (Contains information about your controller)
roles (Contains “roles” a server may accomodate in a desired state)
*.yml (These files are called playbooks and they allow you to actually run your ansible roles on nodes) hosts (Contains a list of all your remote nodes you want to manage. It also allows you to group them)
Vagrantfile (Used with Vagrant) .vagrant (Used with Vagrant)
What is Vagrant?⌗
Vagrant is a tool for managing virtual machines, and Ansible is a tool for provisioning and managing those machines. With Vagrant and Ansible, you can create and manage virtual machines entirely from the command line.
To create a VM with Vagrant, you will need to install Vagrant and a virtualization provider, such as VirtualBox. Once Vagrant is installed, you can use the ‘vagrant init’ command to initialize a Vagrantfile. The Vagrantfile will define the parameters for your virtual machine, such as the operating system, memory, and network settings.
Your first playbook⌗
An ansible playbook is a set of instructions for ansible, written in yaml, that tells ansible what to do.
Example:
- hosts: all
tasks:
- name: install apache
yum: name=httpd state=present
- name: start apache
service: name=httpd state=started
Creating a role⌗
Ansible roles are a way to reuse and share Ansible playbooks. Roles can be used to provision and configure systems, deploy software, and perform other tasks. Roles are typically designed to be used on multiple systems, and can be easily shared with other Ansible users. Roles can be stored in a variety of locations, but the most common is to store them in a directory called “roles” inside the Ansible project directory.
Ansible Galaxy⌗
Ansible Galaxy is a website that hosts a collection of Ansible roles that can be used to automate tasks. Galaxy roles can be downloaded and used without modification, or they can be used as a starting point for creating custom roles.
Jinja2 Templates⌗
Ansible can manage dynamic content by using the template module. The template module can be used to render Jinja2 templates, which can be used to generate output files.
Jinja2 is a template engine for Python. It provides a simple way to create templates that can be used to generate output files. Jinja2 templates can be used in Ansible playbooks to generate dynamic content.