Find all logical errors, inconsistencies and bugs in following ansible code (tasks in role context):
---
# tasks file for service-actions
- name: Load service descriptors
ansible.builtin.slurp:
src: "{{ service_descriptors_file }}"
register: loaded_service_descriptors
changed_when: false
- name: Decode and parse service descriptors
ansible.builtin.set_fact:
service_descriptors: "{{ loaded_service_descriptors.content | b64decode | from_yaml }}"
- name: Validate service descriptors structure
ansible.builtin.fail:
msg: "Invalid service descriptors format - missing 'services' list"
when:
- service_descriptors.services is undefined
- name: debug service descriptors
ansible.builtin.debug:
msg: "appname: {{ appname }}, action: {{ saction }}, descriptors {{ service_descriptors.services | selectattr('name', 'equalto', appname) | selectattr('service_group', 'in', group_names) }}"
- name: Extract target service matching name and group
ansible.builtin.set_fact:
service: >-
{{ service_descriptors.services
| selectattr('name', 'equalto', appname)
| selectattr('service_group', 'in', group_names)
| list | first }}
when: service_descriptors.services is defined
- name: Validate service exists
ansible.builtin.fail:
msg: "Service '{{ appname }}' not found or not part of host's service groups"
when: service is undefined
- name: Handle library apptype
ansible.builtin.debug:
msg: "Skipping '{{ appname }}' - library apptype has no actions"
when:
- service.apptype == 'library'
changed_when: false
- name: Validate action exists
ansible.builtin.debug:
msg: "Action '{{ saction }}' is not defined for service '{{ appname }}'. Please correct the service descriptor!"
when:
- service.apptype != 'library'
- not saction in service.actions
- name: Debug apptype
ansible.builtin.debug:
msg: "APPTYPE: {{ service.apptype }}"
- name: Check process status
ansible.builtin.shell:
cmd: "pgrep -of '{{ service.procname }}'"
register: proc_check
changed_when: false
ignore_errors: true
when: service.apptype != 'library' and 'procname' in service.keys()
- name: debug proc check
ansible.builtin.debug:
msg: "proc_check: {{ proc_check }}"
when: service.apptype != 'spa' and proc_check is defined
- block:
- block: # Start action block
- name: Check if already running (start)
ansible.builtin.debug:
msg: "Process '{{ service.procname }}' is already running"
when: proc_check is defined and ('rc' in proc_check.keys() and proc_check.rc == 0)
- name: Assure pid file is gone if app is not running anymore
ansible.builtin.file:
path: "/opt/mercateo/var/run/{{ service.name }}.pid"
state: absent
when: (proc_check is undefined or proc_check.rc != 0)
- name: Assure log directory for service.log is present
ansible.builtin.file:
path: "{{ logdir }}/{{ service.name }}"
state: directory
owner: "{{ service.deployment_user }}"
group: "{{ service.group }}"
mode: "{{ service.mode }}"
- name: Intent to restart the service
ansible.builtin.debug:
msg: "Action: {{ service.actions.start }} on host {{ short_hostname }}"
- name: Execute start command
ansible.builtin.shell: "/usr/bin/nohup {{ service.actions.start }}"
become_user: "{{ service.deployment_user }}"
become: yes
become_flags: '-i'
register: startoutput
when: proc_check is defined and proc_check.rc != 0 and saction == 'start' and service.apptype not in ['catalog-server']
- name: Execute debug command
ansible.builtin.shell: "/usr/bin/nohup {{ service.actions.debug }}"
become_user: "{{ service.deployment_user }}"
become: yes
become_flags: '-i'
register: debugoutput
when: proc_check is defined and proc_check.rc != 0 and saction == 'debug' and service.apptype in ['standalone', 'sub-meerkat']
- name: Show the start action output
ansible.builtin.debug:
msg: |
TIMEOUT: {{ timeout_seconds }}
{{ startoutput if saction == 'start' else debugoutput }}
- name: Wait for service availability
ansible.builtin.wait_for:
port: "{{ service.statusport }}"
timeout: "{{ timeout_seconds | int | default(600) }}"
state: present
delay: 1
when: proc_check.rc != 0 and service.apptype in ['standalone', 'springboot', 'tomcat-instance', 'sub-meerkat'] and service.statusport is defined and service.statusport is not none
when: saction == 'start' or saction == 'debug' and saction in service.actions.keys() and service.apptype not in ['catalog-server', 'library']
- block: # Stop action block
- name: Check if not running (stop)
ansible.builtin.debug:
msg: "Process '{{ service.procname }}' is not running"
when: proc_check is defined and proc_check.rc != 0
- name: Debug stop command
ansible.builtin.debug:
msg: "Command '{{ service.actions.stop }}' will be issued to stop the process"
- name: Execute stop command
ansible.builtin.shell: "{{ service.actions.stop }}"
become_user: "{{ service.deployment_user }}"
become: yes
become_flags: '-i'
register: stopoutput
when: proc_check is defined and proc_check.rc == 0
- name: Show the stop action output
ansible.builtin.debug:
msg: "{{ stopoutput }}, waiting for statusport to be released"
when: stopoutput is defined
- name: Wait for service termination (status port based)
ansible.builtin.wait_for:
port: "{{ service.statusport }}"
state: stopped
delay: 1
timeout: "{{ timeout_seconds | default(60) }}"
when: proc_check is defined and proc_check.rc == 0 and service.apptype in ['standalone', 'springboot', 'tomcat-instance', 'sub-meerkat'] and service.statusport is defined and service.statusport is not none
- name: Wait for service termination (pid based)
ansible.builtin.wait_for:
path: "/proc/{{ proc_check.stdout }}"
state: absent
timeout: "{{ timeout_seconds }}"
register: wait_result
failed_when: false
when: proc_check is defined and proc_check.rc == 0 and service.apptype in ['standalone', 'springboot', 'tomcat-instance', 'sub-meerkat'] and service.statusport is defined and (service.statusport is undefined or service.statusport is none)
when: saction == 'stop' and saction in service.actions.keys()
- block: # Restart action block (meerkat only)
- name: Execute restart command
ansible.builtin.command:
cmd: "{{ service.actions.restart }}"
become_user: "{{ service.deployment_user }}"
become: yes
become_flags: '-i'
register: restartoutput
- name: Show the stop action output
ansible.builtin.debug:
msg: "{{ stopoutput }}"
when: saction == 'restart' and service.apptype == 'meerkat'
- block: # Restart action block (non-meerkat)
- name: Execute stop command for restart
ansible.builtin.shell: "{{ service.actions.stop }}"
become_user: "{{ service.deployment_user }}"
become: yes
become_flags: '-i'
register: stopoutput
when: proc_check is defined and proc_check.rc == 0
- name: Wait for service termination during restart
ansible.builtin.wait_for:
port: "{{ service.statusport }}"
state: stopped
delay: 1
timeout: "{{ timeout_seconds | default(60) }}"
when: service.apptype in ['standalone', 'springboot', 'tomcat-instance', 'sub-meerkat'] and service.statusport is defined and service.statusport is not none
- name: Wait for service termination (pid based)
ansible.builtin.wait_for:
path: "/proc/{{ proc_check.stdout }}"
state: absent
timeout: "{{ timeout_seconds }}"
register: wait_result
failed_when: false
when: proc_check is defined and proc_check.rc == 0 and service.apptype in ['standalone', 'springboot', 'tomcat-instance', 'sub-meerkat'] and service.statusport is defined and (service.statusport is undefined or service.statusport is none)
- name: Assure pid file is gone before restart
ansible.builtin.file:
path: "/opt/mercateo/var/run/{{ service.name }}.pid"
state: absent
when: (proc_check is undefined or proc_check.rc != 0)
- name: Intent to restart the service
ansible.builtin.debug:
msg: "Action: {{ service.actions.start }} on host {{ short_hostname }}"
- name: Execute start command for restart
ansible.builtin.shell: "/usr/bin/nohup {{ service.actions.start }}"
become_user: "{{ service.deployment_user }}"
become: yes
become_flags: '-i'
register: startoutput
when: saction == 'restart'
- name: Execute debug command for restart
ansible.builtin.shell: "/usr/bin/nohup {{ service.actions.debug }}"
become_user: "{{ service.deployment_user }}"
become: yes
become_flags: '-i'
register: startoutput
when: saction == 'restartdebug'
- name: Wait for service availability after restart
ansible.builtin.wait_for:
port: "{{ service.statusport }}"
state: started
delay: 1
timeout: "{{ timeout_seconds | default(600) }}"
when: service.apptype in ['standalone', 'springboot', 'tomcat-instance', 'sub-meerkat'] and service.statusport is defined and service.statusport is not none and startoutput is defined
when: saction in ['restart', 'restartdebug'] and service.apptype not in ['meerkat', 'library', 'spa', 'catalog-server']
- block: # Status action block
- name: Check process status details
ansible.builtin.debug:
msg: "Process {{ service.procname }} is {{ 'running' if proc_check.rc == 0 else 'not running' }}"
when: proc_check is defined and service.procname is defined
- name: Execute status command
ansible.builtin.shell: "{{ service.actions.status }}"
become_user: "{{ service.deployment_user }}"
become: yes
become_flags: '-i'
register: statusoutput
- name: Show status command output
ansible.builtin.debug:
msg: "{{ statusoutput.stdout_lines }}"
when: statusoutput is defined
when: saction in service.actions and saction == 'status'
when: (saction in service.actions) or (saction in ['restart','restartdebug'] and saction not in service.actions and service.apptype not in ['meerkat', 'library']) and service.apptype not in ['library', 'catalog-server']
- block: # Kill block
- name: Soft-Kill process with SIGTERM
shell: "kill -15 {{ proc_check.stdout }}"
register: kill_result
failed_when: false
- name: Wait for process to terminate
ansible.builtin.wait_for:
path: "/proc/{{ proc_check.stdout }}"
state: absent
timeout: "{{ timeout_seconds }}"
register: wait_result
failed_when: false
- name: Force kill process with SIGKILL
shell: "kill -9 {{ proc_check.stdout }}"
when: wait_result is defined and wait_result.failed is defined and wait_result.failed
changed_when: true
when: proc_check is defined and ('rc' in proc_check.keys() and proc_check.rc == 0) and ('stdout' in proc_check.keys() and proc_check.stdout != "") and kill_on_host is defined and inventory_hostname == kill_on_host and 'procname' in service.keys()
Please be laconic and report with the list of found bugs, inconsistencies and errors.