Ansible : prise en main
Démarrer avec ansible
Environnement
| IP | Nom de l’hôte | Rôle | OS |
|---|---|---|---|
| 172.16.0.10/24 | server.stan.local | Serveur ansible | CentOS 7 |
| 172.16.0.11/24 | client-01.stan.local | Client ansible | CentOS 7 |
| 172.16.0.12/24 | client-02.stan.local | Client ansible | CentOS 7 |
Côté client
Installer python.
[root@cent-os ~]#: yum install -y python
Côté serveur
prerequis : la clé public serveur doit être sur ses clients via la commande ssh-copy-ip. Ensuite il faut que le serveur se connecte 1 fois sur chaque client pour établir l’authenticité de ce dernier.
Installation du packet ansible.
[root@cent-os ~]#: yum install -y ansible
: en l’absence de DNS éditer le fichier /etc/hosts
[root@cent-os ~]#: vim /etc/hosts
aperçu du contenu du fichier
172.16.0.11 client-01.stan.local 172.16.0.12 client-02.stan.local
Éditer l’inventaire de ansible.
[root@cent-os ~]#: vim /etc/ansible/hosts
contenu du fichier
client-01.stan.local client-02.stan.local
Les playbook
Faire un ping sur les clients
: l’indentation est très importante.
Éditer un fichier ping.yml.
[root@cent-os ~]#: vim ping.yml
contenu du fichier
---
- hosts: all
tasks:
- name: Network test
ping:
Vérifier la syntax du fichier *.yml.
[root@cent-os ~]#: ansible-playbook ping.yml --syntax-check
playbook: ping.yml
Lancer la commande.
[root@cent-os ~]#: ansible-playbook ping.yml
PLAY [all] ********************************************************************************************************************************* TASK [Gathering Facts] ********************************************************************************************************************* ok: [ansi-cl-02.ansible.local] ok: [ansi-cl-01.ansible.local] TASK [Network test] ************************************************************************************************************************ ok: [ansi-cl-02.ansible.local] ok: [ansi-cl-01.ansible.local] PLAY RECAP ********************************************************************************************************************************* ansi-cl-01.ansible.local : ok=2 changed=0 unreachable=0 failed=0 ansi-cl-02.ansible.local : ok=2 changed=0 unreachable=0 failed=0
Installer httpd sur les clients
Éditer un fichier httpd.yml.
[root@cent-os ~]#: vim httpd.yml
contenu du fichier
---
- hosts: all
tasks:
- name: Installing Apache (httpd)
yum:
name: httpd
state: present
Lancer la commande.
[root@cent-os ~]#: ansible-playbook httpd.yml
sortie
PLAY [all] ********************************************************************************************************************************* TASK [Gathering Facts] ********************************************************************************************************************* ok: [ansi-cl-01.ansible.local] ok: [ansi-cl-02.ansible.local] TASK [Installing Apache (httpd)] *********************************************************************************************************** changed: [ansi-cl-01.ansible.local] changed: [ansi-cl-02.ansible.local] PLAY RECAP ********************************************************************************************************************************* ansi-cl-01.ansible.local : ok=2 changed=1 unreachable=0 failed=0 ansi-cl-02.ansible.local : ok=2 changed=1 unreachable=0 failed=0
Installer LAMD sur les clients
Dans cet exemple il y a un fichier à copier depuis le serveur ansible vers les clients.
Éditer un fichier index.php.
[root@cent-os ~]#: vim index.php
contenu du fichier
<?php phpinfo(); ?>
Éditer un fichier lamp.yml.
[root@cent-os ~]#: vim lamp.yml
contenu du fichier
---
- hosts: all
become: yes
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
tasks:
- name: Installing Apache (httpd)
package:
name: httpd
state: present
- name: Installing PHP
package:
name: php
state: present
- name: Installing PHP modules
package: name={{item}} state=present
with_items:
- php-pdo
- php-mysql
- php-soap
- php-gd
- php-pear-MDB2-Driver-mysqli
- name: Installing MariaDB
package:
name: mariadb-server
- name: Start Apache
service:
name: httpd
state: started
enabled: yes
- name: Start MariaDB
service:
name: mariadb
state: started
enabled: yes
- name: Installing index.php (test page)
copy:
src: index.php
dest: /var/www/html/index.php
notify:
- restart httpd
- name: Updating firewall
action: command /usr/bin/firewall-cmd --permanent --add-port=80/tcp
- name: Restarting firewall
service:
name: firewalld
state: restarted
by Nicolas SHINEY | April 26, 2018 | No Comments | Système | Tags : ansible configuration de masse