Compare commits

4 Commits

Author SHA1 Message Date
920af07166 serveralias as list 2024-12-13 14:24:34 +01:00
3bc092ada5 make it work on almlinux and debian, really 2023-11-21 14:40:05 +01:00
db9f049e68 with_items -> loop et adaptation nouvelle infra 2021-11-30 15:05:35 +01:00
6d279c75bb solution 2021-03-17 22:55:16 +01:00
19 changed files with 99 additions and 86 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: all
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

60
apache/tasks/main.yml Normal file
View File

@@ -0,0 +1,60 @@
---
# tasks file for myapache
#
- name: include distribution specific variables
include_vars: "{{ ansible_os_family|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: "{{ apache_conf_dir}}/vhost.conf"
mode: 0640
owner: root
group: "{{ apache_group_name }}"
- name: activate apache
tags: httpd
service:
name: "{{ apache_service_name }}"
enabled: yes
- name: open firewall port
tags: httpd
firewalld:
service: http
permanent: yes
immediate: yes
state: enabled
ignore_errors: yes
notify: reload firewalld
when: ansible_os_family=="RedHat"
- name: create documentroot
tags: httpd
file:
name: "{{ item.documentroot }}"
state: directory
loop: "{{ apache_vhosts }}"
- name: create documentroot/Private
tags: httpd
file:
name: "{{ item.documentroot }}/Private"
state: directory
loop: "{{ apache_vhosts }}"
- name: create index file
tags: httpd
copy:
src: index.html
dest: "{{ item.documentroot }}/index.html"
mode: 0644
loop: "{{ 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 %}

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

@@ -0,0 +1,4 @@
apache_package_name: apache2
apache_group_name: www-data
apache_service_name: apache2
apache_conf_dir: /etc/apache2/sites-enabled/

4
apache/vars/redhat.yml Normal file
View File

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

View File

@@ -1,11 +1,11 @@
apache_vhosts:
- servername: orsys.fr
serveralias: www.orsys.fr
serveralias: [ www.orsys.fr ]
documentroot: /var/www/html/orsys.fr
accesslog: /var/log/httpd/access_orsys.fr_log
errorlog: /var/log/httpd/error_orsys.fr_log
- servername: thomas.fr
serveralias: www.thomas.fr
serveralias: [ www.thomas.fr, thomas.org ]
documentroot: /var/www/html/thomas.fr
accesslog: /var/log/httpd/access_thomas.fr_log
errorlog: /var/log/httpd/error_thomas.fr_log

View File

@@ -1,11 +0,0 @@
apache_vhosts:
- servername: foobar.com
serveralias: www.foobar.com
documentroot: /var/www/html/foobar.com
accesslog: /var/log/httpd/access_foobar.com_log
errorlog: /var/log/httpd/error_foobar.com_log
- servername: AllYourBaseAreBelong2.us
serveralias: www.AllYourBaseAreBelong2.us
documentroot: /var/www/html/AllYourBaseAreBelong2.us
accesslog: /var/log/httpd/access_AllYourBaseAreBelong2.us_log
errorlog: /var/log/httpd/error_AllYourBaseAreBelong2.us_log

View File

@@ -0,0 +1,12 @@
apache_vhosts:
- servername: tartempion.fr
serveralias: [www.tartempion.fr ]
documentroot: /var/www/html/tartempion.fr
accesslog: /var/log/apache2/access_tartempion.fr_log
errorlog: /var/log/apache2/error_tartempion.fr_log
- servername: delphine.fr
serveralias: [ www.delphine.fr ]
documentroot: /var/www/html/delphine.fr
accesslog: /var/log/apache2/access_delphine.fr_log
errorlog: /var/log/apache2/error_delphine.fr_log

2
inventory Normal file
View File

@@ -0,0 +1,2 @@
centos1.formation.opendoor.fr
debian1.formation.opendoor.fr

View File

@@ -1,43 +0,0 @@
---
# tasks file for myapache
- name: install apache
yum:
name: httpd
state: latest
- name: conf httpd
notify: reload httpd
template:
src: vhost.conf.jj
dest: /etc/httpd/conf.d/vhost.conf
mode: 0640
owner: root
group: apache
- name: activate apache
service:
name: httpd
enabled: yes
- name: open firewall port
firewalld:
service: http
permanent: yes
immediate: yes
state: enabled
ignore_errors: yes
notify: reload firewalld
- name: create documentroot
file:
name: "{{ item.documentroot }}"
state: directory
with_items:
- "{{ apache_vhosts }}"
- name: create index file
copy:
src: index.html
dest: "{{ item.documentroot }}/index.html"
mode: 0644
with_items:
- "{{ apache_vhosts }}"