Skip to content

Commit

Permalink
Refactor service mask logic (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyphreak authored May 5, 2024
1 parent d1b8d9b commit 7d5b248
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 33 deletions.
17 changes: 17 additions & 0 deletions molecule/common/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
- name: Set yum service facts
ansible.builtin.set_fact:
postgresql_service_release: "{{ _release }}"
postgresql_service_name: "{{ _name }}"
postgresql_service: "{{ _service }}"
vars:
_packages: "{{ postgresql_server_packages }}"
Expand All @@ -61,6 +62,7 @@
- name: Set yum service facts
ansible.builtin.set_fact:
postgresql_service_release: "{{ _release }}"
postgresql_service_name: "{{ _name }}"
postgresql_service: "{{ _service }}"
vars:
_release: "{{ postgresql_server_packages | first | regex_replace('^postgresql-', '') }}"
Expand All @@ -84,6 +86,21 @@
_stopped: "{{ (ansible_os_family == 'RedHat') | ternary('inactive', 'stopped') }}"
_svc_state: "{{ (_state == 'running') | ternary('running', _stopped) }}"

- name: Verify service mask
when: postgresql_service_masked | default('false') | bool
block:
- name: Attempt mask service start
ansible.builtin.shell: >-
systemctl start {{ postgresql_service_name }}
register: _masked_start
ignore_errors: true

- name: Verify service mask
ansible.builtin.assert:
that:
- _masked_start is failed
- _masked_start.stderr | regex_search('.* ' + postgresql_service_name + ' is masked.')

- name: Stop verification for install-only scenarios
ansible.builtin.meta: end_play
when: (postgresql_service | first).state != 'running'
Expand Down
1 change: 1 addition & 0 deletions molecule/hold/converge.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
- name: Deploy PostgreSQL servers
hosts: all
become: true
Expand Down
1 change: 1 addition & 0 deletions molecule/hold/verify.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
- name: Verify PostgreSQL deployment
hosts: all
become: true
Expand Down
62 changes: 31 additions & 31 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ python = "^3.11"

[tool.poetry.group.dev.dependencies]
ansible = "^9.3.0"
ansible-lint = { version = "24.2.0", markers = "platform_system != 'Windows'" }
ansible-lint = { version = "24.2.3", markers = "platform_system != 'Windows'" }
molecule = "^24.2.0"
molecule-plugins = {extras = ["docker"], version = "^23.5.3"}
yamllint = "^1.32.0"
Expand Down
14 changes: 14 additions & 0 deletions tasks/service.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@
- name: Set installation facts
ansible.builtin.include_tasks: vars.yml

- name: Unmask PostgreSQL service
ansible.builtin.command:
cmd: "systemctl unmask {{ _postgresql_service_name }}"
when: not (postgresql_service_masked | bool)
changed_when: false
tags: skip_ansible_lint

- name: Manage PostgreSQL service
ansible.builtin.systemd_service:
name: "{{ _postgresql_service_name }}"
state: "{{ postgresql_service_state }}"
enabled: "{{ postgresql_service_enabled }}"
masked: "{{ postgresql_service_masked }}"

- name: Mask PostgreSQL service
ansible.builtin.command:
cmd: "systemctl mask {{ _postgresql_service_name }}"
when: postgresql_service_masked | bool
changed_when: false
tags: skip_ansible_lint
2 changes: 1 addition & 1 deletion tasks/vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

- name: Set global facts
ansible.builtin.set_fact:
_postgresql_package_all: "{{ _package_server + _package_client}}"
_postgresql_package_all: "{{ _package_server + _package_client }}"
_postgresql_package_server: "{{ _package_server }}"
_postgresql_package_client: "{{ _package_client }}"
_postgresql_package_extra: "{{ postgresql_package_extra | default(_default_package_extra) }}"
Expand Down

0 comments on commit 7d5b248

Please sign in to comment.