miroir de
https://github.com/PAPAMICA/Wiki-Tech.io.git
synchronisé 2024-07-05 09:30:22 +02:00
docs: update Configuration/Ansible/Inventaires
Cette révision appartient à :
Parent
c58bd8b5e0
révision
2b45352d59
|
@ -2,7 +2,7 @@
|
||||||
title: Ansible - Les inventaires
|
title: Ansible - Les inventaires
|
||||||
description: Créer et gérer les inventaires Ansible
|
description: Créer et gérer les inventaires Ansible
|
||||||
published: true
|
published: true
|
||||||
date: 2021-07-09T08:04:50.183Z
|
date: 2021-07-09T14:09:59.676Z
|
||||||
tags: ansible, configuration
|
tags: ansible, configuration
|
||||||
editor: markdown
|
editor: markdown
|
||||||
dateCreated: 2021-07-09T08:01:35.009Z
|
dateCreated: 2021-07-09T08:01:35.009Z
|
||||||
|
@ -180,8 +180,244 @@ https://linuxhint.com/ansible_inventory_json_format/
|
||||||
<div class="video-responsive">
|
<div class="video-responsive">
|
||||||
<iframe width="560" height="315" src="https://www.youtube.com/embed/UuiRDRIJ-sM" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
|
<iframe width="560" height="315" src="https://www.youtube.com/embed/UuiRDRIJ-sM" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
## Informations
|
||||||
|
ansible = forte précédence des variables (ordre hiérarchique)
|
||||||
|
|
||||||
|
Regroupement par famille :
|
||||||
|
- Configuration settings
|
||||||
|
- Command-line options > VARIABLES D'INVENTAIRES
|
||||||
|
- Playbook keywords
|
||||||
|
- Variables
|
||||||
|
|
||||||
|
22 types:
|
||||||
|
```bash
|
||||||
|
command line values (eg “-u user”)
|
||||||
|
role defaults [1]
|
||||||
|
inventory file or script group vars [2]
|
||||||
|
inventory group_vars/all [3]
|
||||||
|
playbook group_vars/all [3]
|
||||||
|
inventory group_vars/* [3]
|
||||||
|
playbook group_vars/* [3]
|
||||||
|
inventory file or script host vars [2]
|
||||||
|
inventory host_vars/* [3]
|
||||||
|
playbook host_vars/* [3]
|
||||||
|
host facts / cached set_facts [4]
|
||||||
|
play vars
|
||||||
|
play vars_prompt
|
||||||
|
play vars_files
|
||||||
|
role vars (defined in role/vars/main.yml)
|
||||||
|
block vars (only for tasks in block)
|
||||||
|
task vars (only for the task)
|
||||||
|
include_vars
|
||||||
|
set_facts / registered vars
|
||||||
|
role (and include_role) params
|
||||||
|
include params
|
||||||
|
extra vars (always win precedence)
|
||||||
|
```
|
||||||
|
Doc : https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html
|
||||||
|
|
||||||
|
## Variables d'inventaires
|
||||||
|
- fichier d'inventaire
|
||||||
|
- group_vars (répertoire)
|
||||||
|
- host_vars (répertoire)
|
||||||
|
|
||||||
|
|
||||||
|
## Squelette
|
||||||
|
```bash
|
||||||
|
├── 00_inventory.yml
|
||||||
|
├── group_vars
|
||||||
|
│ ├── all.yml
|
||||||
|
│ ├── dbserver.yml
|
||||||
|
│ └── webserver
|
||||||
|
│ ├── vault.yml
|
||||||
|
│ └── webserver.yml
|
||||||
|
└── host_vars
|
||||||
|
├── srv1
|
||||||
|
│ └── srv1.yml
|
||||||
|
└── srv2.yml
|
||||||
|
```
|
||||||
|
|
||||||
|
## Multienv
|
||||||
|
multienv >> c'est facile !!!
|
||||||
|
```bash
|
||||||
|
├── dev
|
||||||
|
│ ├── 00_inventory.yml
|
||||||
|
│ ├── group_vars
|
||||||
|
│ │ ├── all.yml
|
||||||
|
│ │ ├── dbserver.yml
|
||||||
|
│ │ └── webserver
|
||||||
|
│ │ ├── vault.yml
|
||||||
|
│ │ └── webserver.yml
|
||||||
|
│ └── host_vars
|
||||||
|
│ ├── srv1
|
||||||
|
│ │ └── srv1.yml
|
||||||
|
│ └── srv2.yml
|
||||||
|
├── prod
|
||||||
|
│ ├── 00_inventory.yml
|
||||||
|
│ ├── group_vars
|
||||||
|
│ │ ├── all.yml
|
||||||
|
│ │ ├── dbserver.yml
|
||||||
|
│ │ └── webserver
|
||||||
|
│ │ ├── vault.yml
|
||||||
|
│ │ └── webserver.yml
|
||||||
|
│ └── host_vars
|
||||||
|
│ ├── srv1
|
||||||
|
│ │ └── srv1.yml
|
||||||
|
│ └── srv2.yml
|
||||||
|
└── stage
|
||||||
|
├── 00_inventory.yml
|
||||||
|
├── group_vars
|
||||||
|
│ ├── all.yml
|
||||||
|
│ ├── dbserver.yml
|
||||||
|
│ └── webserver
|
||||||
|
│ ├── vault.yml
|
||||||
|
│ └── webserver.yml
|
||||||
|
└── host_vars
|
||||||
|
├── srv1
|
||||||
|
│ └── srv1.yml
|
||||||
|
└── srv2.yml
|
||||||
|
```
|
||||||
|
|
||||||
|
## Commande de test
|
||||||
|
```bash
|
||||||
|
ansible -i "node2," all -b -e "var1=xavki" -m debug -a 'msg={{ var1 }}'
|
||||||
|
```
|
||||||
|
|
||||||
|
## Exemples
|
||||||
|
Inventory :
|
||||||
|
```yaml
|
||||||
|
all:
|
||||||
|
children:
|
||||||
|
common:
|
||||||
|
children:
|
||||||
|
webserver:
|
||||||
|
hosts:
|
||||||
|
node[2:3]:
|
||||||
|
vars:
|
||||||
|
var1: "webserver"
|
||||||
|
dbserver:
|
||||||
|
hosts:
|
||||||
|
node4:
|
||||||
|
node5:
|
||||||
|
var1: "node5"
|
||||||
|
vars:
|
||||||
|
var1: "dbserver"
|
||||||
|
monitoring:
|
||||||
|
children:
|
||||||
|
webserver:
|
||||||
|
dbserver:
|
||||||
|
```
|
||||||
|
|
||||||
|
Autour des group_vars
|
||||||
|
```
|
||||||
|
├── group_vars
|
||||||
|
│ ├── dbserver.yml
|
||||||
|
│ └── webserver.yml
|
||||||
|
└── inventory.yml
|
||||||
|
```
|
||||||
|
Ou encore
|
||||||
|
```
|
||||||
|
├── group_vars
|
||||||
|
│ ├── dbserver
|
||||||
|
│ │ └── dbserver.yml
|
||||||
|
│ └── webserver
|
||||||
|
│ └── webserver.yml
|
||||||
|
└── inventory.yml
|
||||||
|
```
|
||||||
|
|
||||||
|
Autour des host_vars
|
||||||
|
```
|
||||||
|
├── group_vars
|
||||||
|
│ ├── dbserver
|
||||||
|
│ │ └── dbserver.yml
|
||||||
|
│ └── webserver
|
||||||
|
│ └── webserver.yml
|
||||||
|
├── host_vars
|
||||||
|
│ ├── node2
|
||||||
|
│ │ └── variables.yml
|
||||||
|
│ └── node5.yml
|
||||||
|
└── inventory.yml
|
||||||
|
```
|
||||||
|
|
||||||
|
Et si on ajoutait un host au hasard et appartenant à "all"
|
||||||
|
```yaml
|
||||||
|
all:
|
||||||
|
children:
|
||||||
|
common:
|
||||||
|
children:
|
||||||
|
webserver:
|
||||||
|
hosts:
|
||||||
|
node[2:3]:
|
||||||
|
dbserver:
|
||||||
|
hosts:
|
||||||
|
node4:
|
||||||
|
node5:
|
||||||
|
monitoring:
|
||||||
|
hosts:
|
||||||
|
node6:
|
||||||
|
children:
|
||||||
|
webserver:
|
||||||
|
dbserver:
|
||||||
|
```
|
||||||
|
```
|
||||||
|
├── group_vars
|
||||||
|
│ ├── all.yml
|
||||||
|
│ ├── dbserver
|
||||||
|
│ │ └── dbserver.yml
|
||||||
|
│ └── webserver
|
||||||
|
│ └── webserver.yml
|
||||||
|
├── host_vars
|
||||||
|
│ ├── node2
|
||||||
|
│ │ └── variables.yml
|
||||||
|
│ └── node5.yml
|
||||||
|
└── inventory.yml
|
||||||
|
```
|
||||||
|
|
||||||
# La commande et le graph
|
# La commande et le graph
|
||||||
<div class="video-responsive">
|
<div class="video-responsive">
|
||||||
<iframe width="560" height="315" src="https://www.youtube.com/embed/D6Ru-M04G4M" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
|
<iframe width="560" height="315" src="https://www.youtube.com/embed/D6Ru-M04G4M" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
## Commande ansible-inventory
|
||||||
|
* pour y voir plus clair
|
||||||
|
* pour grapher
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Export au format json par défaut (serveurs et variables d'inventaire)
|
||||||
|
```bash
|
||||||
|
ansible-inventory -i <inventory_file> --list
|
||||||
|
ansible-inventory -i <inventory_file> --yaml
|
||||||
|
```
|
||||||
|
|
||||||
|
Plus compact
|
||||||
|
```bash
|
||||||
|
ansible-inventory -i 00_inventory.yml --list --export
|
||||||
|
```
|
||||||
|
|
||||||
|
Afficher un format compact sans variables
|
||||||
|
```
|
||||||
|
ansible-inventory -i <inventory_file> --graph
|
||||||
|
```
|
||||||
|
Avec les variables
|
||||||
|
```
|
||||||
|
ansible-inventory -i 00_inventory.yml --graph --vars
|
||||||
|
```
|
||||||
|
Export vers un fichier
|
||||||
|
```
|
||||||
|
ansible-inventory -i <inventory_file> --output
|
||||||
|
```
|
||||||
|
|
||||||
|
Format toml
|
||||||
|
```
|
||||||
|
pip3 install toml
|
||||||
|
ansible-inventory -i <inventory_file> --vars --toml
|
||||||
|
```
|
||||||
|
|
||||||
|
## Mettre sous forme de graphique
|
||||||
|
```
|
||||||
|
pip3 install ansible-inventory-grapher
|
||||||
|
sudo apt install graphviz graphicsmagick-imagemagick-compat
|
||||||
|
ansible-inventory-grapher -i inventory.yml all | dot -Tpng | display png:-
|
||||||
|
```
|
Chargement…
Référencer dans un nouveau ticket