Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f02f3803e0 | |||
| 450421e5f6 | |||
| 6e72c1389c | |||
| 069d12bf89 | |||
| 1724fa1365 | |||
| 29de3e6b8b | |||
| 745f9b0598 |
@@ -0,0 +1,29 @@
|
||||
## Variables complexes
|
||||
|
||||
**Tâche**: Utiliser des variables
|
||||
|
||||
**Condition**: très souvent ;)
|
||||
|
||||
**Norme**: éditeur de texte, modules template, variable d'inventaire
|
||||
|
||||
Préparation: récupérer la base de l'atelier:
|
||||
|
||||
```bash
|
||||
cd
|
||||
git clone https://git.opendoor.fr/tom/abr_day4
|
||||
cd abr_day4
|
||||
|
||||
```
|
||||
|
||||
Objectif: Modifier le rôle pour que l'on puisse héberger plusieurs vhost sur une même machine. Les éléments suivants seront propre à chaque vhost:
|
||||
* servername
|
||||
* serveralias
|
||||
* documentroot
|
||||
* log d'accès et d'erreur
|
||||
|
||||
Prérequis:
|
||||
|
||||
Validation: le playbook s'exécute correctement sur nos cibles.
|
||||
Chaque cible héberge plusieurs sites différents.
|
||||
|
||||
Solution: voir branche "solution"
|
||||
+3
-2
@@ -1,5 +1,6 @@
|
||||
---
|
||||
- name: install apache via ansible playbook
|
||||
hosts: all
|
||||
hosts: cibles
|
||||
|
||||
roles:
|
||||
- apache
|
||||
- tco.apache_abrd4
|
||||
@@ -1,2 +0,0 @@
|
||||
---
|
||||
# defaults file for myapache
|
||||
@@ -1 +0,0 @@
|
||||
<h1>hello World</h1>
|
||||
@@ -1,11 +0,0 @@
|
||||
---
|
||||
# handlers file for myapache
|
||||
- name: reload httpd
|
||||
service:
|
||||
name: "{{ apache_service_name }}"
|
||||
state: reloaded
|
||||
|
||||
- name: reload firewalld
|
||||
service:
|
||||
name: firewalld
|
||||
state: reloaded
|
||||
@@ -1,60 +0,0 @@
|
||||
---
|
||||
# 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 }}"
|
||||
@@ -1,21 +0,0 @@
|
||||
{% for vhost in apache_vhosts %}
|
||||
<VirtualHost *:{{ http_port }}>
|
||||
ServerName {{ vhost.servername }}
|
||||
{% for alias in vhost.serveralias %}
|
||||
ServerAlias {{ alias }}
|
||||
{% endfor %}
|
||||
DocumentRoot {{ vhost.documentroot }}
|
||||
CustomLog {{ vhost.accesslog }} combined
|
||||
ErrorLog {{ vhost.errorlog }}
|
||||
<Directory />
|
||||
Options none
|
||||
Allowoverride none
|
||||
Require all denied
|
||||
</Directory>
|
||||
|
||||
<Directory {{ vhost.documentroot }}>
|
||||
Options {{ vhost.documentrootoptions|default( "none" ) }}
|
||||
Require all granted
|
||||
</Directory>
|
||||
</VirtualHost>
|
||||
{% endfor %}
|
||||
@@ -1,4 +0,0 @@
|
||||
apache_package_name: apache2
|
||||
apache_group_name: www-data
|
||||
apache_service_name: apache2
|
||||
apache_conf_dir: /etc/apache2/sites-enabled/
|
||||
@@ -1,3 +0,0 @@
|
||||
---
|
||||
# vars file for myapache
|
||||
http_port: 80
|
||||
@@ -1,4 +0,0 @@
|
||||
apache_package_name: httpd
|
||||
apache_group_name: apache
|
||||
apache_service_name: httpd
|
||||
apache_conf_dir: /etc/httpd/conf.d
|
||||
@@ -0,0 +1,15 @@
|
||||
apache_vhosts:
|
||||
- servername: vh1
|
||||
serveralias:
|
||||
- sa1
|
||||
- sa2
|
||||
access_log: /var/log/vh1_access
|
||||
error_log: /var/log/vh1_error
|
||||
documentroot: /srv/vh1/www
|
||||
- servername: vh2
|
||||
serveralias:
|
||||
- sa2
|
||||
- sa2
|
||||
access_log: /var/log/vh2_access
|
||||
error_log: /var/log/vh2_error
|
||||
documentroot: /srv/vh2/www
|
||||
@@ -1,11 +0,0 @@
|
||||
apache_vhosts:
|
||||
- servername: 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, thomas.org ]
|
||||
documentroot: /var/www/html/thomas.fr
|
||||
accesslog: /var/log/httpd/access_thomas.fr_log
|
||||
errorlog: /var/log/httpd/error_thomas.fr_log
|
||||
@@ -1,12 +0,0 @@
|
||||
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
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
---
|
||||
language: python
|
||||
python: "2.7"
|
||||
|
||||
# Use the new container infrastructure
|
||||
sudo: false
|
||||
|
||||
# Install ansible
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- python-pip
|
||||
|
||||
install:
|
||||
# Install ansible
|
||||
- pip install ansible
|
||||
|
||||
# Check ansible version
|
||||
- ansible --version
|
||||
|
||||
# Create ansible.cfg with correct roles_path
|
||||
- printf '[defaults]\nroles_path=../' >ansible.cfg
|
||||
|
||||
script:
|
||||
# Basic role syntax check
|
||||
- ansible-playbook tests/test.yml -i tests/inventory --syntax-check
|
||||
|
||||
notifications:
|
||||
webhooks: https://galaxy.ansible.com/api/v1/notifications/
|
||||
@@ -1,9 +1,7 @@
|
||||
Role Name
|
||||
=========
|
||||
|
||||
Rôle de deploiement apache sur une centos.
|
||||
|
||||
1 seul vhost
|
||||
Install apache,
|
||||
|
||||
Requirements
|
||||
------------
|
||||
@@ -13,12 +11,10 @@ None
|
||||
Role Variables
|
||||
--------------
|
||||
|
||||
http_port: 80
|
||||
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
|
||||
apache_server_name: no default
|
||||
apache_document_root: no default
|
||||
|
||||
see also files in vars subdirectory
|
||||
|
||||
Dependencies
|
||||
------------
|
||||
@@ -32,7 +28,7 @@ Including an example of how to use your role (for instance, with variables passe
|
||||
|
||||
- hosts: servers
|
||||
roles:
|
||||
- { myapache }
|
||||
- tco.apache_abrd4
|
||||
|
||||
License
|
||||
-------
|
||||
@@ -42,4 +38,4 @@ BSD
|
||||
Author Information
|
||||
------------------
|
||||
|
||||
Thomas Constans <thomas@opendoor.fr>
|
||||
Thomas C <thomas@opendoor.fr>
|
||||
@@ -0,0 +1,2 @@
|
||||
---
|
||||
# defaults file for tco.apache_abrd4
|
||||
@@ -0,0 +1,6 @@
|
||||
---
|
||||
# handlers file for tco.apache_abrd4
|
||||
- name: restart apache
|
||||
ansible.builtin.service:
|
||||
name: "{{ apache_service_name }}"
|
||||
state: restarted
|
||||
@@ -1,36 +1,31 @@
|
||||
galaxy_info:
|
||||
author: your name
|
||||
description: your description
|
||||
description: your role description
|
||||
company: your company (optional)
|
||||
|
||||
# If the issue tracker for your role is not on github, uncomment the
|
||||
# next line and provide a value
|
||||
# issue_tracker_url: http://example.com/issue/tracker
|
||||
|
||||
# Some suggested licenses:
|
||||
# - BSD (default)
|
||||
# Choose a valid license ID from https://spdx.org - some suggested licenses:
|
||||
# - BSD-3-Clause (default)
|
||||
# - MIT
|
||||
# - GPLv2
|
||||
# - GPLv3
|
||||
# - Apache
|
||||
# - CC-BY
|
||||
license: license (GPLv2, CC-BY, etc)
|
||||
# - GPL-2.0-or-later
|
||||
# - GPL-3.0-only
|
||||
# - Apache-2.0
|
||||
# - CC-BY-4.0
|
||||
license: license (GPL-2.0-or-later, MIT, etc)
|
||||
|
||||
min_ansible_version: 1.2
|
||||
min_ansible_version: 2.1
|
||||
|
||||
# If this a Container Enabled role, provide the minimum Ansible Container version.
|
||||
# min_ansible_container_version:
|
||||
|
||||
# Optionally specify the branch Galaxy will use when accessing the GitHub
|
||||
# repo for this role. During role install, if no tags are available,
|
||||
# Galaxy will use this branch. During import Galaxy will access files on
|
||||
# this branch. If Travis integration is configured, only notifications for this
|
||||
# branch will be accepted. Otherwise, in all cases, the repo's default branch
|
||||
# (usually master) will be used.
|
||||
#github_branch:
|
||||
|
||||
#
|
||||
# platforms is a list of platforms, and each platform has a name and a list of versions.
|
||||
# Provide a list of supported platforms, and for each platform a list of versions.
|
||||
# If you don't wish to enumerate all versions for a particular platform, use 'all'.
|
||||
# To view available platforms and versions (or releases), visit:
|
||||
# https://galaxy.ansible.com/api/v1/platforms/
|
||||
#
|
||||
# platforms:
|
||||
# - name: Fedora
|
||||
@@ -0,0 +1,68 @@
|
||||
---
|
||||
# tasks file for tco.apache_abrd4
|
||||
- name: import OS variables
|
||||
ansible.builtin.include_vars: "{{ ansible_os_family | lower }}.yml"
|
||||
|
||||
- name: install apache
|
||||
ansible.builtin.package:
|
||||
name: "{{ apache_package_name }}"
|
||||
state: present
|
||||
|
||||
- name: conf httpd
|
||||
ansible.builtin.template:
|
||||
src: vhost.conf
|
||||
dest: "{{ apache_config_dir }}/vhost.conf"
|
||||
mode: 0640
|
||||
owner: root
|
||||
group: "{{ apache_group }}"
|
||||
notify: restart apache
|
||||
|
||||
- meta: flush_handlers
|
||||
|
||||
- name: activate apache
|
||||
ansible.builtin.service:
|
||||
name: "{{ apache_service_name }}"
|
||||
enabled: yes
|
||||
state: restarted
|
||||
|
||||
- name: setup firewall
|
||||
block:
|
||||
- name: install firewalld packages
|
||||
ansible.builtin.dnf:
|
||||
name:
|
||||
- python3-firewall
|
||||
- firewalld
|
||||
state: present
|
||||
|
||||
- name: enable firewalld service
|
||||
ansible.builtin.service:
|
||||
name: firewalld
|
||||
enabled: true
|
||||
state: started
|
||||
|
||||
- name: open firewall port
|
||||
ansible.posix.firewalld:
|
||||
service: "{{ item }}"
|
||||
permanent: yes
|
||||
immediate: yes
|
||||
state: enabled
|
||||
loop:
|
||||
- http
|
||||
- https
|
||||
ignore_errors: true
|
||||
when: ansible_os_family == 'RedHat'
|
||||
|
||||
- name: create documentroot
|
||||
file:
|
||||
name: "{{ item.document_root }}"
|
||||
state: directory
|
||||
loop: "{{ apache_vhosts }}"
|
||||
|
||||
|
||||
- name: copy index file
|
||||
ansible.builtin.template:
|
||||
src: index.txt
|
||||
dest: "{{ item.document_root }}/index.html"
|
||||
mode: 0644
|
||||
loop: "{{ apache_vhosts }}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
<h1>hello World {{ apache_server_name }} </h1>
|
||||
@@ -0,0 +1,27 @@
|
||||
{% for vhost in apache_vhosts %}
|
||||
<VirtualHost *:80>
|
||||
ServerName {{ vhost.server_name }}
|
||||
{% for alias in vhost.server_alias %}
|
||||
ServerAlias {{ alias }}
|
||||
{% endfor %}
|
||||
ServerAlias {{ inventory_hostname }}
|
||||
DocumentRoot {{ vhost.document_root }}
|
||||
CustomLog {{ apache_log_dir }}/{{ vhost.server_name }}_access.log combined
|
||||
ErrorLog {{ apache_log_dir }}/{{ vhost.server_name }}_error.log
|
||||
<Directory />
|
||||
Options none
|
||||
Allowoverride none
|
||||
Require all denied
|
||||
</Directory>
|
||||
|
||||
<Directory {{ vhost.document_root }}
|
||||
Require all granted
|
||||
</Directory>
|
||||
<Directory {{ vhost.document_root }}/Private>
|
||||
Options indexes
|
||||
AuthName "stop"
|
||||
AuthType Basic
|
||||
AuthUserFile {{ apache_config_dir }}/passwd
|
||||
require valid-user
|
||||
</Directory>
|
||||
</VirtualHost>
|
||||
@@ -2,4 +2,4 @@
|
||||
- hosts: localhost
|
||||
remote_user: root
|
||||
roles:
|
||||
- myapache
|
||||
- tco.apache_abrd4
|
||||
@@ -0,0 +1,6 @@
|
||||
apache_package_name: apache2
|
||||
apache_service_name: apache2
|
||||
apache_log_dir: /var/log/apache2/
|
||||
apache_config_dir: /etc/apache2/sites-enabled/
|
||||
apache_group: www-data
|
||||
apache_user: www-data
|
||||
@@ -0,0 +1,2 @@
|
||||
---
|
||||
# vars file for tco.apache_abrd4
|
||||
@@ -0,0 +1,6 @@
|
||||
apache_package_name: httpd
|
||||
apache_service_name: httpd
|
||||
apache_log_dir: /var/log/httpd/
|
||||
apache_config_dir: /etc/httpd/conf.d/
|
||||
apache_group: apache
|
||||
apache_user: apache
|
||||
Reference in New Issue
Block a user