Tabla de Contenidos
Ansible
Ansible es una de las pocas posmodernidades que no resultaron ser una operacion psicológica para que le compres el soporte a Red Hat. Esta cosa es genuinamente útil.
Sirve para automatizar las cosas que estás hasta los huevos de hacer, como instalar nginx por nonillonésima vez o algo mas sencillo como actualizar un archivo de configuración en múltiples nodos.
Instalación en FreeBSD
Adivina hermano, pkg install py31-ansible
Creación del entorno de Ansible
Yo creé una jail cuya única función es ejecutar playbooks de ansible. Creé un directorio home que se ve así:
inventories/ playbooks/ unbound.conf ansible/inventories: todo.ini ansible/playbooks: update_dns_records.yaml uptime.yml
Inventarios
Los inventarios es donde le dices a Ansible donde están tus máquinas. Están en formato ini y son bastante simples, aquí un ejemplo:
- todo.ini
[suragu] 10.100.0.1 # router 10.100.0.137 # icarus 10.100.0.131 # debian1 10.100.0.132 # smash 10.100.0.135 # mugen [suragu_dns] 10.100.0.131 # debian1 10.100.0.135 # mugen 10.100.1.5 # dns @ alemania 192.168.1.207 # bucefalo
Sinceramente creo que no hay que explicar mas. hay máquinas separadas por bloques. Honestamente creo que no se puede explicar mas.
Playbooks
Los playbooks son las tareas que realizaremos en las máquinas que hay en los inventories. Para desgracia de todos se escriben en formato yaml.
Como esto es un tutorial introductorio de yaml al nivel de “funciona!” voy a poner un ejemplo de playbook que ejecuta e imprime el uptime en todas las máquinas:
- playbooks/uptime.yml
# ejecuta uptime en los servers especificados --- - name: uptime hosts: all remote_user: root tasks: - name: "ejecutar uptime" command: uptime register: uptime - debug: msg="{{ uptime.stdout }}"
Y para ejecutarlo, con ejemplo de output entero:
~/ansible [ansible] % ansible-playbook -i inventories/todo.ini -l diego playbooks/uptime.yml PLAY [uptime] ************************************************************************************************************ TASK [Gathering Facts] *************************************************************************************************** [WARNING]: Platform freebsd on host 10.100.0.1 is using the discovered Python interpreter at /usr/local/bin/python3.11, but future installation of another Python interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information. ok: [10.100.0.1] ok: [10.100.0.131] [WARNING]: Platform freebsd on host 10.100.0.135 is using the discovered Python interpreter at /usr/local/bin/python3.11, but future installation of another Python interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information. ok: [10.100.0.135] [WARNING]: Platform openbsd on host 10.100.0.132 is using the discovered Python interpreter at /usr/local/bin/python3.11, but future installation of another Python interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information. ok: [10.100.0.132] [WARNING]: Platform freebsd on host 10.100.0.137 is using the discovered Python interpreter at /usr/local/bin/python3.11, but future installation of another Python interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information. ok: [10.100.0.137] TASK [ejecutar uptime] *************************************************************************************************** changed: [10.100.0.137] changed: [10.100.0.1] changed: [10.100.0.131] changed: [10.100.0.132] changed: [10.100.0.135] TASK [debug] ************************************************************************************************************* ok: [10.100.0.1] => { "msg": "11:28PM up 2 days, 3:47, 1 user, load averages: 20.30, 15.39, 8.83" } ok: [10.100.0.137] => { "msg": "11:28p. m. up 2 days, 3:47, 3 users, load averages: 20,30 15,39 8,83" } ok: [10.100.0.131] => { "msg": " 23:28:10 up 2 days, 3:45, 1 user, load average: 0,00, 0,00, 0,00" } ok: [10.100.0.132] => { "msg": "11:28PM up 2 days, 3:45, 6 users, load averages: 0.08, 0.02, 0.01" } ok: [10.100.0.135] => { "msg": " 9:20PM up 4 days, 23:25, 1 user, load averages: 0.00, 0.00, 0.00" } PLAY RECAP *************************************************************************************************************** 10.100.0.1 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 10.100.0.131 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 10.100.0.132 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 10.100.0.135 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 10.100.0.137 : ok=3 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Y podemos ver que da unas warnings, pero porque python es muy listo.
Puedes usar DNS también, en vez de IPs para tener un output mas esclarecedor. Pero yo no haría eso porque no confío lo suficiente en DNS
¡Felicidades por haber conseguido hacer funcionar Ansible!