Skip to content

Instantly share code, notes, and snippets.

@aht
Created March 6, 2013 08:45
Show Gist options
  • Save aht/5097702 to your computer and use it in GitHub Desktop.
Save aht/5097702 to your computer and use it in GitHub Desktop.
ansible postgres playbook with PostGIS on Ubuntu, setting up postgis_template and creating databases from this postgis_template
# Based on https://github.com/ansible/ansible/blob/devel/examples/playbooks/postgresql.yml
---
- hosts: postgres.vm
sudo: yes
tasks:
- name: ensure latest postgres & postgis packages are latest
action: apt pkg=$item update_cache=yes state=latest
with_items:
- postgresql-server-dev-9.1
- postgresql-contrib-9.1
- postgresql-9.1-postgis
- phppgadmin
- python-psycopg2
- name: ensure postgresql server is running
action: service name=postgresql state=started
- hosts: postgres
sudo: yes
sudo_user: postgres
vars_files:
- vars.yml
tasks:
# http://trac.osgeo.org/postgis/wiki/UsersWikiPostGIS15Ubuntu1110src
- name: ensure postgis_template database exists
action: postgresql_db db=postgis_template state=present
register: createdb_postgis_template
- name: make postgis_template a template
action: command psql -d postgis_template -c "UPDATE pg_database SET datistemplate=true WHERE datname='postgis_template';"
only_if: '${createdb_postgis_template.changed}'
- name: create extension uuid-ossp on postgis_template
action: command psql -d postgis_template -c 'CREATE EXTENSION "uuid-ossp";'
only_if: '${createdb_postgis_template.changed}'
- name: run the postgis SQL scripts
action: command psql -d postgis_template -f $item
only_if: '${createdb_postgis_template.changed}'
with_items:
- /usr/share/postgresql/9.1/contrib/postgis-1.5/postgis.sql
- /usr/share/postgresql/9.1/contrib/postgis-1.5/spatial_ref_sys.sql
- /usr/share/postgresql/9.1/contrib/postgis_comments.sql
- name: ensure postgres.conf is correct
action: copy src=./files/postgresql.conf dest=/etc/postgresql/9.1/main/postgresql.conf
notify: restart postgresql
- name: ensure pg_hba.conf is correct
action: copy src=./files/pg_hba.conf dest=/etc/postgresql/9.1/main/pg_hba.conf
notify: restart postgresql
# non-idempotent, but the postgresql_db module does not support template
- name: ensure named databases exist
action: command createdb -T postgis_template $item
with_items: $db_names
ignore_errors: True
- name: ensure user "$db_user" has access to those databases
action: postgresql_user db=$item user=$db_user password="$db_password" priv=ALL state=present
with_items: $db_names
handlers:
- name: restart postgresql
action: service name=postgresql state=restarted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment