From 7d5b2482198d9a5158dae108b7748703971d2c3c Mon Sep 17 00:00:00 2001 From: Teodoro Cook Date: Sun, 5 May 2024 12:29:52 -0600 Subject: [PATCH] Refactor service mask logic (#31) --- molecule/common/verify.yml | 17 +++++++++++ molecule/hold/converge.yml | 1 + molecule/hold/verify.yml | 1 + poetry.lock | 62 +++++++++++++++++++------------------- pyproject.toml | 2 +- tasks/service.yml | 14 +++++++++ tasks/vars.yml | 2 +- 7 files changed, 66 insertions(+), 33 deletions(-) diff --git a/molecule/common/verify.yml b/molecule/common/verify.yml index 901e3b0..498bae3 100644 --- a/molecule/common/verify.yml +++ b/molecule/common/verify.yml @@ -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 }}" @@ -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-', '') }}" @@ -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' diff --git a/molecule/hold/converge.yml b/molecule/hold/converge.yml index cfcf4e5..8e3e66a 100644 --- a/molecule/hold/converge.yml +++ b/molecule/hold/converge.yml @@ -1,3 +1,4 @@ +--- - name: Deploy PostgreSQL servers hosts: all become: true diff --git a/molecule/hold/verify.yml b/molecule/hold/verify.yml index 74635b3..b0c4477 100644 --- a/molecule/hold/verify.yml +++ b/molecule/hold/verify.yml @@ -1,3 +1,4 @@ +--- - name: Verify PostgreSQL deployment hosts: all become: true diff --git a/poetry.lock b/poetry.lock index be45c18..ed35bf9 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. [[package]] name = "ansible" @@ -56,19 +56,19 @@ resolvelib = ">=0.5.3,<1.1.0" [[package]] name = "ansible-lint" -version = "24.2.0" +version = "24.2.3" description = "Checks playbooks for practices and behavior that could potentially be improved" optional = false python-versions = ">=3.10" files = [ - {file = "ansible-lint-24.2.0.tar.gz", hash = "sha256:6bc5d6273f33711ec6d370dfe5fdbe97a64b4c36c2a7a19a249401326eb03616"}, - {file = "ansible_lint-24.2.0-py3-none-any.whl", hash = "sha256:755f369c6baf601b09c4931a4e6bd0df3d79c54eb519f093e5db6ab52ad03e0c"}, + {file = "ansible_lint-24.2.3-py3-none-any.whl", hash = "sha256:8aad0de454e0e803b0416a9c6e409302a8eb93c0f3866a98f035b96647e72213"}, + {file = "ansible_lint-24.2.3.tar.gz", hash = "sha256:2ec67cc1719f56e63b1dfc3728d0c2c4142e531f4452f6ae31f618438d59469a"}, ] [package.dependencies] ansible-compat = ">=4.1.11" ansible-core = ">=2.12.0" -black = ">=22.8.0" +black = ">=24.3.0" filelock = ">=3.3.0" jsonschema = ">=4.10.0" packaging = ">=21.3" @@ -82,7 +82,7 @@ yamllint = ">=1.30.0" [package.extras] docs = ["mkdocs-ansible (>=0.2.0)", "pipdeptree (>=2.7.1)"] -test = ["black", "coverage-enable-subprocess", "coverage[toml] (>=6.4.4)", "jmespath", "license-expression (>=30.1.1)", "mypy", "netaddr", "psutil", "pylint", "pytest (>=7.2.2)", "pytest-mock", "pytest-plus (>=0.6)", "pytest-xdist (>=2.1.0)", "ruamel-yaml-clib", "ruamel.yaml (>=0.17.31)", "types-jsonschema", "types-pyyaml"] +test = ["black", "coverage-enable-subprocess", "coverage[toml] (>=6.4.4)", "jmespath", "license-expression (>=30.3.0)", "mypy", "netaddr", "psutil", "pylint", "pytest (>=7.2.2)", "pytest-mock", "pytest-plus (>=0.6)", "pytest-xdist (>=2.1.0)", "ruamel-yaml-clib", "ruamel.yaml (>=0.17.31)", "types-jsonschema", "types-pyyaml"] [[package]] name = "attrs" @@ -104,33 +104,33 @@ tests-no-zope = ["cloudpickle", "hypothesis", "mypy (>=1.1.1)", "pympler", "pyte [[package]] name = "black" -version = "23.9.1" +version = "24.4.2" description = "The uncompromising code formatter." optional = false python-versions = ">=3.8" files = [ - {file = "black-23.9.1-cp310-cp310-macosx_10_16_arm64.whl", hash = "sha256:d6bc09188020c9ac2555a498949401ab35bb6bf76d4e0f8ee251694664df6301"}, - {file = "black-23.9.1-cp310-cp310-macosx_10_16_universal2.whl", hash = "sha256:13ef033794029b85dfea8032c9d3b92b42b526f1ff4bf13b2182ce4e917f5100"}, - {file = "black-23.9.1-cp310-cp310-macosx_10_16_x86_64.whl", hash = "sha256:75a2dc41b183d4872d3a500d2b9c9016e67ed95738a3624f4751a0cb4818fe71"}, - {file = "black-23.9.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:13a2e4a93bb8ca74a749b6974925c27219bb3df4d42fc45e948a5d9feb5122b7"}, - {file = "black-23.9.1-cp310-cp310-win_amd64.whl", hash = "sha256:adc3e4442eef57f99b5590b245a328aad19c99552e0bdc7f0b04db6656debd80"}, - {file = "black-23.9.1-cp311-cp311-macosx_10_16_arm64.whl", hash = "sha256:8431445bf62d2a914b541da7ab3e2b4f3bc052d2ccbf157ebad18ea126efb91f"}, - {file = "black-23.9.1-cp311-cp311-macosx_10_16_universal2.whl", hash = "sha256:8fc1ddcf83f996247505db6b715294eba56ea9372e107fd54963c7553f2b6dfe"}, - {file = "black-23.9.1-cp311-cp311-macosx_10_16_x86_64.whl", hash = "sha256:7d30ec46de88091e4316b17ae58bbbfc12b2de05e069030f6b747dfc649ad186"}, - {file = "black-23.9.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:031e8c69f3d3b09e1aa471a926a1eeb0b9071f80b17689a655f7885ac9325a6f"}, - {file = "black-23.9.1-cp311-cp311-win_amd64.whl", hash = "sha256:538efb451cd50f43aba394e9ec7ad55a37598faae3348d723b59ea8e91616300"}, - {file = "black-23.9.1-cp38-cp38-macosx_10_16_arm64.whl", hash = "sha256:638619a559280de0c2aa4d76f504891c9860bb8fa214267358f0a20f27c12948"}, - {file = "black-23.9.1-cp38-cp38-macosx_10_16_universal2.whl", hash = "sha256:a732b82747235e0542c03bf352c126052c0fbc458d8a239a94701175b17d4855"}, - {file = "black-23.9.1-cp38-cp38-macosx_10_16_x86_64.whl", hash = "sha256:cf3a4d00e4cdb6734b64bf23cd4341421e8953615cba6b3670453737a72ec204"}, - {file = "black-23.9.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:cf99f3de8b3273a8317681d8194ea222f10e0133a24a7548c73ce44ea1679377"}, - {file = "black-23.9.1-cp38-cp38-win_amd64.whl", hash = "sha256:14f04c990259576acd093871e7e9b14918eb28f1866f91968ff5524293f9c573"}, - {file = "black-23.9.1-cp39-cp39-macosx_10_16_arm64.whl", hash = "sha256:c619f063c2d68f19b2d7270f4cf3192cb81c9ec5bc5ba02df91471d0b88c4c5c"}, - {file = "black-23.9.1-cp39-cp39-macosx_10_16_universal2.whl", hash = "sha256:6a3b50e4b93f43b34a9d3ef00d9b6728b4a722c997c99ab09102fd5efdb88325"}, - {file = "black-23.9.1-cp39-cp39-macosx_10_16_x86_64.whl", hash = "sha256:c46767e8df1b7beefb0899c4a95fb43058fa8500b6db144f4ff3ca38eb2f6393"}, - {file = "black-23.9.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:50254ebfa56aa46a9fdd5d651f9637485068a1adf42270148cd101cdf56e0ad9"}, - {file = "black-23.9.1-cp39-cp39-win_amd64.whl", hash = "sha256:403397c033adbc45c2bd41747da1f7fc7eaa44efbee256b53842470d4ac5a70f"}, - {file = "black-23.9.1-py3-none-any.whl", hash = "sha256:6ccd59584cc834b6d127628713e4b6b968e5f79572da66284532525a042549f9"}, - {file = "black-23.9.1.tar.gz", hash = "sha256:24b6b3ff5c6d9ea08a8888f6977eae858e1f340d7260cf56d70a49823236b62d"}, + {file = "black-24.4.2-cp310-cp310-macosx_10_9_x86_64.whl", hash = "sha256:dd1b5a14e417189db4c7b64a6540f31730713d173f0b63e55fabd52d61d8fdce"}, + {file = "black-24.4.2-cp310-cp310-macosx_11_0_arm64.whl", hash = "sha256:8e537d281831ad0e71007dcdcbe50a71470b978c453fa41ce77186bbe0ed6021"}, + {file = "black-24.4.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:eaea3008c281f1038edb473c1aa8ed8143a5535ff18f978a318f10302b254063"}, + {file = "black-24.4.2-cp310-cp310-win_amd64.whl", hash = "sha256:7768a0dbf16a39aa5e9a3ded568bb545c8c2727396d063bbaf847df05b08cd96"}, + {file = "black-24.4.2-cp311-cp311-macosx_10_9_x86_64.whl", hash = "sha256:257d724c2c9b1660f353b36c802ccece186a30accc7742c176d29c146df6e474"}, + {file = "black-24.4.2-cp311-cp311-macosx_11_0_arm64.whl", hash = "sha256:bdde6f877a18f24844e381d45e9947a49e97933573ac9d4345399be37621e26c"}, + {file = "black-24.4.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:e151054aa00bad1f4e1f04919542885f89f5f7d086b8a59e5000e6c616896ffb"}, + {file = "black-24.4.2-cp311-cp311-win_amd64.whl", hash = "sha256:7e122b1c4fb252fd85df3ca93578732b4749d9be076593076ef4d07a0233c3e1"}, + {file = "black-24.4.2-cp312-cp312-macosx_10_9_x86_64.whl", hash = "sha256:accf49e151c8ed2c0cdc528691838afd217c50412534e876a19270fea1e28e2d"}, + {file = "black-24.4.2-cp312-cp312-macosx_11_0_arm64.whl", hash = "sha256:88c57dc656038f1ab9f92b3eb5335ee9b021412feaa46330d5eba4e51fe49b04"}, + {file = "black-24.4.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:be8bef99eb46d5021bf053114442914baeb3649a89dc5f3a555c88737e5e98fc"}, + {file = "black-24.4.2-cp312-cp312-win_amd64.whl", hash = "sha256:415e686e87dbbe6f4cd5ef0fbf764af7b89f9057b97c908742b6008cc554b9c0"}, + {file = "black-24.4.2-cp38-cp38-macosx_10_9_x86_64.whl", hash = "sha256:bf10f7310db693bb62692609b397e8d67257c55f949abde4c67f9cc574492cc7"}, + {file = "black-24.4.2-cp38-cp38-macosx_11_0_arm64.whl", hash = "sha256:98e123f1d5cfd42f886624d84464f7756f60ff6eab89ae845210631714f6db94"}, + {file = "black-24.4.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:48a85f2cb5e6799a9ef05347b476cce6c182d6c71ee36925a6c194d074336ef8"}, + {file = "black-24.4.2-cp38-cp38-win_amd64.whl", hash = "sha256:b1530ae42e9d6d5b670a34db49a94115a64596bc77710b1d05e9801e62ca0a7c"}, + {file = "black-24.4.2-cp39-cp39-macosx_10_9_x86_64.whl", hash = "sha256:37aae07b029fa0174d39daf02748b379399b909652a806e5708199bd93899da1"}, + {file = "black-24.4.2-cp39-cp39-macosx_11_0_arm64.whl", hash = "sha256:da33a1a5e49c4122ccdfd56cd021ff1ebc4a1ec4e2d01594fef9b6f267a9e741"}, + {file = "black-24.4.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:ef703f83fc32e131e9bcc0a5094cfe85599e7109f896fe8bc96cc402f3eb4b6e"}, + {file = "black-24.4.2-cp39-cp39-win_amd64.whl", hash = "sha256:b9176b9832e84308818a99a561e90aa479e73c523b3f77afd07913380ae2eab7"}, + {file = "black-24.4.2-py3-none-any.whl", hash = "sha256:d36ed1124bb81b32f8614555b34cc4259c3fbc7eec17870e8ff8ded335b58d8c"}, + {file = "black-24.4.2.tar.gz", hash = "sha256:c872b53057f000085da66a19c55d68f6f8ddcac2642392ad3a355878406fbd4d"}, ] [package.dependencies] @@ -142,7 +142,7 @@ platformdirs = ">=2" [package.extras] colorama = ["colorama (>=0.4.3)"] -d = ["aiohttp (>=3.7.4)"] +d = ["aiohttp (>=3.7.4)", "aiohttp (>=3.7.4,!=3.9.0)"] jupyter = ["ipython (>=7.8.0)", "tokenize-rt (>=3.2.0)"] uvloop = ["uvloop (>=0.15.2)"] @@ -1229,4 +1229,4 @@ dev = ["doc8", "flake8", "flake8-import-order", "rstcheck[sphinx]", "sphinx"] [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "f65f31ddd53979d05746061969ff6a01db5ffbfedd0e241ad3ec9ebedd3b0e63" +content-hash = "1c0f022292de13089a4011a49ff57c33316cd60833ca976347794e39d5995cda" diff --git a/pyproject.toml b/pyproject.toml index 6e0be02..5088a02 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" diff --git a/tasks/service.yml b/tasks/service.yml index 9a45b74..d47c71a 100644 --- a/tasks/service.yml +++ b/tasks/service.yml @@ -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 diff --git a/tasks/vars.yml b/tasks/vars.yml index f2a02a3..c5898e3 100644 --- a/tasks/vars.yml +++ b/tasks/vars.yml @@ -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) }}"