Installation/configuration/test de Ansible

Installation/configuration/test de Ansible.

Distribution de travail : Ubuntu 17.10
Serveur de test : Machine virtuelle Gandi

1/ Identification du serveur distant.

Ouvrir :

/etc/hosts

Chercher :

127.0.0.1   localhost

Ajouter après :

217.70.191.12 serv01

2/ Copier la clé publique sur le serveur.

– Génération de la clé ssh :

util06@station06:~$ ssh-keygen

– Copie de la clé ssh :

util06@station06:~$ sudo ssh-copy-id admin@serv01

– Test :

util06@station06:~$ ssh admin@serv01

3/ Installatin de Ansible.

util06@station06:~$ sudo apt-get install software-properties-common
util06@station06:~$ sudo apt-add-repository ppa:ansible/ansible
util06@station06:~$ sudo apt-get update
util06@station06:~$ sudo apt-get install ansible

4/ Identification du serveur.

Créer :

/etc/ansible/hosts

Ajouter :

[web]
217.70.191.12 ansible_user=admin

5/ Test de connexion.

util06@station06:~$ ansible -m ping all --one-line
217.70.191.12 | SUCCESS => {"changed": false, "ping": "pong"}

6/ Affichage de l’uptime du serveur.

util06@station06:~$ ansible all -m command --args "uptime" --one-line 
217.70.191.12 | CHANGED | rc=0 | (stdout)  15:23:47 up  1:51,  1 user,  load average: 0.08, 0.02, 0.01

7/ Affichage de la taille du serveur.

util06@station06:~$ ansible web  -m command -u genma --args "df -h" --one-line
217.70.191.12 | CHANGED | rc=0 | (stdout) Filesystem      Size  Used Avail Use% Mounted on\nudev            234M     0  234M   0% /dev\ntmpfs            49M  1.9M   47M   4% /run\n/dev/xvda1      9.8G  760M  8.6G   8% /\ntmpfs           244M     0  244M   0% /dev/shm\ntmpfs           5.0M     0  5.0M   0% /run/lock\ntmpfs           244M     0  244M   0% /sys/fs/cgroup\ntmpfs            24K     0   24K   0% /var/gandi

8/ Création du répertoire des playbooks.

util06@station06:~$ mkdir -p ANSIBLE/playbook
util06@station06:~$ cd ANSIBLE/playbook
util06@station06:~/ANSIBLE/playbook$ 

9/ Création du playbook de mise-à-jour de serveur.

Créer :

/home/util06/ANSIBLE/playbook/updateupgrade.yml

Ajouter :

- hosts: web
  become: yes
  become_user: root
  become_method: su

  tasks:
  - name: update and upgrade apt packages
    apt:
      update_cache=yes
      state=latest
      upgrade=yes

10/ Jouer le playbook.

util06@station06:~/ANSIBLE/playbook$ ansible-playbook -i /etc/ansible/hosts updateupgrade.yml -K
SUDO password: 

PLAY [web] *********************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************
ok: [217.70.191.12]

TASK [update and upgrade apt packages] *****************************************************************************************************
 [WARNING]: Could not find aptitude. Using apt-get instead.

changed: [217.70.191.12]

PLAY RECAP *********************************************************************************************************************************
217.70.191.12              : ok=2    changed=1    unreachable=0    failed=0   

util06@station06:~/ANSIBLE/playbook$ 

11/ Installation de paquet de base.

Créer :

/home/util06/ANSIBLE/playbook/package_base.yml

Ajouter :

- hosts: web
  become: yes
  become_user: root
  become_method: su

  tasks:
  - name: install common base packages for all servers
    apt:
      update_cache=yes
      state=latest
      name={{item}}
    with_items: 
    - htop
    - vim
    - screen
    - mc

12/ Jouer le playbook.

util06@station06:~/ANSIBLE/playbook$ ansible-playbook -i /etc/ansible/hosts package_base.yml -K
SUDO password: 

PLAY [web] *********************************************************************************************************************************

TASK [Gathering Facts] *********************************************************************************************************************
ok: [217.70.191.12]

TASK [install common base packages for all servers] ****************************************************************************************
changed: [217.70.191.12] => (item=[u'htop', u'vim', u'screen', u'mc'])

PLAY RECAP *********************************************************************************************************************************
217.70.191.12              : ok=2    changed=1    unreachable=0    failed=0   

util06@station06:~/ANSIBLE/playbook$

13/ Configuration des locales.

Créer :

locale.yml

Ajouter :

- hosts: web
  become: yes
  become_user: root
  become_method: su

  tasks:
  - name: generate locales
    locale_gen:
      name: '{{ item }}'
      state: present
    with_items:
      - fr_FR.UTF-8

  - name: set default locale
    lineinfile:
      dest: /etc/default/locale
      regexp: '^LANG=.*$'
      line: 'LANG=fr_FR.UTF-8'
      create: yes
      state: present

  - name: set timezone
    file:
      src: /usr/share/zoneinfo/Europe/Paris
      dest: /etc/localtime
      force: yes
      state: link

Action :

ansible-playbook -i /etc/ansible/hosts locale.yml -K

14/ Dépôt Github.

https://github.com/Nekrofage/playbook_ansible

août 30, 2018