Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hryamzik committed Jul 17, 2015
0 parents commit 4863135
Show file tree
Hide file tree
Showing 14 changed files with 503 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Vars

postgres_host

# Links
http://www.postgresql.org/message-id/CADKbJJXP-eKGf=ntcHRJgTqiN-XhogmD2RJsRdCt4TcW7Mp8=w@mail.gmail.com
http://docs.pgbarman.org/#getting_started
22 changes: 22 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
postgresql_apt_key_id: ACCC4CF8
postgresql_apt_key_url: "https://www.postgresql.org/media/keys/ACCC4CF8.asc"
postgresql_apt_repository: 'deb http://apt.postgresql.org/pub/repos/apt/ {{ansible_distribution_release}}-pgdg main'

postgres_host_name: "{{ postgres_host }}"
postgres_host_description: "{{ postgres_host_name }} PostgreSQL Database for {{ postgres_host }}"
postgres_version: "9.3"
postgres_include_dir: conf.d
# postgres_include_dir_regex: '{{ postgres_include_dir | replace(".","\.") }}'
postgres_include_dir_fullpath: "/etc/postgresql/{{ postgres_version }}/main/{{ postgres_include_dir }}"

barman_ip: "{{ ansible_default_ipv4.address }}"
postgres_barman_pass: "{{ 'barman' | md5 }}"

# wal_level archive or hot_standby
postgres_wal_level: hot_standby

postgres_confirm_service_restart: yes

postgres_restart_params:
archive_mode: "on"
wal_level: "{{ postgres_wal_level }}"
46 changes: 46 additions & 0 deletions examples/Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

defaultbox = "ubuntu/trusty64"
box = ENV['BOX'] || defaultbox

ENV['ANSIBLE_ROLES_PATH'] = "../../"

Vagrant.configure(2) do |config|

config.vm.box = box

config.vm.define "barman" do |barman_cfg|
barman_cfg.vm.hostname = "barman"
barman_cfg.vm.network "private_network", type: "dhcp"
barman_cfg.vm.provider :virtualbox do |v|
v.name = "barman"
# v.gui = true
end
end

config.vm.define "postgres" do |postgres_cfg|
postgres_cfg.vm.hostname = "postgres"
postgres_cfg.vm.network "private_network", type: "dhcp"
postgres_cfg.vm.provider :virtualbox do |v|
v.memory = 4096
v.name = "postgres"
# v.gui = true
end
end

config.vm.provision :ansible do |ansible|
ansible.playbook = "vagrant.yml"
ansible.sudo = true
# ansible.tags = ['debug']
ansible.groups = {
"vagrant" => ["barman", "postgres"],
}
ansible.extra_vars = {
ansible_ssh_user: 'vagrant',
hbase_standalone: true,
}

end

end
8 changes: 8 additions & 0 deletions examples/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[defaults]
retry_files_save_path = /tmp/retry/
ansible_managed = Ansible managed: file modified on %Y-%m-%d %H:%M:%S by {uid} on {host}
hostfile = ./vagrant.py
roles_path = ../../

[ssh_connection]
ssh_args = -o StrictHostKeyChecking=no
39 changes: 39 additions & 0 deletions examples/barman.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
- hosts: all

- name: set up postgres
gather_facts: no
hosts: postgres
sudo: yes
tasks:

- name: install PostgreSQL, postgresql-contrib and postgres ansible management requirements
apt: name={{ item }} cache_valid_time=3600 update_cache=yes
with_items:
- postgresql-9.3
- postgresql-contrib-9.3
- libpq-dev
- python-psycopg2

- name: set listen address to *
lineinfile: dest="/etc/postgresql/9.3/main/postgresql.conf"
regexp="(^\s*)#*(\s*listen_addresses\s*=\s*')[^']*('.*$)"
backrefs=on
line="\1\2*\3"
backup=yes

- service: name=postgresql state=started
# - postgresql_db: name=barmantest
# sudo_user: postgres
# - postgresql_user: db=barmantest name=barman password=barman
# sudo_user: postgres
#

- hosts: barman
sudo: yes
gather_facts: no
vars:
postgres_host: "{{ hostvars.postgres.ansible_eth1.ipv4.address }}"
# postgres_host_name: postgres_vagrant
barman_ip: "{{ hostvars.barman.ansible_eth1.ipv4.address }}"
roles:
- barman
47 changes: 47 additions & 0 deletions examples/vagrant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/python
import json
import string
import os
import argparse
import glob

parser = argparse.ArgumentParser(description='Process ansible inventory options')
parser.add_argument("-l", "--list", action='store_true', help="list of groups" )
parser.add_argument("-H", "--host", help="dictionary of variables for host")

args = parser.parse_args()

def prettyprint(string):
print json.dumps(string, indent=4, sort_keys=True)


def getClients():
clientListString = os.popen("grep ssh .vagrant/provisioners/ansible/inventory/vagrant_ansible_inventory|tr '=' ' '").read()
clients = {}
for clientString in clientListString.split('\n'):
clientVars = clientString.split()
if len(clientVars) == 5:
c={}
name, _, c['ansible_ssh_host'], _, c['ansible_ssh_port'] = clientVars
clients[name] = c
return clients

clients=getClients()

if args.list:
hostlist = {
"_meta" : {
"hostvars": clients
},
"vpnclients": clients.keys(),
# "setupme" : setupmeHosts
}
prettyprint(hostlist)

elif args.host:
try:
prettyprint( clients[args.host] )
except:
pass
else:
prettyprint(clients)
8 changes: 8 additions & 0 deletions examples/vagrant.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- hosts: vagrant
sudo: yes
roles:
- vagrant-simple
tasks:

