Skip to main content

DevOps Assistant: Ansible

· 3 min read

Have you ever worked with remote machines? In the past, IT teams would use SSH to access servers, enter Linux commands line by line, and rely on outdated shell scripts left by predecessors to execute tasks on each machine. This process was time-consuming and error-prone, especially when dealing with software updates or debugging shell scripts.

In modern software development, automation has become key to improving efficiency and reliability. Among many automation tools, Ansible stands out as an essential assistant for DevOps teams. This article introduces Ansible's core features, advantages, and how to integrate it into your daily workflow.

What is Ansible?

Ansible is an open-source automation tool designed to simplify IT infrastructure management and application deployment. Using a straightforward and intuitive YAML syntax, Ansible makes configuration easy to learn and implement.

Advantages

  1. Easy to Use: Ansible uses YAML for configuration, which is simple and structured, helping beginners quickly get started.

  2. Highly Extensible: With a rich library of modules, developers can also write custom modules for specific needs.

  3. Secure and Reliable: No extra ports or agent installations are needed, reducing potential security risks.

  4. Cross-Platform Support: Compatible with multiple operating systems (e.g., Linux, Windows), making it suitable for heterogeneous environments.

How to Use Ansible?

Ansible is written in Python, so you need to install Python on the control machine.

Installation command: pip install ansible

Basic Components

Inventory: Defines a list of managed nodes (servers) and supports grouping for detailed control.

Playbooks: The core files for automation, describing the steps to be executed.

Modules: Functional units that perform specific actions, like installing packages or modifying configurations.

Tasks: Individual operations defined in Playbooks.


Example: Deploying a Web Server

Below is a simple Playbook that demonstrates how to deploy an Apache web server using Ansible:

- name: Deploy Apache Web server
hosts: webservers
become: true
tasks:
- name: Install Apache
apt:
name: apache2
state: present

- name: Start and enable Apache service
service:
name: apache2
state: started
enabled: true

Here’s an example of an inventory/all.yml file. An inventory file can have multiple groups; this example has a single group all, with a single host. The Playbook's hosts parameter matches these hosts. You can use all or a specific host key in the Playbook.

all:
hosts:
host:
ansible_host: 0.0.0.0
ansible_port: 22
ansible_user: admin
ansible_ssh_private_key_file: ~/.ssh/id_rsa

Save the Playbook as deploy_apache.yml and run the following command:

ansible-playbook -i inventory deploy_apache.yml

The remote machine will install Apache2 and start the service!

An Essential Part of Playbook Targeting

Playbooks can target specific groups. Below is an inventory file with two groups: Finance and Marketing. Each group has two machines. When a Playbook specifies hosts: manager, it targets both managers in the inventory.

finance:
hosts:
manager:
ansible_host: 0.0.0.0
ansible_port: 22
ansible_user: laurence
ansible_ssh_private_key_file: ~/.ssh/id_rsa
laptop1:
ansible_host: 1.1.1.1
ansible_port: 22
ansible_user: joy
ansible_ssh_private_key_file: ~/.ssh/id_rsa
marketing:
hosts:
manager:
ansible_host: 0.0.0.0
ansible_port: 22
ansible_user: Kate
ansible_ssh_private_key_file: ~/.ssh/id_rsa
laptop1:
ansible_host: 1.1.1.1
ansible_port: 22
ansible_user: kevin
ansible_ssh_private_key_file: ~/.ssh/id_rsa

The advantage of this structure is the ability to manage multiple machines simultaneously, such as installing baseline environments. However, caution is necessary when operating on many devices.

Conclusion

Ansible’s simplicity and flexibility make it a star in the DevOps toolkit. Whether you're a beginner or an experienced engineer, it enables fully automated workflows, from deployment to management. If you haven't tried Ansible yet, start today and let it become your trusted assistant!