presentazione-ansible/content/_index.md

4.0 KiB

+++ 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 <path>.


Inventory

---
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

  • $ 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

.
├── 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

---
- name: Install prerequisites
  apt:
    name: postgres
    state: present

Task

- 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

---
- 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

{{% /section %}}


FINE