We have decided that automating repetitive task is important in having consistent delivery of services. Configuring our newly provisioned servers is one of such tasks. However there are not much human-readable configuration solutions out there.
In simple terms, Ansible is an open source configuration tool for configuring servers after deployment. It completes automation tasks with a blue print known as "ansible-playbook". Ansible playbook is a "yml" file(e.g ansible-main.yml") that can execute tasks on specified machines. The tasks are split into a separate folder named roles with sub-folders that has the role name as title and "main.yml" file. It also has an inventory.txt file that has the address of each server that the script will run.
This is what a typical ansible directory looks like:
We can run an ansible playbook with the command:
ansible-playbook main.yml
This is the result of running a simple ansible playbookon on a local machine.
The code can be found on github here:.
Ansible is declarative such that ansible tasks describes the state of the computer and ensure the tasks end up in the state that was specified. However, It allows us run tasks in a specific order making it imperative. Safe to say it has a mixture of both.
During setup, ansible runner gathers facts about the target node, so it knows the current state of files(everything in Unix is file :)) so that while it runs, it knows whether a specific file exists on the computer and if it is already in the final state. This makes ansible idempotent.
Ansible execute tasks using its modules.
Package management:
Ansible has modules for different package managers to install, remove and upgrade packages on the target node modules. Some of the modules are apt, yum, dnf, and zypper. The modules name are similar to their corresponding generic name.
For example, we can use the apt module to install nodejs and npm on our machine.
Here we used the apt package to install the latest version of nodejs and npm. The runner will need admin privileges on the target node to perform the operations. The syntax “become: yes” helps us use an elevated privilege to perform such tasks.
Other popular modules on ansible are:
- Copy: The copy module copies a file from the local or remote machine to a location on the target remote machine.
- Shell: It is used to execute shell commands in nodes.
- File: It is used to set attributes of files in a module.
- git: Deploy software (or files) from git checkouts Here is a list of core ansible modules.
Ansible has all these cool features and can be used to configure more than just servers, It can configure any machine with ssh interface or a callable API such as load-balancers and other networking tools. Personally, i like to include my ansible-playbook as a job in a Circleci pipeline to configure the infrastructures i have just deployed. Doing this will ensure i get consistent configurations in as much servers as i want.
I found this well written article Ansible Tutorial for Beginners: Ultimate Playbook & Examples. You should definitely give it a read for more in-depth examples on Ansible.
The idea is to automate as much as we can, till we can boast of a perfect deployment pipeline. Happy Learning!
Top comments (0)