4 Commits

Author SHA1 Message Date
tom 920af07166 serveralias as list 2024-12-13 14:24:34 +01:00
tom 3bc092ada5 make it work on almlinux and debian, really 2023-11-21 14:40:05 +01:00
tom db9f049e68 with_items -> loop et adaptation nouvelle infra 2021-11-30 15:05:35 +01:00
tom 6d279c75bb solution 2021-03-17 22:55:16 +01:00
27 changed files with 164 additions and 216 deletions
-29
View File
@@ -1,29 +0,0 @@
## 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"
+2 -3
View File
@@ -1,6 +1,5 @@
--- ---
- name: install apache via ansible playbook - name: install apache via ansible playbook
hosts: cibles hosts: all
roles: roles:
- tco.apache_abrd4 - apache
@@ -1,7 +1,9 @@
Role Name Role Name
========= =========
Install apache, Rôle de deploiement apache sur une centos.
1 seul vhost
Requirements Requirements
------------ ------------
@@ -11,10 +13,12 @@ None
Role Variables Role Variables
-------------- --------------
apache_server_name: no default http_port: 80
apache_document_root: no default servername: orsys.fr
serveralias: "www.{{ servername }}"
see also files in vars subdirectory documentroot: /var/www/html/orsys.fr
accesslog: /var/log/httpd/access_orsys.fr_log
errorlog: /var/log/httpd/error_orsys.fr_log
Dependencies Dependencies
------------ ------------
@@ -28,7 +32,7 @@ Including an example of how to use your role (for instance, with variables passe
- hosts: servers - hosts: servers
roles: roles:
- tco.apache_abrd4 - { myapache }
License License
------- -------
@@ -38,4 +42,4 @@ BSD
Author Information Author Information
------------------ ------------------
Thomas C <thomas@opendoor.fr> Thomas Constans <thomas@opendoor.fr>
+2
View File
@@ -0,0 +1,2 @@
---
# defaults file for myapache
+1
View File
@@ -0,0 +1 @@
<h1>hello World</h1>
+11
View File
@@ -0,0 +1,11 @@
---
# handlers file for myapache
- name: reload httpd
service:
name: "{{ apache_service_name }}"
state: reloaded
- name: reload firewalld
service:
name: firewalld
state: reloaded
@@ -1,31 +1,36 @@
galaxy_info: galaxy_info:
author: your name author: your name
description: your role description description: your 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
# Choose a valid license ID from https://spdx.org - some suggested licenses: # Some suggested licenses:
# - BSD-3-Clause (default) # - BSD (default)
# - MIT # - MIT
# - GPL-2.0-or-later # - GPLv2
# - GPL-3.0-only # - GPLv3
# - Apache-2.0 # - Apache
# - CC-BY-4.0 # - CC-BY
license: license (GPL-2.0-or-later, MIT, etc) license: license (GPLv2, CC-BY, etc)
min_ansible_version: 2.1 min_ansible_version: 1.2
# 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:
# #
# Provide a list of supported platforms, and for each platform a list of versions. # platforms is a list of platforms, and each platform has a name and 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
@@ -49,4 +54,4 @@ galaxy_info:
dependencies: [] dependencies: []
# List your role dependencies here, one per line. Be sure to remove the '[]' above, # List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list. # if you add dependencies to this list.
+60
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 }}"
+21
View File
@@ -0,0 +1,21 @@
{% 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 %}
@@ -2,4 +2,4 @@
- hosts: localhost - hosts: localhost
remote_user: root remote_user: root
roles: roles:
- tco.apache_abrd4 - myapache
+4
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/
+3
View File
@@ -0,0 +1,3 @@
---
# vars file for myapache
http_port: 80
+4
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
-15
View File
@@ -1,15 +0,0 @@
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
@@ -0,0 +1,11 @@
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
@@ -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
View File
@@ -0,0 +1,2 @@
centos1.formation.opendoor.fr
debian1.formation.opendoor.fr
-29
View File
@@ -1,29 +0,0 @@
---
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/
-2
View File
@@ -1,2 +0,0 @@
---
# defaults file for tco.apache_abrd4
-6
View File
@@ -1,6 +0,0 @@
---
# handlers file for tco.apache_abrd4
- name: restart apache
ansible.builtin.service:
name: "{{ apache_service_name }}"
state: restarted
-68
View File
@@ -1,68 +0,0 @@
---
# 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
@@ -1 +0,0 @@
<h1>hello World {{ apache_server_name }} </h1>
-27
View File
@@ -1,27 +0,0 @@
{% 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>
-6
View File
@@ -1,6 +0,0 @@
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
@@ -1,2 +0,0 @@
---
# vars file for tco.apache_abrd4
-6
View File
@@ -1,6 +0,0 @@
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