7 Commits

Author SHA1 Message Date
tom f02f3803e0 add solution 2026-06-25 10:26:45 +02:00
tom 450421e5f6 typo 2026-06-25 09:23:57 +02:00
tom 6e72c1389c base abr_day4 2026-06-25 09:23:23 +02:00
tom 069d12bf89 typo 2023-09-22 09:10:42 +02:00
tom 1724fa1365 on se base sur l'atelier 15_roles 2023-06-21 15:53:47 +02:00
tom 29de3e6b8b mise au point, rajout precisions dans instructions 2021-06-10 15:51:46 +02:00
tom 745f9b0598 solution 2021-03-17 22:58:14 +01:00
27 changed files with 216 additions and 164 deletions
+29
View File
@@ -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
View File
@@ -1,5 +1,6 @@
--- ---
- name: install apache via ansible playbook - name: install apache via ansible playbook
hosts: all hosts: cibles
roles: roles:
- apache - tco.apache_abrd4
-2
View File
@@ -1,2 +0,0 @@
---
# defaults file for myapache
-1
View File
@@ -1 +0,0 @@
<h1>hello World</h1>
-11
View File
@@ -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
-60
View File
@@ -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 }}"
-21
View File
@@ -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 %}
-4
View File
@@ -1,4 +0,0 @@
apache_package_name: apache2
apache_group_name: www-data
apache_service_name: apache2
apache_conf_dir: /etc/apache2/sites-enabled/
-3
View File
@@ -1,3 +0,0 @@
---
# vars file for myapache
http_port: 80
-4
View File
@@ -1,4 +0,0 @@
apache_package_name: httpd
apache_group_name: apache
apache_service_name: httpd
apache_conf_dir: /etc/httpd/conf.d
+15
View File
@@ -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
-12
View File
@@ -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
-2
View File
@@ -1,2 +0,0 @@
centos1.formation.opendoor.fr
debian1.formation.opendoor.fr
+29
View File
@@ -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 Role Name
========= =========
Rôle de deploiement apache sur une centos. Install apache,
1 seul vhost
Requirements Requirements
------------ ------------
@@ -13,12 +11,10 @@ None
Role Variables Role Variables
-------------- --------------
http_port: 80 apache_server_name: no default
servername: orsys.fr apache_document_root: no default
serveralias: "www.{{ servername }}"
documentroot: /var/www/html/orsys.fr see also files in vars subdirectory
accesslog: /var/log/httpd/access_orsys.fr_log
errorlog: /var/log/httpd/error_orsys.fr_log
Dependencies Dependencies
------------ ------------
@@ -32,7 +28,7 @@ Including an example of how to use your role (for instance, with variables passe
- hosts: servers - hosts: servers
roles: roles:
- { myapache } - tco.apache_abrd4
License License
------- -------
@@ -42,4 +38,4 @@ BSD
Author Information Author Information
------------------ ------------------
Thomas Constans <thomas@opendoor.fr> Thomas C <thomas@opendoor.fr>
+2
View File
@@ -0,0 +1,2 @@
---
# defaults file for tco.apache_abrd4
+6
View File
@@ -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: galaxy_info:
author: your name author: your name
description: your description description: your role description
company: your company (optional) company: your company (optional)
# If the issue tracker for your role is not on github, uncomment the # If the issue tracker for your role is not on github, uncomment the
# next line and provide a value # next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker # issue_tracker_url: http://example.com/issue/tracker
# Some suggested licenses: # Choose a valid license ID from https://spdx.org - some suggested licenses:
# - BSD (default) # - BSD-3-Clause (default)
# - MIT # - MIT
# - GPLv2 # - GPL-2.0-or-later
# - GPLv3 # - GPL-3.0-only
# - Apache # - Apache-2.0
# - CC-BY # - CC-BY-4.0
license: license (GPLv2, CC-BY, etc) 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. # If this a Container Enabled role, provide the minimum Ansible Container version.
# min_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: # platforms:
# - name: Fedora # - name: Fedora
+68
View File
@@ -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 }}
+1
View File
@@ -0,0 +1 @@
<h1>hello World {{ apache_server_name }} </h1>
+27
View File
@@ -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 - hosts: localhost
remote_user: root remote_user: root
roles: roles:
- myapache - tco.apache_abrd4
+6
View File
@@ -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
+2
View File
@@ -0,0 +1,2 @@
---
# vars file for tco.apache_abrd4
+6
View File
@@ -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