- name: now run 'ansible-playbook barman.yml'
debug: msg="now run 'ansible-playbook barman.yml'"
7 changes: 7 additions & 0 deletions handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
- name: delegate postgresql reload
service: name=postgresql state=reloaded
delegate_to: "{{ postgres_host }}"

- name: delegate postgres restart
service: name=postgresql state=restarted
delegate_to: "{{ postgres_host }}"
74 changes: 74 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
- name: Barman | Add PostgreSQL repository apt-key
apt_key:
id: "{{ postgresql_apt_key_id }}"
url: "{{ postgresql_apt_key_url }}"
state: present
when: postgresql_apt_key_url and postgresql_apt_key_id and ansible_distribution_version|version_compare(14.04, '<')

- name: Barman | Add PostgreSQL repository
apt_repository:
repo: "{{ postgresql_apt_repository }}"
state: present
when: postgresql_apt_repository and ansible_distribution_version|version_compare(14.04, '<')

- name: Barman | Install Barman
apt:
name: "{{ item }}"
state: "present"
with_items:
- barman
# - postgresql-client

- name: create /etc/barman.d
file: dest=/etc/barman.d state=directory

- name: deploy global configuration
template: src=barman.conf dest=/etc/barman.conf

- name: deploy server specific configuration
template: src=barman.d/template.conf dest=/etc/barman.d/{{ postgres_host_name }}.conf

- name: setup postgres server and access
include: postgres.yml

- name: check if there's a backup
shell: barman show-backup {{ postgres_host_name }} latest
register: latest_backup
changed_when: latest_backup.rc != 0
failed_when: false
sudo_user: barman

- name: initiate backup for server if there're still 0 of them
shell: barman backup {{ postgres_host_name }} | logger -t barman_{{ postgres_host_name }}
sudo_user: barman
when: latest_backup.changed

- name: check requirements
shell: "barman check {{ postgres_host_name }}"
changed_when: false
sudo_user: barman

- name: remove default cron to add log support
lineinfile: dest=/etc/cron.d/barman
state=absent
regexp='^\s*\*\s+\*\s+\*\s+\*\s+\*\s+barman\s*\[\s*-x\s*/usr/bin/barman\s*\]\s*&&\s*/usr/bin/barman\s*-q\s*cron\s?$'
tags: cron

- name: replace default cron to add log support
cron:
name: "barman cron"
cron_file: barman
user: barman
job: "[ -x /usr/bin/barman ] cron 2>&1 | /usr/bin/logger -t barman"
tags: cron

- name: add daily backups for all servers
cron:
hour: 4
minute: 4
name: "barman backup all"
cron_file: barman
user: barman
job: "[ -x /usr/bin/barman ] backup all 2>&1 | /usr/bin/logger -t barman"
tags: cron
83 changes: 83 additions & 0 deletions tasks/postgres.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
- name: install ansible management requirements
apt: name={{ item }} cache_valid_time=3600 update_cache=yes
with_items:
- libpq-dev
- python-psycopg2
delegate_to: "{{ postgres_host }}"

- name: read configuration parameters
shell: barman show-server all
changed_when: false
register: config_all_raw
tags: psql

- name: import configuration to a fact
set_fact:
config_all: "{{ config_all_raw.stdout.replace('\t',' ') | from_yaml }}"
tags: psql

- name: check incoming_wals_directory
debug: var=config_all['Server main'].incoming_wals_directory
tags: psql

- name: create {{ postgres_include_dir_fullpath }}
file: dest={{ postgres_include_dir_fullpath }} state=directory
delegate_to: "{{ postgres_host }}"
tags: psql

- name: enable {{ postgres_include_dir }} configs inclusion
lineinfile: dest="/etc/postgresql/{{ postgres_version }}/main/postgresql.conf"
regexp="(^\s*)#*(\s*include_dir\s*=\s*')[^']*('.*$)"
backrefs=on
line='\1\2{{ postgres_include_dir }}\3'
backup=yes
delegate_to: "{{ postgres_host }}"
tags: psql

- name: enable Continuous WAL archiving
template: dest={{ postgres_include_dir_fullpath }}/03_barman.conf src=postgres/barman.conf
delegate_to: "{{ postgres_host }}"
register: delegate_postgres_reload1
tags: psql

- name: Allow connections
lineinfile: dest=/etc/postgresql/9.3/main/pg_hba.conf
state=present
regexp="\s*host\s+all\s+barman\s+{{ barman_ip }}\/32"
line="host all barman {{ barman_ip }}/32 md5"
register: delegate_postgres_reload2
delegate_to: "{{ postgres_host }}"
tags: psql

- name: delegate postgresql reload
service: name=postgresql state=reloaded
delegate_to: "{{ postgres_host }}"
when: delegate_postgres_reload1.changed or delegate_postgres_reload2.changed
tags: psql

- name: check if restart is required
command: psql -At -c "SELECT current_setting('{{ item.key }}') <> '{{ item.value }}';"
with_dict: postgres_restart_params
register: postgres_restart_required
always_run: yes
ignore_errors: yes
changed_when: postgres_restart_required.stdout == 't' or postgres_restart_required|failed
delegate_to: "{{ postgres_host }}"
sudo_user: postgres
tags: psql

- name: pause if restart requires confirmation
pause: prompt="Postgres service restart required and it is going to be performed in the next task!"
when: postgres_restart_required.changed and postgres_confirm_service_restart
tags: psql

- name: delegate postgresql restart
service: name=postgresql state=restarted
delegate_to: "{{ postgres_host }}"
when: postgres_restart_required.changed
tags: psql

- include: users.yml
tags: users

# - debug: msg="'{{ postgres_include_dir_regex }}', '{{ postgres_include_dir }}'"
Loading

0 comments on commit 4863135

Please sign in to comment.