Initial commit

This commit is contained in:
Chl 2020-09-29 02:57:15 +02:00
commit e3639cc94c
18 changed files with 680 additions and 0 deletions

View file

@ -0,0 +1,6 @@
---
- name: Install packages
apt:
name: "{{ packages_list }}"
when: ansible_os_family == "Debian"

View file

@ -0,0 +1,25 @@
---
- name: Create database user
postgresql_user:
name: osm
password: osm
become: yes
become_user: postgres
become_method: su
- name: Create database
postgresql_db:
name: osm
owner: osm
become: yes
become_user: postgres
become_method: su
- name: Add PostGIS extension to database
postgresql_ext:
db: osm
name: postgis
become: yes
become_user: postgres
become_method: su

View file

@ -0,0 +1,66 @@
---
# We offer the possibility to host the basemaps repo in files/basemaps
# as a git submodule.
# note : currently, if this method is used, any remote modification is erased.
- name: Check if the basemaps repo has been cloned in files/
run_once: true
local_action: stat path={{ role_path }}/files/basemaps/.git
register: basemaps_locally_available
- name: Copy local basemaps to host
copy:
src: "{{ role_path}}/files/basemaps/"
dest: /srv/osm/basemaps/
when: basemaps_locally_available.stat.exists
- name: Clone basemaps
git:
repo: https://github.com/mapserver/basemaps
dest: /srv/osm/basemaps
when: basemaps_locally_available.stat.exists != True
# This is usually a one shot. Unless no .zip file is in /data, we skip the whole thing.
# Simply remove the .zip files in basemaps/data/ if you need to refresh it.
- name: Check if basemaps/data has been initialized
find:
paths: /srv/osm/basemaps/data
patterns: '*.zip'
register: basemaps_data_zip_list
- name: Upload the local shapefiles, if available, to save some bandwidth to the OSM project
copy:
src: "{{ item }}"
dest: /srv/osm/basemaps/data/
with_fileglob:
# Let's get to the root of the playbook
- "../../../*land-polygons*.zip"
- "*land-polygons*.zip"
when: basemaps_data_zip_list.matched == 0
- name: Initialize basemaps/data
command: make
args:
chdir: /srv/osm/basemaps/data/
when: basemaps_data_zip_list.matched == 0
# Customize Makefile
- name: Customize basemaps Makefile
template:
src: "{{ item }}"
dest: /srv/osm/basemaps/Makefile
backup: no
with_first_found:
- files:
- "basemaps/Makefile.{{ ansible_fqdn }}.j2"
- "basemaps/Makefile.j2"
skip: true
register: tmp
- name: Generate basemaps
# It seems Make doesn't see a makefile's modification worth a rebuild, so we force it a bit.
command: "{{ tmp.changed | ternary('make --always-make', 'make') }}"
args:
chdir: /srv/osm/basemaps/

View file

@ -0,0 +1,49 @@
---
# Upload .osm.pbf files found at the root of the playbook or in the /files role's subfolder
- name: Imposm - Upload any local .osm.pbf files
copy:
src: "{{ item }}"
dest: /srv/osm/imposm/
with_fileglob:
# Let's get to the root of the playbook
- "../../../*.osm.pbf"
- "*.osm.pbf"
# If the .osm.pbf files are not newer than the cache, we skip the whole thing.
- name: Imposm - Check if there is already a cache
stat:
path: /srv/osm/imposm/imposm_relations.cache
register: tmp
- name: Imposm - List pbf files waiting to be imported
command: find /srv/osm/imposm -iname '*.osm.pbf' {{ tmp.stat.exists | ternary('-newer imposm_relations.cache', '') }}
args:
chdir: /srv/osm/imposm
register: imposm_pbf_list
changed_when: False
check_mode: no
- name: Imposm - Import each new files
command: imposm --mapping-file=/srv/osm/basemaps/imposm-mapping.py --proj=EPSG:3857 --read --merge-cache {{ item|quote }}
args:
chdir: /srv/osm/imposm
loop: "{{ imposm_pbf_list.stdout_lines }}"
- name: Imposm - Write to database
command: imposm --mapping-file=/srv/osm/basemaps/imposm-mapping.py --proj=EPSG:3857 --connection=postgis://osm:osm@localhost:5432/osm --write
args:
chdir: /srv/osm/imposm
when: imposm_pbf_list.stdout_lines
- name: Imposm - Optimze database
command: imposm --mapping-file=/srv/osm/basemaps/imposm-mapping.py --proj=EPSG:3857 --connection=postgis://osm:osm@localhost:5432/osm --optimize
args:
chdir: /srv/osm/imposm
when: imposm_pbf_list.stdout_lines
- name: Imposm - Switch new data to production
command: imposm --mapping-file=/srv/osm/basemaps/imposm-mapping.py --proj=EPSG:3857 --connection=postgis://osm:osm@localhost:5432/osm --deploy-production-tables
args:
chdir: /srv/osm/imposm
when: imposm_pbf_list.stdout_lines

