Infrastructure Automation with Ansible
Infrastructure automation has become an essential part of the modern DevOps lifecycle. Manually configuring servers, installing packages, deploying applications, and scaling resources consumes significant time. This approach is prone to human error and reduces process reproducibility. To solve these challenges, Ansible — a powerful automation tool — is used.
Ansible is an open-source tool for automating system administration tasks. It is written in Python and requires no agents to be installed on managed hosts. Configurations are described using YAML format, and SSH protocol is used to connect to servers. Thanks to its simple and readable syntax, Ansible has gained widespread adoption among major companies, including NASA, Twitter, and Red Hat.
Ansible Fundamentals
Definition and How It Works
Ansible functions as a tool for configuration automation, server management, process orchestration, and application deployment. Its architecture is built on Python, and YAML is used as a domain-specific language (DSL) to describe tasks.
Key Advantages
Ansible's agentless architecture ensures easy deployment and reduces maintenance overhead. Using SSH protocol to connect to remote hosts guarantees data transmission security. YAML syntax makes configuration files human-readable and easy to maintain.
Extensibility through Python modules allows creating custom solutions for specific tasks. Support for both Windows and Linux operating systems ensures versatility in heterogeneous environments.
Installation Process
Install Ansible using the pip package manager:
pip install ansible
An alternative method using the system package manager:
sudo apt install ansible
System Architecture and Components
Core Ansible Components
| Component | Purpose | Description |
|---|---|---|
| Inventory | List of managed hosts | Contains information about servers and their grouping |
| Playbook | Set of tasks in a YAML file | Describes the sequence of operations to execute |
| Module | Individual operation | Performs specific actions: copying files, installing packages |
| Role | Reusable logical structure | Groups related tasks into logical blocks |
| Variable | Variables and templates | Enables parameterization of configurations |
| Plugin | Behavior extension | Adds functionality through callback and connection plugins |
Inventory Structure
An inventory is a file describing managed hosts and their grouping:
[web]
web1 ansible_host=192.168.1.10
web2 ansible_host=192.168.1.11
[database]
db1 ansible_host=192.168.1.20
db2 ansible_host=192.168.1.21
The inventory can contain variables for groups and individual hosts. This allows you to customize Ansible's behavior for different servers without modifying the main playbook files.
Creating and Structuring Playbooks
Playbook Basics
A playbook is a YAML-formatted script that describes tasks and their execution order. Each playbook consists of one or more plays, which define actions for specific groups of hosts.
The basic structure of a playbook includes the following elements:
- name: Install