Este post se limita a incluir algunas notas sobre el uso de Ansible y en particular sobre la realización de un tutorial paso a paso sobre la aplicación del mismo, incluyendo los conceptos más básicos.
Inventory file
1 2 3 4 |
[ansiblet1@svprac02 step-01]$ cat hosts host0.example.org ansible_host=192.168.33.10 ansible_user=root host1.example.org ansible_host=192.168.33.11 ansible_user=root host2.example.org ansible_host=192.168.33.12 ansible_user=root |
Ejecuta comandos
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[ansiblet1@svprac02 step-01]$ ansible -m ping all -i hosts host1.example.org | SUCCESS => { "changed": false, "ping": "pong" } host0.example.org | SUCCESS => { "changed": false, "ping": "pong" } host2.example.org | SUCCESS => { "changed": false, "ping": "pong" } |
Módulos (plugins)
1 2 |
[ansiblet1@svprac02 ansible-tuto]$ ansible -i step-02/hosts -m copy -a 'src=/etc/motd dest=/tmp/' host0.example.org [ansiblet1@svprac02 ansible-tuto]$ ansible -i step-02/hosts -m shell -a 'grep DISTRIB_RELEASE /etc/lsb-release' all |
Obtiene información sobre los nodos del inventario
1 2 |
[ansiblet1@svprac02 ansible-tuto]$ ansible -i step-02/hosts -m setup host0.example.org [ansiblet1@svprac02 ansible-tuto]$ ansible -i step-02/hosts -m setup -a 'filter=ansible_memtotal_mb' all |
Fichero de inventario, agrupaciones
1 2 3 4 5 6 7 8 9 |
[ubuntu] host0.example.org [debian] host[1:2].example.org [linux:children] ubuntu debian |
Variables en fichero de inventario
1 2 |
[ubuntu] host0.example.org ansible_host=192.168.0.12 ansible_port=2222 |
Playbooks
1 2 3 4 5 |
[ansiblet1@svprac02 step-04]$ cat apache.yml - hosts: web tasks: - name: Installs apache web server apt: pkg=apache2 state=installed update_cache=true |
Registra resultado de tareas y ejecuta en función de su valor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
- name: Check that our config is valid command: apache2ctl configtest register: result ignore_errors: True - name: Rolling back - Restoring old default virtualhost command: a2ensite 000-default when: result|failed - name: Rolling back - Removing out virtualhost command: a2dissite awesome-app when: result|failed - name: Rolling back - Ending playbook fail: msg="Configuration file is not valid. Please check that before re-running the playbook." when: result|failed |
Iteraciones en playbooks
1 2 3 4 5 6 |
- name: Installs necessary packages apt: pkg={{ item }} state=latest with_items: - apache2 - libapache2-mod-php5 - git |
Usar tags en playbooks
1 2 3 |
- name: Deploy our awesome application git: repo=https://github.com/leucos/ansible-tuto-demosite.git dest=/var/www/awesome-app tags: deploy |
1 |
[ansiblet1@svprac02 step-08]$ ansible-playbook -i hosts apache.yml -t deploy |
Jinja templates
1 2 3 4 5 6 7 8 |
listen cluster bind {{ ansible_eth1['ipv4']['address'] }}:80 mode http stats enable balance roundrobin {% for backend in groups['web'] %} server {{ hostvars[backend]['ansible_hostname'] }} {{ hostvars[backend]['ansible_eth1']['ipv4']['address'] }} check port 80 {% endfor %} |
And in the playbook
1 |
template: src=templates/haproxy.cfg.j2 dest=/etc/haproxy/haproxy.cfg mode=0640 owner=root group=root |
Lanza varios playbooks
1 |
$ ansible-playbook -i step-10/hosts step-10/apache.yml step-10/haproxy.yml |
Variables para hosts y para grupos
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
[ansiblet1@svprac02 step-11]$ ll total 20 -rw-r--r--. 1 ansiblet1 ansiblet 1624 Jul 5 18:54 apache.yml drwxr-xr-x. 2 ansiblet1 ansiblet 25 Jul 5 18:54 files drwxr-xr-x. 2 ansiblet1 ansiblet 21 Jul 5 18:54 group_vars -rw-r--r--. 1 ansiblet1 ansiblet 576 Jul 5 18:54 haproxy.yml -rw-r--r--. 1 ansiblet1 ansiblet 206 Jul 5 18:54 hosts drwxr-xr-x. 2 ansiblet1 ansiblet 81 Jul 5 18:54 host_vars -rw-r--r--. 1 ansiblet1 ansiblet 5246 Jul 5 18:54 README.md drwxr-xr-x. 2 ansiblet1 ansiblet 28 Jul 5 18:54 templates [ansiblet1@svprac02 step-11]$ cat group_vars/haproxy haproxy_check_interval: 3000 haproxy_stats_socket: /tmp/sock |
Se pueden usar en templates
1 2 3 |
{% if haproxy_stats_socket %} stats socket {{ haproxy_stats_socket }} {% endif %} |
Roles
1 2 3 4 5 6 7 8 9 |
[ansiblet1@svprac02 step-12]$ ll roles/apache/ total 0 drwxr-xr-x. 2 ansiblet1 ansiblet 25 Jul 5 18:54 files drwxr-xr-x. 2 ansiblet1 ansiblet 22 Jul 5 18:54 handlers drwxr-xr-x. 2 ansiblet1 ansiblet 22 Jul 5 18:54 tasks [ansiblet1@svprac02 step-12]$ ll roles/apache/tasks/ total 4 -rw-r--r--. 1 ansiblet1 ansiblet 1221 Jul 5 18:54 main.yml |
Se lanzan los roles desde el playbook principal
1 2 3 4 5 6 7 8 |
[ansiblet1@svprac02 step-12]$ cat site.yml - hosts: web roles: - { role: apache } - hosts: haproxy roles: - { role: haproxy } |
Estructura de los roles
roles
|
|_some_role
|
|_defaults
| |
| |_main.yml
| |_…
|
|_files
| |
| |_file1
| |_…
|
|_handlers
| |
| |_main.yml
| |_some_other_file.yml
| |_ …
|
|_meta
| |
| |_main.yml
| |_some_other_file.yml
| |_ …
|
|_tasks
| |
| |_main.yml
| |_some_other_file.yml
| |_ …
|
|_templates
| |
| |_template1.j2
| |_…
|
|_vars
|
|_main.yml
|_some_other_file.yml
|_ …
Referencias
https://www.ansible.com/get-started
https://www.ansible.com/how-ansible-works
https://www.ansible.com/webinars-training/introduction-to-ansible