|
|
|
|
<!-- .slide: data-state="nologo-slide" style="text-align: center" -->
|
|
|
|
|
![Logo Ansible](images/logo-ansible.svg)
|
|
|
|
|
# Installation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Installation d'Ansible
|
|
|
|
|
Environnement : CentOS 7 / 1vCPU / 512m RAM / 10G Disk
|
|
|
|
|
|
|
|
|
|
```none
|
|
|
|
|
$ sudo yum -y install epel-release
|
|
|
|
|
$ sudo yum -y update
|
|
|
|
|
$ sudo yum -y install ansible
|
|
|
|
|
$ ansible --version
|
|
|
|
|
ansible 2.9.16
|
|
|
|
|
config file = /etc/ansible/ansible.cfg
|
|
|
|
|
configured module search path = [u'/home/ansible/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
|
|
|
|
|
ansible python module location = /usr/lib/python2.7/site-packages/ansible
|
|
|
|
|
executable location = /bin/ansible
|
|
|
|
|
python version = 2.7.5 (default, Nov 16 2020, 22:23:17) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Environnement : CentOS 8 / 1vCPU / 768m RAM / 10G Disk
|
|
|
|
|
|
|
|
|
|
```none
|
|
|
|
|
$ sudo dnf -y install epel-release
|
|
|
|
|
$ sudo dnf -y update
|
|
|
|
|
$ sudo dnf -y install ansible
|
|
|
|
|
$ ansible --version
|
|
|
|
|
ansible 2.9.16
|
|
|
|
|
config file = /etc/ansible/ansible.cfg
|
|
|
|
|
configured module search path = ['/home/ansible/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
|
|
|
|
|
ansible python module location = /usr/lib/python3.6/site-packages/ansible
|
|
|
|
|
executable location = /usr/bin/ansible
|
|
|
|
|
python version = 3.6.8 (default, Aug 24 2020, 17:57:11) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Configuration SSH
|
|
|
|
|
|
|
|
|
|
![Schéma connexion ssh](images/ansible-archi-ssh.png) <!-- .element: height="550px" -->
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-state="small-code" -->
|
|
|
|
|
1. Génération d'une paire de clés SSH
|
|
|
|
|
|
|
|
|
|
```none
|
|
|
|
|
ssh-keygen
|
|
|
|
|
Generating public/private rsa key pair.
|
|
|
|
|
Enter file in which to save the key (~/.ssh/id_rsa):
|
|
|
|
|
Created directory '/home/formation/.ssh'.
|
|
|
|
|
Enter passphrase (empty for no passphrase):
|
|
|
|
|
Enter same passphrase again:
|
|
|
|
|
Your identification has been saved in ~/.ssh/id_rsa.
|
|
|
|
|
Your public key has been saved in ~/.ssh/id_rsa.pub.
|
|
|
|
|
The key fingerprint is:
|
|
|
|
|
08:d5:a7:66:ca:a0:0c:6c:2f:40:2b:0f:25:de:cf:4c root@centos7
|
|
|
|
|
The key's randomart image is:
|
|
|
|
|
+--[ RSA 2048]----+
|
|
|
|
|
| .o o.. |
|
|
|
|
|
| o +Eo |
|
|
|
|
|
| + . |
|
|
|
|
|
| . + o |
|
|
|
|
|
| S o = * o|
|
|
|
|
|
| . o @.|
|
|
|
|
|
| . = o|
|
|
|
|
|
| . o |
|
|
|
|
|
| o. |
|
|
|
|
|
+-----------------+
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
2. Copie de la clé publique sur les machines cibles
|
|
|
|
|
|
|
|
|
|
```none
|
|
|
|
|
ssh-copy-id -i formation@10.6.214.70
|
|
|
|
|
ssh-copy-id -i formation@10.6.214.72
|
|
|
|
|
ssh-copy-id -i formation@10.6.214.73
|
|
|
|
|
ssh-copy-id -i formation@10.6.214.74
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-state="small-table" -->
|
|
|
|
|
Fichier | Description
|
|
|
|
|
- | -
|
|
|
|
|
`/etc/ansible/ansible.cfg` | Fichier de configuration global
|
|
|
|
|
`~/.ansible.cfg` | Fichier de surcharge par utilisateur
|
|
|
|
|
`/path/to/project/ansible.cfg` | Fichier de surcharge par projet
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
`/path/to/project/ansible.cfg`
|
|
|
|
|
```toml
|
|
|
|
|
[defaults]
|
|
|
|
|
log_path=./ansible.log
|
|
|
|
|
roles_path = ./roles:/opt/othersite/roles
|
|
|
|
|
retry_files_save_path=./retries/
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Exemple de surcharge locale de la configuration d'Ansible.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-state="nologo-slide" style="text-align: center" -->
|
|
|
|
|
![Logo Ansible](images/logo-ansible.svg)
|
|
|
|
|
# Inventaire
|
|
|
|
|
|
|
|
|
|
Fichier de déclaration des machines cibles à adresser.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Objectifs de l'inventaire
|
|
|
|
|
|
|
|
|
|
1. Déclarer la liste des machines à adresser.
|
|
|
|
|
|
|
|
|
|
2. Déclarer et associer des variables à :
|
|
|
|
|
|
|
|
|
|
- une machine en particulier,
|
|
|
|
|
|
|
|
|
|
- un sous-ensemble des machines,
|
|
|
|
|
|
|
|
|
|
- l'ensemble des machines.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
![Inventaire](images/ansible-ad-hoc-multi-node-deployment.png)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* L'inventaire par défaut est défini dans le fichier `/etc/ansible/hosts`.
|
|
|
|
|
|
|
|
|
|
* Il faut être _root_ pour pouvoir le modifier.
|
|
|
|
|
|
|
|
|
|
* Il est également possible de créer un fichier inventaire à l'emplacement de notre choix.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* Il est aussi possible de gérer plusieurs inventaires correspondant à des plateformes différentes.
|
|
|
|
|
<small>Dans le homedir de l'utilisateur Ansible par exemple :</small>
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-state="medium-code" -->
|
|
|
|
|
```none
|
|
|
|
|
~/inventories/
|
|
|
|
|
├── dev
|
|
|
|
|
│ └── hosts <----- inventaire de l'environnement de dev
|
|
|
|
|
├── qualif
|
|
|
|
|
│ └── hosts <----- inventaire de l'environnement de qualif
|
|
|
|
|
└── prod
|
|
|
|
|
└── hosts <----- inventaire de l'environnement de production
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Déclaration de machines
|
|
|
|
|
|
|
|
|
|
Adresse IP
|
|
|
|
|
|
|
|
|
|
```none
|
|
|
|
|
10.6.214.70
|
|
|
|
|
10.6.214.72
|
|
|
|
|
10.6.214.73
|
|
|
|
|
10.6.214.74
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Alias de la machine et adresse IP
|
|
|
|
|
|
|
|
|
|
```none
|
|
|
|
|
lb.formation.sii.fr ansible_host=10.6.214.70
|
|
|
|
|
web1.formation.sii.fr ansible_host=10.6.214.72
|
|
|
|
|
web2.formation.sii.fr ansible_host=10.6.214.73
|
|
|
|
|
bdd.formation.sii.fr ansible_host=10.6.214.74
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
<small>[https://docs.ansible.com/ansible/intro_inventory.html#hosts-and-groups](https://docs.ansible.com/ansible/intro_inventory.html#hosts-and-groups)</small>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Variables possibles pour les connexions :
|
|
|
|
|
|
|
|
|
|
* `ansible_host`
|
|
|
|
|
Le nom (si ce nom est différent de l'alias) ou l'adresse IP de la machine cible.
|
|
|
|
|
|
|
|
|
|
* `ansible_port`
|
|
|
|
|
Le port ssh (si différent de 22).
|
|
|
|
|
|
|
|
|
|
* `ansible_user`
|
|
|
|
|
L'utilisateur ssh à utiliser (si différent de l'utilisateur courant).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
* `ansible_ssh_pass`
|
|
|
|
|
Le mot de passe ssh de l'utilisateur `ansible_user`.
|
|
|
|
|
|
|
|
|
|
* `ansible_ssh_private_key_file`
|
|
|
|
|
La clé privée utilisée par ssh. Utile en cas d'utilisation de plusieurs clés et si on ne veut pas utiliser SSH agent.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Déclaration de groupes de machines
|
|
|
|
|
<!-- .slide: data-state="small-code" -->
|
|
|
|
|
```toml
|
|
|
|
|
lb.formation.sii.fr ansible_host=10.6.214.70
|
|
|
|
|
web1.formation.sii.fr ansible_host=10.6.214.72
|
|
|
|
|
web2.formation.sii.fr ansible_host=10.6.214.73
|
|
|
|
|
bdd.formation.sii.fr ansible_host=10.6.214.74
|
|
|
|
|
|
|
|
|
|
[lb] # groupe
|
|
|
|
|
lb.formation.sii.fr
|
|
|
|
|
|
|
|
|
|
[web]
|
|
|
|
|
web[1:2].formation.sii.fr # exemple de raccourci
|
|
|
|
|
# d'écriture
|
|
|
|
|
|
|
|
|
|
[db]
|
|
|
|
|
bdd.formation.sii.fr
|
|
|
|
|
|
|
|
|
|
[formation:children] # groupe de groupes
|
|
|
|
|
lb
|
|
|
|
|
web
|
|
|
|
|
db
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Groupes par défaut
|
|
|
|
|
|
|
|
|
|
* `all` : contient toutes les machines.
|
|
|
|
|
|
|
|
|
|
* `ungrouped` : contient toutes les machines qui n'appartiennent pas à un groupe (en dehors de `all`)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Lancer une commande sur un groupe de machines spécifique
|
|
|
|
|
<!-- .slide: data-state="small-code" -->
|
|
|
|
|
```none
|
|
|
|
|
$ ansible formation -m ping
|
|
|
|
|
```
|
|
|
|
|
```json
|
|
|
|
|
10.6.214.70 | SUCCESS => {
|
|
|
|
|
"changed": false,
|
|
|
|
|
"ping": "pong"
|
|
|
|
|
}
|
|
|
|
|
10.6.214.74 | SUCCESS => {
|
|
|
|
|
"changed": false,
|
|
|
|
|
"ping": "pong"
|
|
|
|
|
}
|
|
|
|
|
10.6.214.72 | SUCCESS => {
|
|
|
|
|
"changed": false,
|
|
|
|
|
"ping": "pong"
|
|
|
|
|
}
|
|
|
|
|
10.6.214.73 | SUCCESS => {
|
|
|
|
|
"changed": false,
|
|
|
|
|
"ping": "pong"
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Variables de groupes
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-state="small-code" -->
|
|
|
|
|
```ini
|
|
|
|
|
...
|
|
|
|
|
[web]
|
|
|
|
|
web[1-2].formation.sii.fr
|
|
|
|
|
|
|
|
|
|
[db]
|
|
|
|
|
bdd.formation.sii.fr
|
|
|
|
|
|
|
|
|
|
[formation:children]
|
|
|
|
|
web
|
|
|
|
|
db
|
|
|
|
|
|
|
|
|
|
[formation:vars] # variables du groupe formation
|
|
|
|
|
ansible_user: "formation"
|
|
|
|
|
ansible_ssh_private_key_file: /home/formation/.ssh/id_rsa
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Vérifier les variables de l'inventaire
|
|
|
|
|
<!-- .slide: data-state="medium-code" -->
|
|
|
|
|
|
|
|
|
|
```none
|
|
|
|
|
$ ansible-inventory -i inventories/formation/hosts --list --yaml
|
|
|
|
|
```
|
|
|
|
|
```yaml
|
|
|
|
|
all:
|
|
|
|
|
children:
|
|
|
|
|
web:
|
|
|
|
|
hosts:
|
|
|
|
|
web1.formation.sii.fr:
|
|
|
|
|
ansible_become: 'yes'
|
|
|
|
|
ansible_become_pass: ansible
|
|
|
|
|
ansible_host: 192.168.56.102
|
|
|
|
|
ansible_ssh_private_key_file: /home/ansible/.ssh/id_rsa
|
|
|
|
|
ansible_user: ansible
|
|
|
|
|
web2.formation.sii.fr:
|
|
|
|
|
ansible_become: 'yes'
|
|
|
|
|
ansible_become_pass: ansible
|
|
|
|
|
ansible_host: 192.168.56.103
|
|
|
|
|
ansible_ssh_private_key_file: /home/ansible/.ssh/id_rsa
|
|
|
|
|
ansible_user: ansible
|
|
|
|
|
db:
|
|
|
|
|
hosts:
|
|
|
|
|
bdd.formation.sii.fr:
|
|
|
|
|
ansible_become: 'yes'
|
|
|
|
|
ansible_become_pass: ansible
|
|
|
|
|
ansible_host: 192.168.56.104
|
|
|
|
|
ansible_ssh_private_key_file: /home/ansible/.ssh/id_rsa
|
|
|
|
|
ansible_user: ansible
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Externaliser les variables
|
|
|
|
|
|
|
|
|
|
A utiliser pour séparer les variables de la liste des machines.
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-state="small-code" -->
|
|
|
|
|
```none
|
|
|
|
|
~/inventories/
|
|
|
|
|
└── lab
|
|
|
|
|
├── group_vars
|
|
|
|
|
│ ├── web <--- variables pour le groupe web
|
|
|
|
|
│ └── db <--- variables pour le groupe db
|
|
|
|
|
├── host_vars
|
|
|
|
|
│ ├── web1.formation.sii.fr <--- variables pour la machine web1
|
|
|
|
|
│ └── web2.formation.sii.fr <--- variables pour la machine web2
|
|
|
|
|
└── hosts
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Les dossiers `group_vars` et `host_vars` permettent l'organisation des fichiers de variables.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<!-- .slide: data-state="nologo-slide" style="text-align: center" -->
|
|
|
|
|
## Travaux pratiques
|
|
|
|
|
|
|
|
|
|
![Travaux pratiques](images/tp.gif)
|
|
|
|
|
|
|
|
|
|
<small>[TP Ansible : Inventaire](travaux-pratiques/tp-ansible-inventaire.html)</small>
|