Skip to content

PostgreSQL Module Playbook


This playbook provides direct access to an Ansible Module. The following Modules are supported by this playbook:

For each of the above modules, variable module_database must be defined. This variable is a dictionary of the options as are used by the module. The documentation for each module can be found by click on the module name above.

Playbook AWX / Ansible Automation Platform Template Import

This playbook includes the AWX feature where it imports the playbook as job templates in to AWX / Ansible Automation Platform. The following job templates that will be created:

  • Database/PostgreSQL/Database/Create Create a Database

  • Database/PostgreSQL/Database/Delete Delete a Database

  • Database/PostgreSQL/Database/Dump Dump a database to a .sql file

  • Database/PostgreSQL/Database/Import Import a database from a .sql file

  • Database/PostgreSQL/Database/Rename Rename a database

On import to AWX / Ansible Automation Platform a credential type will also be created, playbook/mysql/login that can be used to supply the required secrets to log in to the database.

Module definitions

Each module definition is detailed below under the tab matching the module name. This directly shows what is or is not supported.

community-mysql-mysql_db.yaml
---

- name: Connection Validation - Assumed Socket
  ansible.builtin.assert:
    that:
      - module_database.host.socket is defined
    msg: "If no host DNS or IP Address is specified, you must supply the socket to use for the connection"
  when: module_database.host.name is not defined


- name: Variable Validation - Dump Database
  ansible.builtin.assert:
    that:
      - module_database.target is defined
    msg: "to dump a database, you must specify the path to save the dump"
  when: module_database.state == 'dump'


- name: Variable Validation - Import Database
  ansible.builtin.assert:
    that:
      - module_database.target is defined
    msg: "to Import a database, you must specify the path to the dump (.sql) file"
  when: module_database.state == 'restore'


- name: Variable Validation - Rename Database
  ansible.builtin.assert:
    that:
      - module_database.target is defined
    msg: "To rename a database, option target must be specified"
  when: module_database.state == 'rename'


- name: Install PostgreSQL Dependencies
  ansible.builtin.apt:
    name: "{{ item }}"
    state: present
    install_recommends: false
  become: true
  loop: "{{ apps }}"
  vars:
    apps:
      - postgresql-client


- name: Install PostgreSQL required PIP Modules
  ansible.builtin.pip:
    name: "{{ item }}"
    state: present
    extra_args: '--index-url https://gitlab.com/api/v4/projects/45741845/packages/pypi/simple'
  become: true
  loop: "{{ modules }}"
  failed_when: false # This task can fail as python 3.10 packages not avail
  vars:
    modules:
      - psycopg2


- name: "{{ task_name_module | default('MariaDB Database Action') }}"
  community.postgresql.postgresql_db:
    login_user: "{{ nfc_pb_postgres_username }}"
    login_password: "{{ nfc_pb_postgres_password }}"

    login_host: "{{ module_database.host.name }}"
    port: "{{ module_database.host.port | default(omit) }}"

    login_unix_socket: "{{ module_database.host.socket | default(omit) }}"

    name: "{{ module_database.name }}"
    state: "{{ module_database.state }}"

    force: "{{ module_database.force | default(omit) }}"
    target: "{{ module_database.target | default(omit) }}"

  become: "{{ module_database.become | default(omit) }}"
  delegate_to: "{{ module_database.delegate_to | default(omit) }}"
  no_log: "{{ module_database.no_log | default(false) | bool }}"
  retries: "{{ module_database.retries | default(omit) }}"

About:

This page forms part of our Project Ansible Playbooks.

Page Metadata
Version: ToDo: place files short git commit here
Date Created: 2024-01-14
Date Edited: 2024-01-14

Contribution:

Would You like to contribute to our Ansible Playbooks project? You can assist in the following ways:

 

ToDo: Add the page list of contributors