Skip to content

This initializes a PostgreSQL database with a Ansible playbook

Notifications You must be signed in to change notification settings

rgl/ansible-init-postgres

Repository files navigation

About

Build status

This initializes a PostgreSQL database with a Ansible playbook.

Usage

To use in a Docker Compose file do as the example docker-compose.yml file.

To use in a Ansible Playbook Kubernetes Job do as:

# try kubectl logs -n yugabytedb jobs/initialize-dex-database
# NB as an alternative/complement see https://github.com/coderanger/migrations-operator.
- name: Initialize the dex database
  block:
    # NB unfortunately we need this delete step because using force:true fails
    #    with:
    #       field is immutable","field":"spec.template"
    - name: Delete the dex database initialization job
      kubernetes.core.k8s:
        namespace: yugabytedb
        state: absent
        definition:
          apiVersion: batch/v1
          kind: Job
          metadata:
            name: initialize-dex-database
      changed_when: false
    - name: Initialize the dex database
      # TODO derive changed_when from the ansible playbook execution result.
      kubernetes.core.k8s:
        namespace: yugabytedb
        wait: yes
        wait_condition:
          type: Complete
          status: "True"
        definition:
          apiVersion: batch/v1
          kind: Job
          metadata:
            name: initialize-dex-database
          spec:
            template:
              spec:
                containers:
                  - name: init
                    image: ghcr.io/rgl/ansible-init-postgres:main
                    env:
                      # see https://www.postgresql.org/docs/16/libpq-envars.html
                      - name: PGHOST
                        value: yb-tservers.yugabytedb
                      - name: PGPORT
                        value: '5433'
                      - name: PGUSER
                        value: yugabyte
                      - name: DEX_PASSWORD
                        value: 66964843358242dbaaa7778d8477c288
                    command:
                      - ansible-init-postgres
                      - |
                        {%- raw -%}
                        - hosts: localhost
                          connection: local
                          vars:
                            database: dex
                            users:
                              - name: dex
                                password: "{{ lookup('ansible.builtin.env', 'DEX_PASSWORD') }}"
                                privs: all
                          tasks:
                            - name: Create database
                              community.postgresql.postgresql_db:
                                name: '{{ database }}'
                            - name: Create user
                              community.postgresql.postgresql_user:
                                name: '{{ item.name }}'
                                password: '{{ item.password }}'
                              loop: '{{ users }}'
                            - name: Grant user database permissions
                              community.postgresql.postgresql_privs:
                                type: database
                                database: '{{ database }}'
                                roles: '{{ item.name }}'
                                privs: '{{ item.privs }}'
                                grant_option: false
                              loop: '{{ users }}'
                        {%- endraw -%}
                restartPolicy: Never
            backoffLimit: 1

Lint the example.yml playbook:

./ansible-lint.sh --offline --parseable example.yml

List this repository dependencies (and which have newer versions):

export GITHUB_COM_TOKEN='YOUR_GITHUB_PERSONAL_TOKEN'
./renovate.sh

About

This initializes a PostgreSQL database with a Ansible playbook

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages