Skip to content

Instantly share code, notes, and snippets.

@linuxsimba
Last active May 31, 2017 13:48
Show Gist options
  • Select an option

  • Save linuxsimba/724419ab5a2f20be95ae93d5d9789cb9 to your computer and use it in GitHub Desktop.

Select an option

Save linuxsimba/724419ab5a2f20be95ae93d5d9789cb9 to your computer and use it in GitHub Desktop.
postgres failover and fallback scripts with Ansible Tower
- name: Gather facts
hosts: all
become: yes
- name: Stop Ansible Tower
hosts: tower
become: yes
tasks:
- name: Stop Ansible Tower
command: ansible-tower-service stop
- name: Failback PostgreSQL
hosts: database_bslave
become: yes
tasks:
- name: Stop PostgreSQL
service:
name: postgresql-9.4
state: stopped
- name: Remove recovery file
file:
path: /var/lib/pgsql/9.4/data/recovery.done
state: absent
- name: Update Ansible Tower configuration
hosts: tower
become: yes
tasks:
- name: Update Tower postgres.py
lineinfile:
dest: /etc/tower/conf.d/postgres.py
regexp: "^(.*'HOST':)"
line: "\\1 '{{ hostvars[groups['database'][0]].ansible_nodename }}',"
backrefs: yes
notify: start tower
- name: Start Ansible Tower
command: ansible-tower-service start
handlers:
- name: start tower
command: ansible-tower-service start
- include: psql-replication.yml
- name: Gather facts
hosts: all
become: yes
- name: Failover PostgreSQL
hosts: database_slave
become: yes
tasks:
- name: Promote secondary PostgreSQL server to primary
command: /usr/pgsql-9.4/bin/pg_ctl promote
become_user: postgres
environment:
PGDATA: /var/lib/pgsql/9.4/data
ignore_errors: yes
- name: Update Ansible Tower configuration
hosts: tower
become: yes
tasks:
- name: Update Tower postgres.py
lineinfile:
dest: /etc/tower/conf.d/postgres.py
regexp: "^(.*'HOST':)"
line: "\\1 '{{ hostvars[groups['database_slave'][0]].ansible_nodename }}',"
backrefs: yes
notify: restart tower
handlers:
- name: restart tower
command: ansible-tower-service restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment