Ansible - Technological watch
What kind of tool is Ansible ?
Ansible is a software release in 2012. Developed in python, Ansible is used to do tasks like configuration management, application deployment, intraservice orchestration, and provisioning. It is an open-source tool and it is very easy to use.
Imagine you have to configure a new linux machine as a web server and want to configure it: You want to create a new user, install nginx, configure it, copy your application into your server…
Easy right ? It should only take less than one hour !
And now, imagine you have to go this for one hundred of machines !
To make it this easy, we can write simple tasks with the YAML syntax called “playbook”. Ansible will read the given tasks in these playbooks, and run them into a server list to make the modifications.
Idempotence
Ansible has an idempotent philosophy;
What is idempotent ? Simply it is: “No matter how many times you call the operation, the result will be the same”. In other words, the final state is the same if you do an action 1 time or 10 times.
For example, you want to install a package: the first time you want to install a package, you want the installation happening. But the second time, the package is already installed, so we don’t care to install it again.
With Ansible, we do not code, we actually write our desired final state.
We can imagine having a job pipeline running every day to assure the state didn’t change.
But why can’t we simply write a bash script ? It would do the same ?
Actually not. Let’s take an example:
Imagine you have written a bash script which does 3 non-idempotent operations:
To test it, you would run it, but it crashes in the middle of the script.
- Operation 1: OK
- Operation 2: KO
- Operation 3: Not triggered
Here, we are now in a middle state, and we can’t re-run the script !
As the operations are not idempotent, if you run the script again, the « operation 1 » would be re-executed !
If the operations are idempotent, we can re-run the script, No matter how many times you call the operation, the result will remain the same !
Ansible Architecture
Modules
Modules are kind of functions proposed by the Ansible developers and the community.
Ansible is open source and has a big community. Most of the time, you will simply use pre-created roles.
In Ansible, you it exists hundred of modules in Ansible developed by the developers community.
Let’s take a simple example with the copy
module.
This represents a simple task. The code is very easy to read !
Note: You can create your own roles to re-use code.
You can find more information here: https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_reuse_roles.html#id2
Inventories
An inventory represents the list of the servers we want to apply the modifications. The syntax is very simple. First, we have under parentheses the name of the group, and then we have the IPs of the servers you want to target.
Playbooks
Finally we have the most interesting past, the playbooks ! A playbook is a sequence of tasks we want to apply on our inventory.
Here, we launch two tasks on our targets. First we display a debug message, then we copy a simple file hello.txt
. Easy to understand right ?
And finally, to run the playbook, you can simply run:
Or you can run with the --check
flag to run in dry-run and check everything will be fine.
Ansible comes with plugins, which you can fetch environment variables, run tasks as sudo users, and much more!
And of course, we can make more complex operations like making conditions or loops, but take care because Ansible isn’t a programming language.
Ansible Vault
Sometimes, we want to securely store sensitive information, such as passwords or secret keys, in encrypted files. This ensures that confidential data remains protected, especially when sharing or version-controlling Ansible playbooks. The encrypted files can be seamlessly integrated into playbooks, and Ansible will handle the decryption process during execution.
Here’s a brief example of how to use Ansible Vault:
First, create an encrypted file. Ansible Vault will prompt you to enter a password. After entering the password, you can add sensitive information to the file. For example:
You can edit the secrets later with this command:
Finally, we can use the encrypted variables we defined like this:
When running the playbook, Ansible will prompt you for the Vault password before execution. This ensures that only authorized users can access the sensitive information stored in the encrypted file.
Ansible Tower
Ansible Tower is an API, a web service and a web console designed to make it easier for use by computer teams. It is a central console for managing automation tasks. Tower is a commercial product supported by Red Hat, Inc.
There is a free version, serving as a beta version for its big sister Tower, called AWX. This version is also usable by companies, but is not guaranteed.
My opinion
I really love Ansible ! Ansible offers a simple way to automate tasks on servers.
I personally use it to generate and save my server backups, deploy SSH keys, create a specific configuration environment, and much more !
Of course, Ansible is not a mandatory, and you can do the same than Ansible does with a bash script. But Ansible offers idempotence and a simple syntax. 😌
Atomic design - Technological watch
Learn what is the atomic design in less than 5 minutes !
Redis - Technological watch
Learn what is Redis in less than 5 minutes !
Svelte - Technological watch
Learn what is Svelte in less than 5 minutes !
Jenkins - Technological watch
Learn what is Jenkins in less than 5 minutes !
PicoCSS - Technological watch
Learn what is PicoCSS in less than 5 minutes !
OpenAPI / Swagger - Technological watch
Learn what is OpenAPI / Swagger in less than 5 minutes !
HashiCorp Vault - Technological watch
Learn what is HashiCorp Vault in less than 5 minutes !
RabbitMQ - Technological watch
Learn what is RabbitMQ in less than 5 minutes !