diff --git a/README.md b/README.md index 477a615..dbcbc9d 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,9 @@ This role controls an Adobe Experience Manager (AEM) 6.x service on Linux server ## Requirements -This role requires Ansible 2.0 or higher and works with AEM 6.1 or higher. The role requires an AEM service that can be controlled with the Ansible `service` module to be installed on the target machine. +This role requires Ansible 2.4 or higher and works with AEM 6.1 or +higher. The role requires an AEM service that can be controlled with the +Ansible `service` module to be installed on the target machine. ## Role Variables @@ -34,6 +36,35 @@ The desired state of the service after this role finishes, one of `started`, `st The time to wait for the startup or shutdown to finish (in seconds). +aem_service_restricted_mode: false + +Enables / disables the restricted mode to work with customized commands like `sudo`. + + # aem_service_start_command: + +Overwrites the default (service manager related) start command. + + # aem_service_stop_command: + +Overwrites the default (service manager related) stop command. + + # aem_service_status_command: + +Overwrites the default (service manager related) status command. + + # aem_service_status_stopped_status_codes: + +Overwrites the default (service manager related) stopped status codes +when set. + + # aem_service_status_started_status_codes: + +Overwrites the default (service manager related) started status codes +when set. + +aem_service_status_valid_status_codes: "{{ _aem_service_status_stopped_status_codes | union(_aem_service_status_started_status_codes) | unique }}" + +# List of all valid AEM status codes. ## Dependencies diff --git a/defaults/main.yml b/defaults/main.yml index b4d8634..11c62d5 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -8,3 +8,24 @@ aem_service_state: started aem_service_timeout: 1200 # URL to poll while waiting for startup aem_service_login_url: "http://localhost:{{ aem_service_port }}/libs/granite/core/content/login.html" + +# Enables / disables the restricted mode to work with customized commands like sudo +aem_service_restricted_mode: false + +# Overwrites the default (service manager related) start command +# aem_service_start_command: + +# Overwrites the default (service manager related) stop command +# aem_service_stop_command: + +# Overwrites the default (service manager related) status command +# aem_service_status_command: + +# Overwrites the default (service manager related) stopped status codes +# aem_service_status_stopped_status_codes: + +# Overwrites the default (service manager related) started status codes +# aem_service_status_started_status_codes: + +# List of all valid AEM status codes. +aem_service_status_valid_status_codes: "{{ _aem_service_status_stopped_status_codes | union(_aem_service_status_started_status_codes) | unique }}" \ No newline at end of file diff --git a/tasks/main.yml b/tasks/main.yml index 10b7ee3..b2a146b 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,3 +1,9 @@ +- name: Include service manager specific variables. + include_vars: "{{ item }}" + with_first_found: + - "service_mgr_{{ ansible_service_mgr | mandatory }}.yml" + - service_mgr_default.yml + - include_tasks: start_aem.yml when: aem_service_state == 'started' diff --git a/tasks/start_aem.yml b/tasks/start_aem.yml index 0b9c521..22b8d39 100644 --- a/tasks/start_aem.yml +++ b/tasks/start_aem.yml @@ -1,8 +1,24 @@ -- name: Start AEM. +- name: Start AEM (escalated). service: name: "{{ aem_service_name }}" state: started - register: aem_service_start_result + register: aem_service_start_result_escalated + when: not aem_service_restricted_mode + +- name: Start AEM (restricted). + block: + - name: Retrieve AEM status (restricted). + raw: "{{ _aem_service_status_command }}" + changed_when: false + register: _aem_status_result_1 + failed_when: _aem_status_result_1.rc not in aem_service_status_valid_status_codes + + - name: Start AEM (restricted). + raw: "{{ _aem_service_start_command }}" + when: _aem_status_result_1.rc != 0 + register: aem_service_start_result_restricted + + when: aem_service_restricted_mode - name: "Wait for AEM startup. [{{ inventory_hostname }}]" uri: @@ -12,6 +28,6 @@ until: result.content.find("QUICKSTART_HOMEPAGE") != -1 retries: "{{ aem_service_timeout // 10 }}" delay: 10 - when: aem_service_start_result.changed + when: aem_service_start_result_escalated.changed or aem_service_start_result_restricted.changed tags: - skip_ansible_lint \ No newline at end of file diff --git a/tasks/stop_aem.yml b/tasks/stop_aem.yml index 69564cb..86e6a5d 100644 --- a/tasks/stop_aem.yml +++ b/tasks/stop_aem.yml @@ -1,5 +1,32 @@ -- name: Stop AEM. +- name: Stop AEM (escalated). service: name: "{{ aem_service_name }}" state: stopped - register: aem_service_stop_result \ No newline at end of file + register: aem_service_stop_result_escalated + when: not aem_service_restricted_mode + +- name: Stop AEM (restricted). + block: + - name: Retrieve AEM status (restricted). + raw: "{{ _aem_service_status_command }}" + changed_when: false + failed_when: _aem_status_result.rc not in aem_service_status_valid_status_codes + register: _aem_status_result + + - name: Stop AEM (restricted). + raw: "{{ _aem_service_stop_command }}" + when: _aem_status_result.rc == 0 + register: aem_service_stop_result_restricted + + when: aem_service_restricted_mode + +- name: Wait for AEM shutdown. + wait_for: + port: "{{ aem_service_port }}" + state: stopped + delay: 0 + sleep: 10 + timeout: "{{ aem_service_timeout }}" + when: aem_service_stop_result_escalated.changed or aem_service_stop_result_restricted.changed + tags: + - skip_ansible_lint \ No newline at end of file diff --git a/vars/service_mgr_default.yml b/vars/service_mgr_default.yml new file mode 100644 index 0000000..c97e26e --- /dev/null +++ b/vars/service_mgr_default.yml @@ -0,0 +1,5 @@ +_aem_service_start_command: "{{ aem_service_start_command | default('sudo -n /etc/init.d/'+aem_service_name+' start') }}" +_aem_service_stop_command: "{{ aem_service_stop_command | default('sudo -n /etc/init.d/'+aem_service_name+' stop') }}" +_aem_service_status_command: "{{ aem_service_status_command | default('sudo -n /etc/init.d/'+aem_service_name+' status') }}" +_aem_service_status_stopped_status_codes: "{{ aem_service_status_stopped_status_codes | default([4]) }}" +_aem_service_status_started_status_codes: "{{ aem_service_status_started_status_codes | default([0]) }}" \ No newline at end of file diff --git a/vars/service_mgr_systemd.yml b/vars/service_mgr_systemd.yml new file mode 100644 index 0000000..3df3a89 --- /dev/null +++ b/vars/service_mgr_systemd.yml @@ -0,0 +1,5 @@ +_aem_service_start_command: "{{ aem_service_start_command | default('sudo -n /bin/systemctl start '+aem_service_name ) }} && echo $?" +_aem_service_stop_command: "{{ aem_service_stop_command | default('sudo -n /bin/systemctl stop '+aem_service_name ) }} && echo $?" +_aem_service_status_command: "{{ aem_service_status_command | default('sudo -n /bin/systemctl is-active '+aem_service_name) }}" +_aem_service_status_stopped_status_codes: "{{ aem_service_status_stopped_status_codes | default([3]) }}" +_aem_service_status_started_status_codes: "{{ aem_service_status_started_status_codes | default([0]) }}" \ No newline at end of file