Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic package install tasks #2

Merged
merged 24 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .ansible-lint-ignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# This file contains ignores rule violations for ansible-lint
meta/main.yml schema[meta]
handlers/main.yml args[module]
4 changes: 0 additions & 4 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@
name: lint

on:

pull_request:
branches:
- master

defaults:

run:
working-directory: 'nephelaiio.postgresql'

jobs:

lint:

name: lint
runs-on: ubuntu-latest
steps:
Expand Down
11 changes: 4 additions & 7 deletions .github/workflows/molecule.yml
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
---
name: molecule

on:

pull_request:
branches:
- master
schedule:
- cron: "0 5 * * 5"

defaults:

run:
working-directory: 'nephelaiio.postgresql'

jobs:

molecule:

name: molecule
runs-on: ubuntu-latest
strategy:
matrix:
scenario:
- name: default
- name: install
image:
- name: ubuntu2204
command: /lib/systemd/systemd
- name: ubuntu2004
command: /lib/systemd/systemd
- name: debian12
command: /lib/systemd/systemd
- name: debian11
command: /lib/systemd/systemd
- name: centos8
- name: rockylinux9
command: /usr/lib/systemd/systemd
steps:
- name: Check out the codebase
Expand Down
7 changes: 2 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,19 @@
# See: https://github.com/marketplace/actions/publish-ansible-role-to-galaxy
# See: https://github.com/ansible/galaxy/issues/46

name: Release
name: elease
on:
push:
tags:
- "*"

defaults:

run:
working-directory: 'nephelaiio.postgresql'

jobs:

release:

name: Release
name: release
runs-on: ubuntu-latest
steps:
- name: Check out the codebase.
Expand Down
7 changes: 7 additions & 0 deletions .talismanrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fileignoreconfig:
- filename: poetry.lock
ignore_detectors: [filecontent]
- filename: molecule/common/verify.yml
ignore_detectors: [filecontent]
- filename: templates/postgres.conf.j2
ignore_detectors: [filecontent]
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
.PHONY: all ${MAKECMDGOALS}

MOLECULE_SCENARIO ?= default
MOLECULE_DOCKER_IMAGE ?= ubuntu2004
MOLECULE_SCENARIO ?= install
MOLECULE_DOCKER_IMAGE ?= ubuntu2204
MOLECULE_DOCKER_COMMAND ?= /lib/systemd/systemd
GALAXY_API_KEY ?=
GITHUB_REPOSITORY ?= $$(git config --get remote.origin.url | cut -d: -f 2 | cut -d. -f 1)
GITHUB_ORG = $$(echo ${GITHUB_REPOSITORY} | cut -d/ -f 1)
Expand All @@ -11,11 +12,13 @@ REQUIREMENTS = requirements.yml
all: install version lint test

test: lint
MOLECULE_DOCKER_IMAGE=${MOLECULE_DOCKER_IMAGE} \
MOLECULE_DOCKER_COMMAND=${MOLECULE_DOCKER_COMMAND} \
poetry run molecule test -s ${MOLECULE_SCENARIO}

install:
@type poetry >/dev/null || pip3 install poetry
@poetry install
@poetry install --no-root

lint: install
poetry run yamllint .
Expand All @@ -32,7 +35,9 @@ collections:
requirements: roles collections

dependency create prepare converge idempotence side-effect verify destroy login reset:
MOLECULE_DOCKER_IMAGE=${MOLECULE_DOCKER_IMAGE} poetry run molecule $@ -s ${MOLECULE_SCENARIO}
MOLECULE_DOCKER_IMAGE=${MOLECULE_DOCKER_IMAGE} \
MOLECULE_DOCKER_COMMAND=${MOLECULE_DOCKER_COMMAND} \
poetry run molecule $@ -s ${MOLECULE_SCENARIO}

rebuild: destroy prepare create

Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ Please refer to the [defaults file](/defaults/main.yml) for an up to date list o

## Dependencies

