Compare commits
4 Commits
solution_a
...
solution
| Author | SHA1 | Date | |
|---|---|---|---|
| 84c28ca3f6 | |||
| 910975fd17 | |||
| b8297efac6 | |||
| 250b199fea |
27
Readme.md
27
Readme.md
@@ -1,31 +1,28 @@
|
|||||||
## Includes et import
|
# Solution (proposition)
|
||||||
|
|
||||||
**Tâche**: écrire des playbooks modulaires
|
Plutôt que de conditionner chaque tâche en fonction de la distribution (ce qui sera pénible et difficile à maintenir), la solution consistant à inclure un fichier de variables propre à chaque OS cible est plus élégante.
|
||||||
|
|
||||||
**Condition**: selon besoin
|
|
||||||
|
|
||||||
**Norme**: includes et import
|
|
||||||
|
|
||||||
**Préparation**:
|
**Préparation**:
|
||||||
|
|
||||||
** Pratique **
|
** Pratique **
|
||||||
|
|
||||||
Récupérer la solution de l'atelier handler
|
En examinant les différents fichiers présents dans ce dépôt, essayez de
|
||||||
|
déterminez de quelle manière ce playbook a été adapté pour fonctionner aussi
|
||||||
|
bien sur une CentOS qu'une Debian.
|
||||||
|
|
||||||
Identifier dans le playbook et le fichier vhost.conf toutes les spécificités de RedHat (qui vont empécher le playbook de fonctionner)
|
L'idée est de transformer en variables toutes les spécificités de chaque distribution (nom
|
||||||
|
du paquet, du service, de l'utilisateur dédié au service, du répertoire de conf ...) et de définir ces variables dans des fichiers dont le nom correspond à la distribution cible.
|
||||||
|
|
||||||
Trouver la solution à cette problématique (exécution conditionnelle, variabilisation ...)
|
Il suffit de faire ensuite un include de ces fichiers en construisant le nom du fichier autour de la variable "ansible_distribution"
|
||||||
|
|
||||||
Implémenter la solution
|
L'exécution du block de tâches "firewalld", spécifique à CentOS est conditionné à cette distribution via la clause "when"
|
||||||
|
|
||||||
Sur la debian, la configuration par défaut entre en conflit avec la notre:
|
La tâche "conf httpd" ne fonctionnera pas sur Debian. Pourquoi ? Proposez une solution.
|
||||||
|
|
||||||
Modifier le playbook pour que le fichier /etc/apache2/sites-enabled/000-default.conf soit supprimé UNIQUEMENT sur la debian, avec redémarrage du service apache si besoin
|
Le nom du groupe est différent, il faut en faire une variable
|
||||||
|
|
||||||
|
|
||||||
** Validation:
|
** Validation:
|
||||||
|
|
||||||
```bash
|
le playbook s'exécute correctement sur la machine debian
|
||||||
curl debian1
|
|
||||||
<span style="text-align: center;background-color: #FD5401; font-size: 42px;">Hello World</span>
|
|
||||||
```
|
|
||||||
|
|||||||
65
apache.yml
65
apache.yml
@@ -8,55 +8,68 @@
|
|||||||
state: restarted
|
state: restarted
|
||||||
|
|
||||||
tasks:
|
tasks:
|
||||||
- name: read OS var file
|
|
||||||
include_vars: "{{ ansible_os_family | lower }}.yml"
|
- name: import OS variables
|
||||||
|
include_vars: "{{ ansible_distribution | lower }}.yml"
|
||||||
|
|
||||||
- name: install apache
|
- name: install apache
|
||||||
package:
|
package:
|
||||||
name: "{{ apache_package_name }}"
|
name: "{{ apache_package_name }}"
|
||||||
state: latest
|
state: present
|
||||||
|
|
||||||
- name: conf httpd
|
- name: conf httpd
|
||||||
template:
|
template:
|
||||||
src: vhost.conf
|
src: vhost.conf
|
||||||
dest: "{{ apache_config_dir }}/vhost.conf"
|
dest: "{{ apache_config_dir}}/vhost.conf"
|
||||||
mode: 0640
|
mode: 0640
|
||||||
owner: root
|
owner: root
|
||||||
group: "{{ apache_group }}"
|
group: "{{ apache_group_name }}"
|
||||||
notify: restart apache
|
notify: restart apache
|
||||||
|
|
||||||
- name: delete defaultconf on debian
|
|
||||||
file:
|
|
||||||
path: "{{ apache_config_dir }}/000-default.conf"
|
|
||||||
state: absent
|
|
||||||
notify: restart apache
|
|
||||||
when: ansible_os_family == 'Debian'
|
|
||||||
|
|
||||||
- name: call handlers, if needed
|
|
||||||
ansible.builtin.meta: flush_handlers
|
|
||||||
|
|
||||||
- name: activate apache
|
- name: activate apache
|
||||||
service:
|
service:
|
||||||
name: "{{ apache_service_name }}"
|
name: "{{ apache_service_name }}"
|
||||||
enabled: yes
|
enabled: yes
|
||||||
state: started
|
state: started
|
||||||
|
|
||||||
- name: open firewall port
|
- name: remove default site conf
|
||||||
firewalld:
|
file:
|
||||||
service: http
|
path: /etc/apache2/sites-enabled/000-default.conf
|
||||||
permanent: yes
|
state: absent
|
||||||
immediate: yes
|
notify: restart apache
|
||||||
state: enabled
|
|
||||||
ignore_errors: yes
|
- name: setup firewall
|
||||||
when: ansible_os_family=='RedHat'
|
block:
|
||||||
|
- name: install firewalld packages
|
||||||
|
yum:
|
||||||
|
name:
|
||||||
|
- python3-firewall
|
||||||
|
- firewalld
|
||||||
|
state: present
|
||||||
|
|
||||||
|
- name: enable firewalld service
|
||||||
|
service:
|
||||||
|
name: firewalld
|
||||||
|
enabled: true
|
||||||
|
state: started
|
||||||
|
|
||||||
|
- name: open firewall port
|
||||||
|
firewalld:
|
||||||
|
service: http
|
||||||
|
permanent: yes
|
||||||
|
immediate: yes
|
||||||
|
state: enabled
|
||||||
|
ignore_errors: true
|
||||||
|
when: ansible_distribution == 'CentOS'
|
||||||
|
|
||||||
|
|
||||||
- name: create documentroot
|
- name: create documentroot
|
||||||
file:
|
file:
|
||||||
name: /var/www/html/example.org
|
name: /var/www/html/example.org/
|
||||||
state: directory
|
state: directory
|
||||||
|
|
||||||
- name: create index file
|
- name: copy index file
|
||||||
copy:
|
template:
|
||||||
src: index.txt
|
src: index.txt
|
||||||
dest: /var/www/html/example.org/index.html
|
dest: /var/www/html/example.org/index.html
|
||||||
mode: 0644
|
mode: 0644
|
||||||
|
|||||||
6
centos.yml
Normal file
6
centos.yml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
#/home/formation/sib_10_premier_playbook/centos.yml
|
||||||
|
apache_service_name: httpd
|
||||||
|
apache_package_name: httpd
|
||||||
|
apache_config_dir: /etc/httpd/conf.d
|
||||||
|
apache_log_dir: /var/log/httpd
|
||||||
|
apache_group_name: apache
|
||||||
@@ -1,9 +1,6 @@
|
|||||||
#/home/formation/sib_12_handlers/debian.yml
|
#/home/formation/sib_10_premier_playbook/debian.yml
|
||||||
---
|
|
||||||
apache_service_name: apache2
|
apache_service_name: apache2
|
||||||
apache_package_name: apache2
|
apache_package_name: apache2
|
||||||
apache_user: www-data
|
|
||||||
apache_group: www-data
|
|
||||||
apache_config_dir: /etc/apache2/sites-enabled/
|
apache_config_dir: /etc/apache2/sites-enabled/
|
||||||
apache_log_dir: /var/log/apache2/
|
apache_log_dir: /var/log/apache2
|
||||||
|
apache_group_name: www-data
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
#/home/formation/sib_12_handlers/redhat.yml
|
|
||||||
---
|
|
||||||
apache_service_name: httpd
|
|
||||||
apache_package_name: httpd
|
|
||||||
apache_user: apache
|
|
||||||
apache_group: apache
|
|
||||||
apache_config_dir: /etc/httpd/conf.d/
|
|
||||||
apache_log_dir: /var/log/httpd/
|
|
||||||
25
vhost.conf
Normal file
25
vhost.conf
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#/home/formation/sib_10_premier_playbook/vhost.conf
|
||||||
|
<VirtualHost *:80>
|
||||||
|
ServerName example.org
|
||||||
|
ServerAlias www.example.org
|
||||||
|
ServerAlias {{ inventory_hostname }}
|
||||||
|
DocumentRoot /var/www/html/example.org
|
||||||
|
CustomLog {{ apache_log_dir }}/example.org_access.log combined
|
||||||
|
ErrorLog {{ apache_log_dir }}/example.org_error.log
|
||||||
|
<Directory />
|
||||||
|
Options none
|
||||||
|
Allowoverride none
|
||||||
|
Require all denied
|
||||||
|
</Directory>
|
||||||
|
|
||||||
|
<Directory /var/www/html/example.org>
|
||||||
|
Require all granted
|
||||||
|
</Directory>
|
||||||
|
<Directory /var/www/html/example.org/Private>
|
||||||
|
Options indexes
|
||||||
|
AuthName "stop"
|
||||||
|
AuthType Basic
|
||||||
|
AuthUserFile /etc/{{ apache_service_name }}/passwd
|
||||||
|
require valid-user
|
||||||
|
</Directory>
|
||||||
|
</VirtualHost>
|
||||||
Reference in New Issue
Block a user