presentazione-ansible/content/_index.md

225 lines
4.0 KiB
Markdown
Raw Permalink Normal View History

2020-04-26 18:53:41 +02:00
+++
2020-06-22 18:26:30 +02:00
title = "Ansible per principianti"
2020-04-26 18:53:41 +02:00
outputs = ["Reveal"]
+++
2020-06-22 18:26:30 +02:00
# Ansible
### for dummies
#### (???)
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
---
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
## Cos'è:
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
{{% fragment %}} * E' un software per automatizzare la configurazione (_provisioning_) di un computer {{% /fragment %}}
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
{{% fragment %}} * E' una serie di programmi CLI (`ansible`, `ansible-playbook`, `ansible-galaxy`, `ansible-vault`, ...) {{% /fragment %}}
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
{{% fragment %}} * E' scritto in python {{% /fragment %}}
2020-05-08 20:34:50 +02:00
2020-06-22 18:26:30 +02:00
{{% fragment %}} * Si configura in `yaml` (o `ini`, ma preferiamo `yaml`) {{% /fragment %}}
2020-04-26 18:53:41 +02:00
---
2020-05-08 20:34:50 +02:00
## A cosa serve:
2020-06-22 18:26:30 +02:00
* A configurare _automaticamente_ una o più macchine
2020-05-08 20:34:50 +02:00
2020-06-22 18:26:30 +02:00
* A tenere traccia facilmente di tutti i passi necessari a configurare una macchina (_infrastructure-as-code_)
2020-05-08 20:34:50 +02:00
---
{{% section %}}
2020-04-26 18:53:41 +02:00
# Concetti base
---
2020-06-22 18:26:30 +02:00
## Playbook
2020-06-22 18:26:30 +02:00
* Un _playbook_ è un documento strutturato (`yaml` o `ini`) che istruisce ansible su cosa fare.
2020-05-08 20:34:50 +02:00
2020-06-22 18:26:30 +02:00
* 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.
2020-05-08 20:34:50 +02:00
---
2020-06-22 18:26:30 +02:00
## Connettore
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
* Ansible interagisce con le macchine da configurare attraverso un connettore.
2020-06-22 18:26:30 +02:00
* Lo standard *de facto* è `ssh` (quindi è essenziale avere un minimo di dimestichezza con la sintassi di ssh)
---
2020-06-22 18:26:30 +02:00
## Inventory
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
* L'_inventory_ è un registro di informazioni
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
* Di default ansible legge l'inventory a `/etc/ansible/hosts`. Si può specificare l'inventory con l'opzione `-i <path>`.
2020-05-08 20:34:50 +02:00
2020-04-26 18:53:41 +02:00
---
2020-06-22 18:26:30 +02:00
## Inventory
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
```yaml
2020-04-26 18:53:41 +02:00
---
2020-06-22 18:26:30 +02:00
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:
2020-04-26 18:53:41 +02:00
```
---
2020-06-22 18:26:30 +02:00
## Role
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
* Un _role_ è una funzione autocontenuta
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
* E' una ricetta da applicare ad una macchina ottenere una funzionalità su quella macchina
2020-04-26 18:53:41 +02:00
---
2020-06-22 18:26:30 +02:00
## Facts
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
* I _facts_ sono le verità che riguardano una macchina su cui ansible agisce
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
* Sono raccolti ad inizio esecuzione su ogni macchina
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
* Sono locali alla macchina
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
* Possono essere impostati direttamente da un playbook, se ne abbiamo bisogno
2020-04-26 18:53:41 +02:00
---
2020-06-22 18:26:30 +02:00
## Module
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
* Un _module_ è una libreria che ansible usa per eseguire codice
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
* Rende (più) facile eseguire azioni complesse su una macchina remota
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
* ```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
```
2020-04-26 18:53:41 +02:00
2020-05-08 20:34:50 +02:00
{{% /section %}}
2020-04-26 18:53:41 +02:00
---
2020-06-22 18:26:30 +02:00
{{% section %}}
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
# Roles
2020-04-26 18:53:41 +02:00
---
2020-06-22 18:26:30 +02:00
## Role
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
`ansible-galaxy role init gitea`
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
```yaml
.
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
└── main.yml
```
2020-04-26 18:53:41 +02:00
---
2020-06-22 18:26:30 +02:00
## Task
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
`tasks/main.yml`
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
```yaml
2020-04-26 18:53:41 +02:00
---
2020-06-22 18:26:30 +02:00
- name: Install prerequisites
apt:
name: postgres
state: present
2020-04-26 18:53:41 +02:00
```
---
2020-06-22 18:26:30 +02:00
## Task
2020-05-08 20:34:50 +02:00
2020-06-22 18:26:30 +02:00
```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
```
2020-05-08 20:34:50 +02:00
2020-06-22 18:26:30 +02:00
---
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
## Handler
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
`handlers/main.yml`
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
```yaml
---
- name: restart postgres
systemd:
name: postgres.service
state: restarted
```
2020-04-26 18:53:41 +02:00
---
2020-06-22 18:26:30 +02:00
## Template
2020-04-26 18:53:41 +02:00
2020-06-22 18:26:30 +02:00
`templates/pg_hba.conf.j2`
2020-04-26 18:53:41 +02:00
```
2020-06-22 18:26:30 +02:00
{% 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 %}
2020-04-26 18:53:41 +02:00
```
2020-06-22 18:26:30 +02:00
[Jinja syntax](https://jinja.palletsprojects.com/)
2020-04-26 18:53:41 +02:00
2020-05-08 20:34:50 +02:00
{{% /section %}}
2020-05-11 15:35:42 +02:00
---
2020-04-26 18:53:41 +02:00
# FINE