Skip to content
Snippets Groups Projects
Verified Commit 318583b5 authored by Chris Croome's avatar Chris Croome
Browse files

test fix start service for molecule

parent cc153f02
No related branches found
No related tags found
No related merge requests found
Pipeline #33594 failed
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
skip_list: skip_list:
- command-instead-of-module - command-instead-of-module
- key-order[task] - key-order[task]
- name[template]
- package-latest - package-latest
# vim: syntax=yaml # vim: syntax=yaml
... ...
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
- systemd_unit is defined - systemd_unit is defined
quiet: "{% if ansible_verbosity == 0 %}true{% else %}false{% endif %}" quiet: "{% if ansible_verbosity == 0 %}true{% else %}false{% endif %}"
- name: Include the package install tasks when defined - name: "Include the package install tasks when defined for {{ systemd_unit.name }}"
ansible.builtin.include_tasks: pkg_present.yml ansible.builtin.include_tasks: pkg_present.yml
loop: "{{ systemd_unit.pkgs }}" loop: "{{ systemd_unit.pkgs }}"
loop_control: loop_control:
...@@ -37,13 +37,13 @@ ...@@ -37,13 +37,13 @@
- systemd_unit.pkgs | length > 0 - systemd_unit.pkgs | length > 0
# Checks needed when this file is the role entry point # Checks needed when this file is the role entry point
- name: Include check tasks - name: "Include check tasks for {{ systemd_unit.name }}"
ansible.builtin.include_tasks: checks.yml ansible.builtin.include_tasks: checks.yml
when: >- when: >-
( systemd_unit_files_existing is not defined ) or ( systemd_unit_files_existing is not defined ) or
( systemd_unit_files_enabled is not defined ) ( systemd_unit_files_enabled is not defined )
- name: Include the systemd unit file absent tasks - name: "Include the systemd unit file absent tasks for {{ systemd_unit.name }}"
ansible.builtin.include_tasks: file_absent.yml ansible.builtin.include_tasks: file_absent.yml
loop: "{{ systemd_unit.files }}" loop: "{{ systemd_unit.files }}"
loop_control: loop_control:
...@@ -53,7 +53,7 @@ ...@@ -53,7 +53,7 @@
- systemd_file.state is defined - systemd_file.state is defined
- systemd_file.state == "absent" - systemd_file.state == "absent"
- name: Include the systemd unit file edited, present or templated tasks - name: "Include the systemd unit file edited, present or templated tasks for {{ systemd_unit.name }}"
ansible.builtin.include_tasks: file_present.yml ansible.builtin.include_tasks: file_present.yml
loop: "{{ systemd_unit.files }}" loop: "{{ systemd_unit.files }}"
loop_control: loop_control:
...@@ -66,7 +66,7 @@ ...@@ -66,7 +66,7 @@
- name: Set the unit state and restart it when the unit file exists - name: Set the unit state and restart it when the unit file exists
block: block:
- name: Verify the service when systemd_unit.verify is defined - name: "Verify the service when systemd_unit.verify is defined for {{ systemd_unit.name }}"
ansible.builtin.command: "systemd-analyze verify {{ systemd_unit.verify }}" ansible.builtin.command: "systemd-analyze verify {{ systemd_unit.verify }}"
check_mode: false check_mode: false
changed_when: false changed_when: false
...@@ -75,10 +75,10 @@ ...@@ -75,10 +75,10 @@
- name: Verify the service when systemd_unit.verify is not defined - name: Verify the service when systemd_unit.verify is not defined
block: block:
- name: Include the checks - name: "Include the checks for {{ systemd_unit.name }}"
ansible.builtin.include_tasks: checks.yml ansible.builtin.include_tasks: checks.yml
- name: Verify the service when it is in the list of existing unit files after appending .service - name: "Verify the service when it is in the list of existing unit files after appending .service for {{ systemd_unit.name }}"
ansible.builtin.command: "systemd-analyze verify {{ systemd_unit.name }}.service --man=false" ansible.builtin.command: "systemd-analyze verify {{ systemd_unit.name }}.service --man=false"
check_mode: false check_mode: false
changed_when: false changed_when: false
...@@ -86,7 +86,7 @@ ...@@ -86,7 +86,7 @@
- systemd_unit.name is not regex('[.]service$') - systemd_unit.name is not regex('[.]service$')
- systemd_unit_verify in systemd_unit_files_enabled - systemd_unit_verify in systemd_unit_files_enabled
- name: Verify the service when it is in the list of existing unit files - name: "Verify the service when it is in the list of existing unit files for {{ systemd_unit.name }}"
ansible.builtin.command: "systemd-analyze verify {{ systemd_unit.name }}" ansible.builtin.command: "systemd-analyze verify {{ systemd_unit.name }}"
check_mode: false check_mode: false
changed_when: false changed_when: false
...@@ -101,7 +101,7 @@ ...@@ -101,7 +101,7 @@
- systemd_file_changed is defined - systemd_file_changed is defined
- systemd_file_changed | bool - systemd_file_changed | bool
- name: Unit not enabled and stopped - name: "Systemd {{ systemd_unit.name }} unit not enabled and stopped"
ansible.builtin.systemd_service: ansible.builtin.systemd_service:
name: "{{ systemd_unit.name }}" name: "{{ systemd_unit.name }}"
enabled: false enabled: false
...@@ -110,7 +110,7 @@ ...@@ -110,7 +110,7 @@
- systemd_unit.state is defined - systemd_unit.state is defined
- systemd_unit.state == "stopped" - systemd_unit.state == "stopped"
- name: Systemd unit enabled - name: "Systemd unit {{ systemd_unit.name }} enabled"
ansible.builtin.systemd_service: ansible.builtin.systemd_service:
name: "{{ systemd_unit.name }}" name: "{{ systemd_unit.name }}"
enabled: true enabled: true
...@@ -118,15 +118,13 @@ ...@@ -118,15 +118,13 @@
( systemd_unit.state is not defined ) or ( systemd_unit.state is not defined ) or
( systemd_unit.state == "enabled" ) ( systemd_unit.state == "enabled" )
- name: Systemd unit started - name: "Systemd {{ systemd_unit.name }} unit started tasks included"
ansible.builtin.systemd_service: ansible.builtin.include_tasks: unit_started.yml
name: "{{ systemd_unit.name }}"
state: started
when: >- when: >-
( systemd_unit.state is not defined ) or ( systemd_unit.state is not defined ) or
( systemd_unit.state == "enabled" ) ( systemd_unit.state == "enabled" )
- name: Systemd unit restarted - name: "Systemd {{ systemd_unit.name }} unit restarted"
ansible.builtin.systemd_service: ansible.builtin.systemd_service:
service: "{{ systemd_unit.name }}" service: "{{ systemd_unit.name }}"
state: restarted state: restarted
...@@ -136,73 +134,6 @@ ...@@ -136,73 +134,6 @@
( ( systemd_file_changed is defined ) and ( ( systemd_file_changed is defined ) and
( systemd_file_changed | bool ) ) ( systemd_file_changed | bool ) )
- name: Check that the service is running
ansible.builtin.service_facts:
vars:
systemd_service_jpq: 'ansible_facts.services.["{{ systemd_unit.name }}.service"]|[0]'
register: systemd_service_facts
until: systemd_service_facts | community.general.json_query(systemd_service_jpq) == "running"
retries: 20
delay: 3
- name: Debug the service status
ansible.builtin.debug:
msg: "{{ systemd_service_facts | community.general.json_query(systemd_service_jpq) }}"
verbosity: 3
vars:
systemd_service_jpq: 'ansible_facts.services.["{{ systemd_unit.name }}.service"]|[0]'
- name: Systemd daemon-reload when the service is not running
ansible.builtin.systemd_service:
daemon_reload: true
vars:
systemd_service_jpq: 'ansible_facts.services.["{{ systemd_unit.name }}.service"]|[0]'
changed_when: true
when: (systemd_service_facts | community.general.json_query(systemd_service_jpq)).state != "running"
- name: Start the service using the command module if it is not running
ansible.builtin.command: "service {{ systemd_unit.name }} start"
vars:
systemd_service_jpq: 'ansible_facts.services.["{{ systemd_unit.name }}.service"]|[0]'
changed_when: true
when: (systemd_service_facts | community.general.json_query(systemd_service_jpq)).state != "running"
- name: Check that the service is running
ansible.builtin.service_facts:
vars:
systemd_service_jpq: 'ansible_facts.services.["{{ systemd_unit.name }}.service"]|[0]'
register: systemd_service_facts
until: systemd_service_facts | community.general.json_query(systemd_service_jpq) == "running"
retries: 20
delay: 3
- name: Debug the service status
ansible.builtin.debug:
msg: "{{ systemd_service_facts | community.general.json_query(systemd_service_jpq) }}"
verbosity: 3
vars:
systemd_service_jpq: 'ansible_facts.services.["{{ systemd_unit.name }}.service"]|[0]'
- name: Check that the service is enabled and running
ansible.builtin.assert:
that:
- (systemd_service_facts | community.general.json_query(systemd_service_jpq)).status == "enabled"
- (systemd_service_facts | community.general.json_query(systemd_service_jpq)).state == "running"
quiet: true
fail_msg: >-
The {{ systemd_unit.name }}.service
{% if (systemd_service_facts | community.general.json_query(systemd_service_jpq)).status != "enabled" %}status
is {{ (systemd_service_facts | community.general.json_query(systemd_service_jpq)).status }}
when it should be enabled
{% endif %}{% if (systemd_service_facts | community.general.json_query(systemd_service_jpq)).state != "running" %}state
is {{ (systemd_service_facts | community.general.json_query(systemd_service_jpq)).state }} when
it should be running{% endif %}
vars:
systemd_service_jpq: 'ansible_facts.services.["{{ systemd_unit.name }}.service"]|[0]'
when: >-
( ( systemd_unit.state is not defined ) or
( systemd_unit.state == "enabled" ) )
when: systemd_file_state.stat.exists | bool when: systemd_file_state.stat.exists | bool
tags: tags:
......
# Copyright 2022-2024 Chris Croome
#
# This file is part of the Webarchitects systemd Ansible role.
#
# The Webarchitects systemd Ansible role is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
#
# The Webarchitects Ansible role is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with the Webarchitects systemd Ansible role. If not, see <https://www.gnu.org/licenses/>.
---
- name: Start systemd unit
block:
- name: "Systemd unit {{ systemd_unit.name }} started"
ansible.builtin.systemd_service:
name: "{{ systemd_unit.name }}"
state: started
register: systemd_service_started
until:
- systemd_service_started.status.SubState is defined
- systemd_service_started.status.SubState == "running"
- systemd_service_started.status.Result is defined
- systemd_service_started.status.Result == "success"
retries: 20
delay: 3
- name: "Debug the results from starting the {{ systemd_unit.name }} systemd service"
ansible.builtin.debug:
var: systemd_service_started
verbosity: "{% if ansible_check_mode | bool or ansible_diff_mode | bool %}1{% else %}2{% endif %}"
- name: "Systemd daemon-reload for {{ systemd_unit.name }}"
ansible.builtin.systemd_service:
daemon_reload: true
when:
- systemd_service_started.status.NeedDaemonReload is defined
- systemd_service_started.status.NeedDaemonReload == "yes"
- name: Check the state of services
ansible.builtin.service_facts:
register: systemd_service_facts
- name: Check that the service is enabled and running
ansible.builtin.assert:
that:
- (systemd_service_facts | community.general.json_query(systemd_service_jpq)).status == "enabled"
- (systemd_service_facts | community.general.json_query(systemd_service_jpq)).state == "running"
quiet: true
fail_msg: >-
The {{ systemd_unit.name }}.service
{% if (systemd_service_facts | community.general.json_query(systemd_service_jpq)).status != "enabled" %}status
is {{ (systemd_service_facts | community.general.json_query(systemd_service_jpq)).status }}
when it should be enabled
{% endif %}{% if (systemd_service_facts | community.general.json_query(systemd_service_jpq)).state != "running" %}state
is {{ (systemd_service_facts | community.general.json_query(systemd_service_jpq)).state }} when
it should be running{% endif %}
vars:
systemd_service_jpq: 'ansible_facts.services.["{{ systemd_unit.name }}.service"]|[0]'
tags:
- systemd
...
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment