Ansible is a powerful open-source tool for IT automation, widely used by system administrators, DevOps engineers, and IT teams. Ansible allows for the automation of repetitive tasks such as managing servers, applications, and networks. Its strength lies in its simplicity, flexibility, and agentless architecture, meaning no additional software needs to be installed on the target systems.
Ansible uses SSH (or WinRM for Windows) to manage target systems without requiring additional software installation. This reduces maintenance efforts and security risks.
Ansible uses YAML files to define configuration tasks. YAML is easy to read and enables even non-programmers to create Ansible Playbooks.
Ansible can efficiently manage small to large environments, from a handful of servers to thousands of systems, scaling seamlessly.
Ansible supports a wide range of platforms, including:
Thanks to Ansible Galaxy and a large community, there are already many ready-to-use roles and modules available.
The inventory is a list of target systems (hosts) that Ansible will manage. It can be stored in a simple text file, YAML, or a dynamic inventory like a cloud API. Example of a static inventory:
[webservers]
192.168.1.10
192.168.1.11
[dbservers]
192.168.1.20
A Playbook is a YAML file that describes which tasks should be performed on which hosts. Example:
---
- name: Install Apache Web Server
hosts: webservers
become: yes
tasks:
- name: Install Apache
apt:
name: apache2
state: present
Roles enable the modularization of Playbooks. Each role has a defined directory structure for variables, tasks, templates, and handlers.
Ansible modules are standalone units that perform specific tasks, such as installing a package or copying a file. Examples:
Variables allow flexibility and reusability in Playbooks. Example:
---
- name: Install software
hosts: all
vars:
software_name: nginx
tasks:
- name: Install {{ software_name }}
apt:
name: "{{ software_name }}"
state: present
Ansible can install and configure web servers like Apache or Nginx on multiple systems simultaneously.
---
- name: Install and configure Apache
hosts: webservers
become: yes
tasks:
- name: Install Apache
apt:
name: apache2
state: present
- name: Copy configuration file
copy:
src: /home/user/apache.conf
dest: /etc/apache2/sites-available/000-default.conf
- name: Restart Apache
service:
name: apache2
state: restarted
New users can be created centrally on multiple servers.
---
- name: Create users
hosts: all
become: yes
tasks:
- name: Add user
user:
name: "deploy"
state: present
shell: /bin/bash
Ansible can be used to perform security updates on all systems.
---
- name: Install security updates
hosts: all
become: yes
tasks:
- name: Apply security updates
apt:
upgrade: dist
state: latest
Ansible is an essential tool in the modern DevOps world. It integrates seamlessly into Continuous Integration/Continuous Deployment (CI/CD) pipelines, such as Jenkins or GitLab CI. This allows automated deployments, simplified rollbacks, and consistent environments.
Ansible Galaxy is a platform where pre-built roles and Playbooks can be shared. It’s a great starting point to save time and adopt best practices.
ansible-galaxy install geerlingguy.apache
Ansible is a powerful, flexible, and easy-to-learn tool for IT automation. It saves time, reduces errors, and ensures consistency in IT environments. Whether you’re managing a few servers or orchestrating thousands of systems, Ansible is the perfect solution.
If you have questions about Ansible or need help with implementation, feel free to reach out!