Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Bonnegent Sebastien
Cours Ansible
Commits
d502ade3
Commit
d502ade3
authored
Nov 23, 2020
by
Bonnegent Sebastien
Browse files
version finale
parent
f190d2e3
Pipeline
#8660
passed with stage
in 16 seconds
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
.gitignore
0 → 100644
View file @
d502ade3
*~
html/*.html
cours1.md
View file @
d502ade3
...
...
@@ -6,8 +6,8 @@ title: Ansible Pratique
# Ansible
## Support
https://gitlab.insa-rouen.fr/bonnegent/cours_ansible/-/
blob/master/cours1.md
*
cours1.md:
<https://gitlab.insa-rouen.fr/bonnegent/cours_ansible/-/blob/master/cours1.md>
*
version html:
<
https://gitlab.insa-rouen.fr/bonnegent/cours_ansible/-/
pipelines>
## A quoi ca sert ?
-
automatisation des installation
...
...
@@ -55,7 +55,6 @@ Jason Edelman. « Network Automation with Ansible. », O’Reilly Media, 2016.
-
fichiers textes au format YAML
~~~yml
---
# a playbook is a list of plays
-
name: "PLAY1"
hosts: localhost
tasks:
...
...
@@ -73,15 +72,22 @@ Jason Edelman. « Network Automation with Ansible. », O’Reilly Media, 2016.
## Prerequis #2
~~~
bash
# Debian/Ubuntu
sudo apt install ansible
$
sudo apt install ansible
# Fedora/Centos
sudo dnf install ansible
$
sudo dnf install ansible
# Windows
pip3 install ansible
$
pip3 install ansible
ssh-copy-id root@cible1
$ ssh-copy-id root@cible1
~~~
# Utilisation de base
## Inventaire minimum
~~~
# fichier ./hosts
cible1
~~~
## Gestion des cibles: l'inventaire
...
...
@@ -95,7 +101,7 @@ ansible_user=root
# ansible_password=MotDePasse
~~~
##
Ansible #1
##
Ping
~~~
bash
$ ansible ubuntu -i ./hosts -m ping
The authenticity of host 192.168.1.152 cant be established.
...
...
@@ -111,7 +117,7 @@ cible1 | SUCCESS => {
}
~~~
##
Ansible #2
##
Commande
~~~
bash
$ ansible ubuntu -i ./hosts -a 'cat /etc/os-release'
cible1 | CHANGED | rc=0 >>
...
...
@@ -129,6 +135,20 @@ VERSION_CODENAME=groovy
UBUNTU_CODENAME=groovy
~~~
## Variables automatiques
~~~
bash
$ ansible cible1 -i ./hosts -m setup
cible1 | SUCCESS => {
"ansible_facts": {
"ansible_apparmor": {
"status": "disabled"
},
"ansible_architecture": "x86_64"
...
"ansible_distribution": "Ubuntu",
...
~~~
# Livre de jeu
## Playbook #1

...
...
@@ -159,7 +179,7 @@ UBUNTU_CODENAME=groovy
argument2
:
bar
~~~
##
Playbook:
vim
##
Installation de
vim
~~~yml
---
-
name: vim
...
...
@@ -173,7 +193,7 @@ UBUNTU_CODENAME=groovy
state: present
~~~
##
Playbook
#
3
##
Application
#
1
~~~
bash
$ ansible-playbook -i hosts vim.yml
...
...
@@ -190,7 +210,7 @@ PLAY RECAP ********************
cible1 : ok=2 changed=1
~~~
##
Playbook
#
4
##
Application
#
2
~~~
bash
$ ansible-playbook -i hosts vim.yml
...
...
@@ -206,7 +226,7 @@ PLAY RECAP ********************
cible1 : ok=2 changed=0
~~~
##
Playbook #5
##
Vérification
~~~
bash
$ ssh root@192.168.1.152 -p 2222
Welcome to Ubuntu 20.10 (GNU/Linux 5.8.0-28-generic x86_64)
...
...
@@ -216,19 +236,54 @@ root@d743d4e1dd28:~# apt remove -y vim
root@d743d4e1dd28:~# echo "relancer le playbook"
~~~
## Playbook #6
### Options utiles
* --check / -C : applique aucun changement
* --diff / -D : affiche les différences
## Options utiles
* check / -C : applique aucun changement
* diff / -D : affiche les différences
### Copie
## Boucles
~~~
yml
-
name: installation de plusieurs paquets
apt:
name: {{ item }}
state: present
with_items:
-
vim
-
netcat
-
htop
~~~
mkdir files/
date > files/test_copie.txt
## Boucles pour les installations
~~~
yml
-
name: installation de plusieurs paquets
apt:
name: ['vim', 'netcat', 'htop']
state: present
~~~
## Cron
~~~
yml
-
name: auto renew des certificats
cron:
name: letsencrypt_renewal
special_time: monthly
job: "/usr/bin/certbot renew"
-
name: mise à jour automatique base antivirus
cron:
name: freshclam
hour: "06"
minute: "30"
job: "/usr/bin/freshclam --quiet >/dev/null"
~~~
## Copie
~~~
bash
$ mkdir files/
$ date > files/test_copie.txt
~~~
~~~
yml
-
name: copie d'un fichier
copy:
src: test_copie.txt
...
...
@@ -240,18 +295,29 @@ date > files/test_copie.txt
## Template
~~~
bash
mkdir templates
cat templates/test_template.txt
$
mkdir templates
$
cat templates/test_template.txt
ansible_hostname: {{ ansible_hostname }}
~~~
~~~
bash
~~~
yml
-
name: copie d'un template
template:
src: test_template.txt
dest: /root/
~~~
## Mise à jour
~~~
yml
-
name: Mise à jour des paquets
hosts: all
tasks:
-
name: upgrade all packages
dnf:
name: "
*
"
state: latest
~~~
# Variables
## Variables
- group_vars/all: s'applique à toutes les cibles
...
...
@@ -260,25 +326,25 @@ ansible_hostname: {{ ansible_hostname }}
## Variables
~~~
bash
mkdir group_vars host_vars
echo "une variable: {{ test_var }}" >> templates/test_template.txt
echo "test_var: dans group_vars/all" >> group_vars/all
ansible-playbook -i hosts template.yml
ansible all -i ./hosts -a "cat /root/test_template.txt"
$
mkdir group_vars host_vars
$
echo "une variable: {{ test_var }}" >> templates/test_template.txt
$
echo "test_var: dans group_vars/all" >> group_vars/all
$
ansible-playbook -i hosts template.yml
$
ansible all -i ./hosts -a "cat /root/test_template.txt"
~~~
## Variables
~~~
bash
echo "test_var: dans group_vars/ubuntu" >> group_vars/ubuntu
$
echo "test_var: dans group_vars/ubuntu" >> group_vars/ubuntu
~~~
~~~
bash
ansible-playbook -i hosts template.yml
ansible all -i ./hosts -a "cat /root/test_template.txt"
$
ansible-playbook -i hosts template.yml
$
ansible all -i ./hosts -a "cat /root/test_template.txt"
~~~
~~~
bash
echo "test_var: dans host_vars" >> host_vars/cible1
$
echo "test_var: dans host_vars" >> host_vars/cible1
~~~
## Variables securisees
...
...
@@ -288,10 +354,10 @@ echo "test_var: dans host_vars" >> host_vars/cible1
## Variables securisees
~~~
bash
mv host_vars/cible1 vars.yml
mkdir host_vars/cible1
mv vars.yml host_vars/cible1/
echo 'mon_secret: "{{ vault_mon_secret }}"' >> host_vars/cible1/vars.yml
$
mv host_vars/cible1 vars.yml
$
mkdir host_vars/cible1
$
mv vars.yml host_vars/cible1/
$
echo 'mon_secret: "{{ vault_mon_secret }}"' >> host_vars/cible1/vars.yml
~~~
## Creation du trousseau
...
...
@@ -308,10 +374,79 @@ $ ansible-playbook -i hosts --ask-vault-pass template.yml
$ ansible-playbook -i hosts --vault-password-file .vault_pass_file template.yml
~~~
# Handler
## But
* tâche réalisée une seule fois (à la fin)
* seulement si une des tâches associées est modifiée
* exemple: redémarrage d'un daemon
## Exemple
~~~
yml
handlers:
-
name: restart httpd
systemd:
state: restarted
name: httpd
- name: fait truc
command: echo "truc"
~~~
## Utilisation
~~~
yml
tasks:
-
name: une action
command: "true"
notify:
-
restart httpd
- name: une autre action
command: "true"
notify:
- restart httpd
~~~
# Conditions
## Vérifier la présence d'un chemin
~~~
yml
-
stat: path=/var/lib/clamav/daily.cvd
register: freshclamdir
-
name: mise à jour de la base antivirus
command: /usr/bin/freshclam
when: freshclamdir.stat.path is not defined
~~~
## Grande famille
~~~
yml
tasks:
-
shell: echo "seulement famille Debian"
when: ansible_facts['os_family'] == "Debian"
- shell: echo "seulement Red Hat 6, dérivées, et >= 6"
when:
- ansible_facts['os_family'] == "RedHat"
- ansible_facts['lsb']['major_release']|int >= 6
~~~
## Distribution
~~~
yml
-
shell: echo "seulement Debian 9 ou CentOS 7"
when: (ansible_facts['distribution'] == "CentOS" and ansible_facts['distribution_major_version'] == "7") or
(ansible_facts['distribution'] == "Debian" and ansible_facts['distribution_major_version'] == "9")
- shell: echo "seulement CentOS 7"
when:
- ansible_facts['distribution'] == "CentOS"
- ansible_facts['distribution_major_version'] == "7"
~~~
# FIN
## Documentation
https://docs.ansible.com/ansible/latest/user_guide/intro_getting_started.html
* <
https://docs.ansible.com/ansible/latest/user_guide/intro_getting_started.html
>
## Applications
* Serveurs linux: https://gitlab.insa-rouen.fr/bonnegent/ansible-serveurs
* Salles de TP: https://gitlab.insa-rouen.fr/bonnegent/ansible-tp
* Serveurs linux:
<https://gitlab.insa-rouen.fr/bonnegent/ansible-serveurs>
* Salles de TP:
<https://gitlab.insa-rouen.fr/bonnegent/ansible-tp>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment