+++ title = "Ansible per principianti" outputs = ["Reveal"] +++ # Ansible ### for dummies #### (???) --- ## Cos'è: {{% fragment %}} * E' un software per automatizzare la configurazione (_provisioning_) di un computer {{% /fragment %}} {{% fragment %}} * E' una serie di programmi CLI (`ansible`, `ansible-playbook`, `ansible-galaxy`, `ansible-vault`, ...) {{% /fragment %}} {{% fragment %}} * E' scritto in python {{% /fragment %}} {{% fragment %}} * Si configura in `yaml` (o `ini`, ma preferiamo `yaml`) {{% /fragment %}} --- ## A cosa serve: * A configurare _automaticamente_ una o più macchine * A tenere traccia facilmente di tutti i passi necessari a configurare una macchina (_infrastructure-as-code_) --- {{% section %}} # Concetti base --- ## Playbook * Un _playbook_ è un documento strutturato (`yaml` o `ini`) che istruisce ansible su cosa fare. * Può contenere tutto il _codice_ che ci serve, ma in generale si usa insieme agli _inventory_ e ai _role_ ed è più che altro un entrypoint. --- ## Connettore * Ansible interagisce con le macchine da configurare attraverso un connettore. * Lo standard *de facto* è `ssh` (quindi è essenziale avere un minimo di dimestichezza con la sintassi di ssh) --- ## Inventory * L'_inventory_ è un registro di informazioni * Di default ansible legge l'inventory a `/etc/ansible/hosts`. Si può specificare l'inventory con l'opzione `-i `. --- ## Inventory ```yaml --- all: hosts: central.machine.org: children: nice: hosts: nancy.machine.org: manny.machine.org: naughty: hosts: john.machine.org: bob.machine.org: red: hosts: charon.machine.org: godot.machine.org: bob.machine.org: blue: hosts: nancy.machine.org: ``` --- ## Role * Un _role_ è una funzione autocontenuta * E' una ricetta da applicare ad una macchina ottenere una funzionalità su quella macchina --- ## Facts * I _facts_ sono le verità che riguardano una macchina su cui ansible agisce * Sono raccolti ad inizio esecuzione su ogni macchina * Sono locali alla macchina * Possono essere impostati direttamente da un playbook, se ne abbiamo bisogno --- ## Module * Un _module_ è una libreria che ansible usa per eseguire codice * Rende (più) facile eseguire azioni complesse su una macchina remota * ```sh $ ansible-doc -l|wc -l 3387 $ ansible-doc -l|grep -E "^apt" apt Manages apt-packages apt_key Add or remove an apt key apt_repo Manage APT repositories via apt-repo apt_repository Add and remove APT repositories apt_rpm apt_rpm package manager ``` {{% /section %}} --- {{% section %}} # Roles --- ## Role `ansible-galaxy role init gitea` ```yaml . ├── defaults │   └── main.yml ├── files ├── handlers │   └── main.yml ├── meta │   └── main.yml ├── README.md ├── tasks │   └── main.yml ├── templates ├── tests │   ├── inventory │   └── test.yml └── vars └── main.yml ``` --- ## Task `tasks/main.yml` ```yaml --- - name: Install prerequisites apt: name: postgres state: present ``` --- ## Task ```yaml - name: Configure database template: src: templates/pg_hba.conf.j2 dest: /var/lib/postgres/pg_hba.conf owner: postgres group: postgres mode: 0600 notify: restart postgres ``` --- ## Handler `handlers/main.yml` ```yaml --- - name: restart postgres systemd: name: postgres.service state: restarted ``` --- ## Template `templates/pg_hba.conf.j2` ``` {% for client in postgresql_hba_entries %} {{ client.type }} {{ client.database }} {{ client.user }} {{ client.address|default('') }} {{ client.ip_address|default('') }} {{ client.ip_mask|default('') }} {{ client.auth_method }} {{ client.auth_options|default("") }} {% endfor %} ``` [Jinja syntax](https://jinja.palletsprojects.com/) {{% /section %}} --- # FINE