Ansible

Automatizacion IT sin agentes - configurar servidores, desplegar apps, orquestar workflows con playbooks YAML

TL;DR

Qué: Herramienta de automatización IT sin agentes para gestión de configuración y despliegue.

Por qué: No necesita agentes, sintaxis YAML simple, operaciones idempotentes, enorme biblioteca de módulos.

Quick Start

Instalar:

# macOS
brew install ansible

# Linux (pip)
pip install ansible

# Verificar
ansible --version

Primer comando (ad-hoc):

# Ping localhost
ansible localhost -m ping

# Ejecutar comando en hosts remotos
ansible all -i "server1,server2," -m shell -a "uptime"

Archivo de inventario (hosts.ini):

[webservers]
web1.example.com
web2.example.com

[databases]
db1.example.com

Cheatsheet

ComandoDescripción
ansible all -m pingPing a todos los hosts
ansible-playbook play.ymlEjecutar playbook
ansible-playbook -i hosts play.ymlCon inventario
ansible-galaxy install roleInstalar rol
ansible-vault encrypt fileCifrar archivo
ansible-doc -lListar módulos

Gotchas

Basic playbook

# playbook.yml
---
- name: Configure web servers
  hosts: webservers
  become: yes

  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: present
        update_cache: yes

    - name: Start nginx
      service:
        name: nginx
        state: started
        enabled: yes

    - name: Copy config
      template:
        src: nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      notify: Restart nginx

  handlers:
    - name: Restart nginx
      service:
        name: nginx
        state: restarted

Variables

# En el playbook
vars:
  http_port: 80
  max_clients: 200

# En archivo vars
# vars/main.yml
app_name: myapp
app_version: "1.0.0"

# Usar variables
- name: Create directory
  file:
    path: "/opt/{{ app_name }}"
    state: directory

Common modules

# Operaciones de archivos
- file: path=/tmp/test state=directory mode=0755
- copy: src=file.txt dest=/tmp/file.txt
- template: src=config.j2 dest=/etc/app/config

# Gestión de paquetes
- apt: name=nginx state=present
- yum: name=httpd state=latest

# Gestión de servicios
- service: name=nginx state=started enabled=yes

# Comandos
- command: /usr/bin/make
- shell: echo $HOME > /tmp/home.txt

Roles structure

roles/
  webserver/
    tasks/main.yml
    handlers/main.yml
    templates/
    files/
    vars/main.yml
    defaults/main.yml

Next Steps