This commit is contained in:
2021-03-17 22:55:16 +01:00
parent 2f2d25fc40
commit 6d279c75bb
14 changed files with 40 additions and 36 deletions

12
Readme
View File

@@ -1,12 +0,0 @@
Tâche: Utiliser des variables
Condition: très souvent ;)
Norme: éditeur de texte, modules template, variable d'inventaire
Objectif: Toujours avec des variables, faire en sorte qu'une machine puisse héberger *plusieurs* vhost
Prérequis:
* On peut se baser sur l'exercice précédent.
Validation: le playbook s'exécute correctement sur nos cibles.
Chaque cible héberge plusieurs sites différents.

View File

@@ -1,7 +1,5 @@
---
- name: install apache via ansible playbook
hosts: test
user: ansible
become: true
hosts: cibles
roles:
- myapache2
- apache

View File

@@ -3,7 +3,7 @@ Role Name
Rôle de deploiement apache sur une centos.
more than 1 vhost per host
1 seul vhost
Requirements
------------
@@ -14,15 +14,11 @@ Role Variables
--------------
http_port: 80
Hosts variables
--------------
apache_vhosts:
- servername: orsys.fr
serveralias: "www.{{ servername }}"
documentroot: /var/www/html/orsys.fr
accesslog: /var/log/httpd/access_orsys.fr_log
errorlog: /var/log/httpd/error_orsys.fr_log
servername: orsys.fr
serveralias: "www.{{ servername }}"
documentroot: /var/www/html/orsys.fr
accesslog: /var/log/httpd/access_orsys.fr_log
errorlog: /var/log/httpd/error_orsys.fr_log
Dependencies
------------

View File

@@ -2,7 +2,7 @@
# handlers file for myapache
- name: reload httpd
service:
name: httpd
name: "{{ apache_service_name }}"
state: reloaded
- name: reload firewalld

View File

@@ -1,24 +1,34 @@
---
# tasks file for myapache
- name: install apache
yum:
name: httpd
#
- name: include distribution specific variables
include_vars: "{{ ansible_distribution|lower }}.yml"
- name: install apache almost anywhere
tags: httpd
package:
name: "{{ apache_package_name}}"
state: latest
- name: conf httpd
tags: httpd
notify: reload httpd
template:
src: vhost.conf.jj
dest: /etc/httpd/conf.d/vhost.conf
dest: "{{ apache_conf_dir}}/vhost.conf"
mode: 0640
owner: root
group: apache
- name: activate apache
tags: httpd
service:
name: httpd
name: "{{ apache_service_name }}"
enabled: yes
- name: open firewall port
tags: httpd
firewalld:
service: http
permanent: yes
@@ -26,8 +36,10 @@
state: enabled
ignore_errors: yes
notify: reload firewalld
when: ansible_distribution=="CentOS" or "RedHat"
- name: create documentroot
tags: httpd
file:
name: "{{ item.documentroot }}"
state: directory
@@ -35,9 +47,10 @@
- "{{ apache_vhosts }}"
- name: create index file
tags: httpd
copy:
src: index.html
dest: "{{ item.documentroot }}/index.html"
mode: 0644
with_items:
- "{{ apache_vhosts }}"
- "{{ apache_vhosts }}"

View File

@@ -1,7 +1,9 @@
{% for vhost in apache_vhosts %}
<VirtualHost *:{{ http_port }}>
ServerName {{ vhost.servername }}
ServerAlias {{ vhost.serveralias }}
{% for alias in vhost.serveralias %}
ServerAlias {{ alias }}
{% endfor %}
DocumentRoot {{ vhost.documentroot }}
CustomLog {{ vhost.accesslog }} combined
ErrorLog {{ vhost.errorlog }}
@@ -11,8 +13,9 @@
Require all denied
</Directory>
<Directory {{ vhost.documentroot }}>
Require all granted
<Directory {{ vhost.documentroot }}>
Options {{ vhost.documentrootoptions|default( "none" ) }}
Require all granted
</Directory>
</VirtualHost>
{% endfor %}

3
apache/vars/centos.yml Normal file
View File

@@ -0,0 +1,3 @@
apache_package_name: httpd
apache_service_name: httpd
apache_conf_dir: /etc/httpd/conf.d

3
apache/vars/debian.yml Normal file
View File

@@ -0,0 +1,3 @@
apache_package_name: apache2
apache_service_name: apache2
apache_conf_dir: /etc/apache2/sites-enabled/