Ansible is an open source automation tool developed by Red Hat, primarily used by DevOps teams to manage servers.
Simply, it automates repetitive tasks, making your life as a developer much easier.
In this blog post series, we will explore how to use Ansible to set up your development development environment. We'll start with the basics of Ansible and then dive into automating the management of tools and configurations.
But why should you care about this? Imagine this scenario: you need to switch to a new computer or replicate your exact development environment across your work and personal machines. Starting from scratch every time can be time-consuming and error-prone. You might create a manual task list and execute each step whenever needed. This is where automation tools like Ansible shine. Ansible reduces manual effort and streamlines these processes.
Installation
Follow the steps provided in the official installation guide or use the following commands.
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible
Inventory
Inventory is a list of managed nodes or host.
Here's an example inventory.ini
file:
[all]
127.0.0.1 ansible_connection=local
In this file, [all]
is the host group name, and 127.0.0.1 ansible_connection=local
represents the local machine.
Variables
Variables are stored in files and can be used within Ansible playbooks to manage data dynamically during task execution.
For instance, you can have a default.config.yml
file with content like:
print_message: "Hello Ansible!"
Configuration
The configuration file allows you to set various parameters that govern how Ansible behaves when running tasks.
In an ansible.cfg
file, you can configure settings like this:
[defaults]
inventory = inventory.ini
Tasks
Tasks are the fundamental units of work defined in a playbook. They are the building blocks of Ansible automation.
Here's an example task defined in a tasks/echo.yml
file:
- name: Print Message
command: echo {{ print_message }}
register: echo_result # Capture the output
- name: Display the result
debug:
var: echo_result.stdout
This task will print print_message
value set in default.config.yml
and capture the output for display.
Playbooks
A playbook is a file that defines a series of tasks to be executed on targeted hosts. Playbooks are at the core of Ansible automation.
Here's an example playbook.yml
file:
- name: Configure host
hosts: all
vars_files:
- default.config.yml
tasks:
- import_tasks: tasks/echo.yml
To run this playbook, use the following command:
ansible-playbook playbook.yml