Ansible on Linux: Automate Server Configuration Without Agent
Ansible is an open-source configuration automation tool that allows you to manage Linux servers remotely without installing any agent on hosts. It uses SSH for communication and YAML to describe the desired state of your infrastructure.
Why Use Ansible?
- Agent-free: uses SSH, which you already have configured
- Readable YAML: anyone understands what the playbook does
- Idempotent: run the same playbook 10 times, the result is always the same
- Scales: 1 command to configure 1 or 1000 servers simultaneously
Installation
# Ubuntu/Debian
sudo apt update && sudo apt install ansible -y
# Verificar versão
ansible --version
Configuring the Inventory
The inventory is the file that lists the servers that Ansible will manage. By default it's in /etc/ansible/hosts, but you can create a local file:
# ./hosts
[webservers]
192.168.1.10
192.168.1.11
[dbservers]
db1.exemplo.com
db2.exemplo.com
[todos:children]
webservers
dbservers
Ad-Hoc Commands
Before writing a playbook, you can run one-off commands on all hosts:
# Testa conectividade com todos os hosts
ansible all -i hosts -m ping
# Verifica o uptime de todos os webservers
ansible webservers -i hosts -m command -a "uptime"
# Instala nginx em todos os webservers (com sudo)
ansible webservers -i hosts -m apt -a "name=nginx state=present" -b
The -b (become) parameter runs with sudo. The -i hosts parameter points to the inventory file.
First Playbook
A playbook describes the desired state of your servers in YAML:
# instalar-nginx.yml
---
- name: Instalar e iniciar nginx
hosts: webservers
become: yes
tasks:
- name: Atualizar cache apt
apt:
update_cache: yes
- name: Instalar nginx
apt:
name: nginx
state: present
- name: Garantir que nginx está rodando e sobe no boot
service:
name: nginx
state: started
enabled: yes
# Rodar o playbook
ansible-playbook -i hosts instalar-nginx.yml
# Dry run: mostra o que faria sem aplicar
ansible-playbook -i hosts instalar-nginx.yml --check
Most Used Modules
apt/yum— package installation and removalservice— start, stop, enable servicescopy/template— copy files and Jinja2 templates to hostsuser— create and remove system usersfile— create directories and set permissionscommand/shell— execute arbitrary commandsgit— clone and update repositories
Variables and Templates
# group_vars/webservers.yml
app_port: 8080
app_name: minha-api
- name: Criar arquivo de configuração
template:
src: templates/app.conf.j2
dest: /etc/app/app.conf
owner: www-data
mode: '0644'
Templates use Jinja2 syntax: {{ app_port }}, {% if condicao %}, etc.
Organizing with Roles
For larger projects, the practice is to organize playbooks into roles:
ansible-galaxy init nginx-role
This creates the standard structure: tasks/, handlers/, templates/, vars/, defaults/. Each role is reusable across projects.
Ansible in the Job Market
Ansible is among the top 5 most in-demand tools in DevOps and SRE job postings. Mastering basic playbooks already sets a sysadmin apart from someone who only manages servers manually.
For certification: Red Hat offers the RHCE (Red Hat Certified Engineer), which since version 8 is 100% based on Ansible — proving you can automate with it is worth more than ever.
That was just one piece. The course teaches the whole path — from your first command to exam day.