View file

@ -0,0 +1,75 @@
---
# MapCache
- name: Configure MapCache
template:
src: "{{ item }}"
dest: /srv/osm/mapcache/mapcache.xml
backup: no
with_first_found:
- "mapcache/mapcache.xml.{{ ansible_fqdn }}.j2"
- "mapcache/mapcache.xml.{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.j2"
- "mapcache/mapcache.xml.{{ ansible_distribution }}.j2"
- "mapcache/mapcache.xml.j2"
notify:
- handlerRestartApache
# Apache customizations
- name: Apache - Activate CGI module
apache2_module:
state: present
name: cgid
notify:
- handlerRestartApache
# Kinda paranoiac but I prefer the CGI not being freely accessible
- name: Apache - Restrict access to CGI
lineinfile:
path: /etc/apache2/conf-available/serve-cgi-bin.conf
regexp: '^(\s+)Require '
line: '\1Require local'
backrefs: yes
notify:
- handlerRestartApache
# We gather some extra data for the vhost template
- name: Apache - Set a fact about whether SSL is enabled or not - 1
apache2_module:
state: present
name: ssl
check_mode: yes
register: tmp
- name: Apache - Set a fact about whether SSL is enabled or not - 2
set_fact:
apache_module_ssl_enabled: "{{ not tmp.changed }}"
- name: Apache - Set up vhost
template:
src: "{{ item }}"
dest: "{{ mapcache_vhost_filename }}"
backup: no
with_first_found:
- "apache2/vhost.{{ ansible_fqdn }}.j2"
- "apache2/vhost.{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.j2"
- "apache2/vhost.{{ ansible_distribution }}.j2"
- "apache2/vhost.j2"
notify:
- handlerRestartApache
# We try not to generate a 'changed' when using a2ensite, so we check beforehand.
- name: Apache - Check if vhost is enabled
command: find -L /etc/apache2/sites-enabled/ -samefile {{ mapcache_vhost_filename | quote }}
register: tmp
changed_when: False
check_mode: no
- name: Apache - Enable vhost
command: a2ensite {{ mapcache_vhost_filename | basename | quote }}
when: not tmp.stdout
notify:
- handlerRestartApache
- name: Apache - Check config
command: apache2ctl configtest
changed_when: False

View file

@ -0,0 +1,38 @@
---
- name: Gather os specific variables
block:
- include_vars: "{{ item }}"
with_first_found: # Files are in 'vars/' subdirectory and contain a one level dictionary 'var: value'
- "{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.yml"
- "{{ ansible_distribution }}.yml"
- "{{ ansible_os_family }}.yml"
#- "defaults.yml" # No defaults for this role at the moment.
rescue:
- fail:
msg: "Unexpected OS/Distribution. Create and fill vars/{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.yml"
# Dedicated directories
- name: Imposm dedicated directory
file:
path: /srv/osm/imposm
state: directory
- name: MapCache dedicated directory
file:
path: /srv/osm/mapcache
state: directory
# Packages installation
- import_tasks: 10_package-install.yml
# Database creation
- import_tasks: 20_database-creation.yml
# Retrieve the OSM basemaps
- import_tasks: 30_basemaps.yml
# Import with imposm
- import_tasks: 40_imposm.yml
# Mapcache and Apache
- import_tasks: 50_mapcache-and-apache.yml