inventory
This commit is contained in:
53
ansible/002-inventory.md
Normal file
53
ansible/002-inventory.md
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
# Inventory
|
||||||
|
|
||||||
|
L’elenco delle macchine da gestire viene specificato nel file `inventory`, un file di testo che può essere redatto in diversi formati, tra cui `YAML` o `INI`. Tale file contiene gli indirizzi IP o i nomi DNS dei nodi che saranno gestiti da Ansible.
|
||||||
|
|
||||||
|
Il percorso predefinito per il file inventory è `/etc/ansible/hosts`. Qualora si utilizzi un file personalizzato, è necessario specificarlo durante l’esecuzione dei comandi Ansible tramite l’opzione -i. Ad esempio:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
ansible-playbook -i inventory playbook/my-playbook.yml -K --ask-vault-pass
|
||||||
|
```
|
||||||
|
|
||||||
|
In questo comando, `-i` indica il file inventory, mentre `-K` (o `--ask-become-pass`) richiede la password per l’escalation dei privilegi.
|
||||||
|
|
||||||
|
## Raggruppamento degli Host
|
||||||
|
|
||||||
|
Gli host possono essere organizzati in gruppi secondo diverse best practices, come:
|
||||||
|
|
||||||
|
- `what`: raggruppamento per tipologia (es. webserver, dbserver)
|
||||||
|
- `where`: raggruppamento per ubicazione geografica
|
||||||
|
- `when`: raggruppamento per utilizzo (es. test, sviluppo, produzione)
|
||||||
|
|
||||||
|
Di seguito un esempio di file inventory in formato INI:
|
||||||
|
|
||||||
|
```ini
|
||||||
|
mail.example.com
|
||||||
|
|
||||||
|
[webservers]
|
||||||
|
foo.example.com
|
||||||
|
bar.example.com
|
||||||
|
|
||||||
|
[dbservers]
|
||||||
|
192.168.1.10
|
||||||
|
192.168.1.11
|
||||||
|
192.168.1.[20:30]
|
||||||
|
```
|
||||||
|
|
||||||
|
## Variabili
|
||||||
|
|
||||||
|
È possibile definire variabili specifiche per ciascun host o per i gruppi di host. Alcuni esempi:
|
||||||
|
|
||||||
|
- `ansible_python_interpreter=/usr/bin/python3`
|
||||||
|
- `ansible_user=myuser`
|
||||||
|
|
||||||
|
## Verifica della configurazione
|
||||||
|
|
||||||
|
Per verificare che il file inventory sia stato compilato correttamente, è possibile utilizzare il seguente comando:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd ~/git/ansible
|
||||||
|
|
||||||
|
ansible-inventory --list -i inventory
|
||||||
|
```
|
||||||
|
|
||||||
|
Se il file è stato redatto senza errori, il comando restituirà un riepilogo strutturato del contenuto dell’inventory.
|
||||||
Reference in New Issue
Block a user