By default this role does not depend on any external roles. If any such dependency is required please [add them](/meta/main.yml) according to [the documentation](http://docs.ansible.com/ansible/playbooks_roles.html#role-dependencies)
Role execution requires filters defined in [nephelaiio.plugins](https://galaxy.ansible.com/ui/repo/published/nephelaiio/plugins/) collection

## Example Playbook

```
- hosts: servers
roles:
- role: nephelaiio.postgresql
postgresql_package_state: latest
```

## Testing

Expand Down
36 changes: 32 additions & 4 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,34 @@
---
_postgresql_package_default:
default:
- postgresql
postgresql_release: 16
postgresql_package_state: present
postgresql_service_state: started
postgresql_service_enabled: true
postgresql_db_init: true
postgresql_db_path: /var/lib/postgresql
postgresql_user: postgres
postgresql_group: postgres
postgresql_datadir: "/var/lib/postgresql/data"

postgresql_state: present
__postgresql_package_name:
debian:
- "postgresql-{{ postgresql_release }}"
- "postgresql-client-{{ postgresql_release }}"
redhat:
- "postgresql{{ postgresql_release }}-server"
- "postgresql{{ postgresql_release }}"
__postgresql_service_name:
debian: "postgresql@{{ postgresql_release }}-main"
redhat: "postgresql-{{ postgresql_release }}"
__postgresql_bindir:
redhat: "/usr/pgsql-{{ postgresql_release }}/bin"
debian: "/usr/lib/postgresql/{{ postgresql_release }}/bin"
__postgresql_configfile:
redhat: "{{ postgresql_datadir }}/postgresql.conf"
debian: "/etc/postgresql/{{ postgresql_release }}/main/postgresql.conf"

__postgresql_os_search:
- "{{ ansible_distribution | lower }}-{{ ansible_distribution_release | lower }}"
- "{{ ansible_distribution | lower }}-{{ ansible_distribution_major_version }}"
- "{{ ansible_distribution | lower }}"
- "{{ ansible_os_family | lower }}"
- "default"
10 changes: 10 additions & 0 deletions handlers/main.yml
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
---
- name: Restart PostgreSQL
ansible.builtin.service:
name: "{{ _postgresql_service_name }}"
state: "restarted"
listen: postgresql_restart

- name: Reload daemon configuration

Check warning on line 8 in handlers/main.yml

View workflow job for this annotation

GitHub Actions / lint

args[module]

missing required arguments: name

Check warning on line 8 in handlers/main.yml

View workflow job for this annotation

GitHub Actions / molecule (install, ubuntu2204, /lib/systemd/systemd)

args[module]

missing required arguments: name

Check warning on line 8 in handlers/main.yml

View workflow job for this annotation

GitHub Actions / molecule (install, ubuntu2004, /lib/systemd/systemd)

args[module]

missing required arguments: name

Check warning on line 8 in handlers/main.yml

View workflow job for this annotation

GitHub Actions / molecule (install, debian12, /lib/systemd/systemd)

args[module]

missing required arguments: name

Check warning on line 8 in handlers/main.yml

View workflow job for this annotation

GitHub Actions / molecule (install, debian11, /lib/systemd/systemd)

args[module]

missing required arguments: name

Check warning on line 8 in handlers/main.yml

View workflow job for this annotation

GitHub Actions / molecule (install, rockylinux9, /usr/lib/systemd/systemd)

args[module]

missing required arguments: name
ansible.builtin.service:
daemon_reload: true
listen: daemon_reload
2 changes: 1 addition & 1 deletion meta/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ galaxy_info:
galaxy_tags: ['postgresql']

dependencies:
- role: nephelaiio.plugins
- name: nephelaiio.postgresql_repo
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
---
- name: Converge

hosts: all

become: true
roles:

- nephelaiio.plugins
- nephelaiio.postgresql
7 changes: 2 additions & 5 deletions molecule/default/prepare.yml → molecule/common/prepare.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
---
- name: Prepare

hosts: all

gather_facts: true

become: true

roles:
- nephelaiio.postgresql_repo
tasks:

- name: Update apt package cache
ansible.builtin.apt:
update_cache: yes
Expand Down
73 changes: 73 additions & 0 deletions molecule/common/verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
- name: Verify
hosts: all
gather_facts: true
become: true
tasks:
- name: Gather service facts
ansible.builtin.service_facts:

- name: Gather package facts
ansible.builtin.package_facts:

- name: Set yum package facts
ansible.builtin.set_fact:
postgresql_server_packages: "{{ packages.keys() | select('match', '^postgresql[0-9]+-server$') }}"
postgresql_client_packages: "{{ packages.keys() | select('match', '^postgresql[0-9]+$') }}"
when: ansible_os_family == 'RedHat'

- name: Set apt package facts
ansible.builtin.set_fact:
postgresql_server_packages: "{{ packages.keys() | select('match', '^postgresql-[0-9]+$') }}"
postgresql_client_packages: "{{ packages.keys() | select('match', '^postgresql-client-[0-9]+$') }}"
when: ansible_os_family == 'Debian'

- name: Verify packages
block:
- name: Verify deployed packages
ansible.builtin.assert:
that:
- postgresql_server_packages | length == 1
- postgresql_client_packages | length == 1

rescue:
- name: Debug server package list
ansible.builtin.debug:
var: postgresql_server_packages

- name: Debug client package list
ansible.builtin.debug:
var: postgresql_client_packages

- name: Fail verification
ansible.builtin.fail:

- name: Set yum service facts
ansible.builtin.set_fact:
postgresql_service_release: "{{ _release }}"
postgresql_service: "{{ _service }}"
vars:
_packages: "{{ postgresql_server_packages }}"
_release: "{{ _packages | first | regex_replace('^postgresql([0-9]+)-server', '\\1') }}"
_name: "postgresql-{{ _release }}.service"
_service: "{{ [_name] | map('extract', services) }}"
when: ansible_os_family == 'RedHat'

- name: Set yum service facts
ansible.builtin.set_fact:
postgresql_service_release: "{{ _release }}"
postgresql_service: "{{ _service }}"
vars:
_release: "{{ postgresql_server_packages | first | regex_replace('^postgresql-', '') }}"
_name: "postgresql@{{ _release }}-main.service"
_service: "{{ [_name] | map('extract', services) }}"
when: ansible_os_family == 'Debian'

- name: Verify package deployment
ansible.builtin.assert:
that:
- postgresql_service | length == 1
- (postgresql_service | first).state == 'running'
- (postgresql_service | first).status == _status
vars:
_status: "{{ (ansible_os_family == 'RedHat') | ternary('enabled', 'active') }}"
24 changes: 0 additions & 24 deletions molecule/default/molecule.yml

This file was deleted.

10 changes: 0 additions & 10 deletions molecule/default/verify.yml

This file was deleted.

46 changes: 46 additions & 0 deletions molecule/install/molecule.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
dependency:
name: galaxy
options:
role-file: requirements.yml
requirements-file: requirements.yml
driver:
name: docker
platforms:
- name: postgresql-install-01
image: "geerlingguy/docker-${MOLECULE_DOCKER_IMAGE:-ubuntu2204}-ansible:latest"
command: ${MOLECULE_DOCKER_COMMAND:-""}
cgroupns_mode: host
privileged: true
pre_build_image: true
volumes:
- /sys/fs/cgroup:/sys/fs/cgroup:rw
tmpfs:
- /tmp
- /opt
provisioner:
name: ansible
playbooks:
prepare: ../common/prepare.yml
converge: ../common/converge.yml
verify: ../common/verify.yml
verifier:
name: ansible
scenario:
prepare_sequence:
- prepare
create_sequence:
- create
converge_sequence:
- converge
destroy_sequence:
- destroy
cleanup_sequence:
- cleanup
test_sequence:
- dependency
- create
- prepare
- converge
- side_effect
- verify
Loading
Loading