From 903693e67f81a28b596b6ee31e49d0ae3adb99f9 Mon Sep 17 00:00:00 2001 From: Alessandro Fael Garcia Date: Thu, 17 Sep 2020 17:00:27 +0200 Subject: [PATCH] Refactor handlers (#321) --- CHANGELOG.md | 1 + handlers/main.yml | 31 +++++++++++++++-------------- tasks/config/modify-systemd.yml | 6 +++--- tasks/config/template-config.yml | 10 +++++----- tasks/config/upload-config.yml | 8 ++++---- tasks/opensource/install-alpine.yml | 2 +- tasks/opensource/install-bsd.yml | 12 +++++------ tasks/opensource/install-debian.yml | 2 +- tasks/opensource/install-oss.yml | 2 +- tasks/opensource/install-redhat.yml | 2 +- tasks/opensource/install-source.yml | 8 ++++---- tasks/opensource/install-suse.yml | 2 +- tasks/plus/install-alpine.yml | 2 +- tasks/plus/install-debian.yml | 2 +- tasks/plus/install-freebsd.yml | 2 +- tasks/plus/install-redhat.yml | 2 +- tasks/plus/install-suse.yml | 2 +- tasks/plus/setup-license.yml | 8 ++++---- 18 files changed, 53 insertions(+), 51 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0cd9e0..0d5421e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ FEATURES: ENHANCEMENTS: +* Added handlers to check for NGINX syntax validity and fail if any errors are detected. * Major backend refactoring to reduce the number of files and tasks. * You can now specify an `nginx_repository` for NGINX Plus too. * Moved "constant" variables to `vars/main.yml`. diff --git a/handlers/main.yml b/handlers/main.yml index 3ae7f56..8a5c72c 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -1,28 +1,29 @@ --- - name: "(Handler) Check NGINX" command: "nginx -t" - changed_when: false + register: config + ignore_errors: yes + listen: "(Handler) Run NGINX" -- name: "(Handler) Systemd Daemon-Reload" +- name: "(Handler) Print NGINX error if syntax check fails" + fail: + msg: "{{ config.stderr }}" + when: config.rc != 0 + listen: "(Handler) Run NGINX" + +- name: "(Handler) Systemd daemon-reload" systemd: daemon_reload: yes - notify: "(Handler) Start NGINX" -- name: "(Handler) Run NGINX" - block: - - name: "(Handler) Start NGINX" - service: - name: nginx - state: started - enabled: yes - notify: "(Handler) Check NGINX" - - - name: "(Handler) Reload NGINX" - command: "nginx -s reload" - changed_when: false +- name: "(Handler) Start/Reload NGINX" + service: + name: nginx + state: reloaded + enabled: yes when: - nginx_start | bool - not ansible_check_mode | bool + listen: "(Handler) Run NGINX" - name: "(Handler) Start NGINX Amplify agent" service: diff --git a/tasks/config/modify-systemd.yml b/tasks/config/modify-systemd.yml index 10742e5..649e0f8 100644 --- a/tasks/config/modify-systemd.yml +++ b/tasks/config/modify-systemd.yml @@ -20,7 +20,7 @@ when: - not nginx_service_custom | bool - not nginx_service_clean | bool - notify: "(Handler) Systemd Daemon-Reload" + notify: "(Handler) Systemd daemon-reload" - name: "Customize override for NGINX systemd service" copy: @@ -32,14 +32,14 @@ when: - nginx_service_custom | bool - not nginx_service_clean | bool - notify: "(Handler) Systemd Daemon-Reload" + notify: "(Handler) Systemd daemon-reload" - name: "Remove override for NGINX systemd service" file: path: "{{ nginx_service_overridepath }}" state: absent when: nginx_service_clean | bool - notify: "(Handler) Systemd Daemon-Reload" + notify: "(Handler) Systemd daemon-reload" - name: "Modify systemd" debug: diff --git a/tasks/config/template-config.yml b/tasks/config/template-config.yml index 043d153..a325a3a 100644 --- a/tasks/config/template-config.yml +++ b/tasks/config/template-config.yml @@ -35,7 +35,7 @@ backup: yes mode: 0644 when: nginx_main_template_enable | bool - notify: "(Handler) Reload NGINX" + notify: "(Handler) Run NGINX" - name: "(DEPRECATED) Ensure NGINX HTTP Directory Exists" file: @@ -65,7 +65,7 @@ mode: 0644 with_dict: "{{ nginx_http_template }}" when: nginx_http_template_enable | bool - notify: "(Handler) Reload NGINX" + notify: "(Handler) Run NGINX" - name: "(DEPRECATED) Dynamically Generate NGINX Stub Status Configuration File" template: @@ -74,7 +74,7 @@ backup: yes mode: 0644 when: nginx_status_enable | bool - notify: "(Handler) Reload NGINX" + notify: "(Handler) Run NGINX" - name: "(DEPRECATED) Dynamically Generate NGINX API Configuration File" template: @@ -83,7 +83,7 @@ backup: yes mode: 0644 when: nginx_rest_api_enable | bool - notify: "(Handler) Reload NGINX" + notify: "(Handler) Run NGINX" - name: "(DEPRECATED) Ensure NGINX Stream Directory Exists" file: @@ -101,4 +101,4 @@ mode: 0644 with_dict: "{{ nginx_stream_template }}" when: nginx_stream_template_enable | bool - notify: "(Handler) Reload NGINX" + notify: "(Handler) Run NGINX" diff --git a/tasks/config/upload-config.yml b/tasks/config/upload-config.yml index dd7f393..b53a590 100644 --- a/tasks/config/upload-config.yml +++ b/tasks/config/upload-config.yml @@ -19,7 +19,7 @@ mode: 0644 with_fileglob: "{{ nginx_html_upload_src }}" when: nginx_html_upload_enable | bool - notify: "(Handler) Reload NGINX" + notify: "(Handler) Run NGINX" - name: "(DEPRECATED) Ensure NGINX Main Directory Exists" file: @@ -35,7 +35,7 @@ backup: yes mode: 0644 when: nginx_main_upload_enable | bool - notify: "(Handler) Reload NGINX" + notify: "(Handler) Run NGINX" - name: "(DEPRECATED) Ensure NGINX HTTP Directory Exists" file: @@ -52,7 +52,7 @@ mode: 0644 with_fileglob: "{{ nginx_http_upload_src }}" when: nginx_http_upload_enable | bool - notify: "(Handler) Reload NGINX" + notify: "(Handler) Run NGINX" - name: "(DEPRECATED) Ensure NGINX Stream Directory Exists" file: @@ -69,7 +69,7 @@ mode: 0644 with_fileglob: "{{ nginx_stream_upload_src }}" when: nginx_stream_upload_enable | bool - notify: "(Handler) Reload NGINX" + notify: "(Handler) Run NGINX" - name: "(DEPRECATED) Ensure SSL Certificate Directory Exists" file: diff --git a/tasks/opensource/install-alpine.yml b/tasks/opensource/install-alpine.yml index d77ee92..1951ecf 100644 --- a/tasks/opensource/install-alpine.yml +++ b/tasks/opensource/install-alpine.yml @@ -11,4 +11,4 @@ repository: "{{ repository }}" state: "{{ nginx_state }}" update_cache: yes - notify: "(Handler) Start NGINX" + notify: "(Handler) Run NGINX" diff --git a/tasks/opensource/install-bsd.yml b/tasks/opensource/install-bsd.yml index bced3af..b22dde1 100644 --- a/tasks/opensource/install-bsd.yml +++ b/tasks/opensource/install-bsd.yml @@ -21,7 +21,7 @@ name: "www/nginx{{ nginx_version | default('') }}" state: "{{ nginx_state }}" when: nginx_bsd_install_packages | bool - notify: "(Handler) Start NGINX" + notify: "(Handler) Run NGINX" - name: "(FreeBSD) Install NGINX port" portinstall: @@ -29,7 +29,7 @@ use_packages: "{{ nginx_bsd_portinstall_use_packages | default(omit) }}" state: "{{ nginx_state }}" when: not nginx_bsd_install_packages | bool - notify: "(Handler) Start NGINX" + notify: "(Handler) Run NGINX" when: ansible_facts['system'] == "FreeBSD" - name: "(OpenBSD) Install NGINX" @@ -40,7 +40,7 @@ build: no state: "{{ nginx_state }}" when: nginx_bsd_install_packages | bool - notify: "(Handler) Start NGINX" + notify: "(Handler) Run NGINX" - name: "(OpenBSD) Install NGINX port" openbsd_pkg: @@ -48,7 +48,7 @@ build: yes state: "{{ nginx_state }}" when: not nginx_bsd_install_packages | bool - notify: "(Handler) Start NGINX" + notify: "(Handler) Run NGINX" when: ansible_facts['system'] == "OpenBSD" - name: "(NetBSD) Install NGINX" @@ -56,7 +56,7 @@ - name: "NetBSD) Install NGINX package" command: "pkg_add www/nginx{{ nginx_version | default('') }}" when: nginx_bsd_install_packages | bool - notify: "(Handler) Start NGINX" + notify: "(Handler) Run NGINX" - name: "(NetBSD) Install NGINX port" fail: @@ -69,7 +69,7 @@ - name: "Install NGINX package" command: "pkg install www/nginx{{ nginx_version | default('') }}" when: nginx_bsd_install_packages | bool - notify: "(Handler) Start NGINX" + notify: "(Handler) Run NGINX" - name: "Install NGINX port" fail: diff --git a/tasks/opensource/install-debian.yml b/tasks/opensource/install-debian.yml index 2a67e7f..ac5bc1b 100644 --- a/tasks/opensource/install-debian.yml +++ b/tasks/opensource/install-debian.yml @@ -11,4 +11,4 @@ apt: name: "nginx{{ nginx_version | default('') }}" state: "{{ nginx_state }}" - notify: "(Handler) Start NGINX" + notify: "(Handler) Run NGINX" diff --git a/tasks/opensource/install-oss.yml b/tasks/opensource/install-oss.yml index 35c7f20..3426e8f 100644 --- a/tasks/opensource/install-oss.yml +++ b/tasks/opensource/install-oss.yml @@ -25,7 +25,7 @@ name: "nginx{{ nginx_version | default('') }}" state: "{{ nginx_state }}" when: nginx_install_from == "os_repository" - notify: "(Handler) Start NGINX" + notify: "(Handler) Run NGINX" when: ansible_facts['system'] | lower is not search('bsd') - name: "Install NGINX in Unix systems" diff --git a/tasks/opensource/install-redhat.yml b/tasks/opensource/install-redhat.yml index fa45b97..58c4f74 100644 --- a/tasks/opensource/install-redhat.yml +++ b/tasks/opensource/install-redhat.yml @@ -30,4 +30,4 @@ disablerepo: "*" enablerepo: "nginx" update_cache: yes - notify: "(Handler) Start NGINX" + notify: "(Handler) Run NGINX" diff --git a/tasks/opensource/install-source.yml b/tasks/opensource/install-source.yml index 5327567..342c822 100644 --- a/tasks/opensource/install-source.yml +++ b/tasks/opensource/install-source.yml @@ -378,7 +378,7 @@ state: restarted enabled: yes when: ansible_facts['service_mgr'] == "systemd" - notify: "(Handler) Start NGINX" + notify: "(Handler) Run NGINX" - name: "Upload upstart NGINX service file" copy: @@ -405,7 +405,7 @@ - name: "Start Upstart NGINX service reload" command: "nginx" when: ansible_facts['service_mgr'] == "upstart" - notify: "(Handler) Start NGINX" + notify: "(Handler) Run NGINX" - name: "Upload SysVinit NGINX service file" copy: @@ -415,7 +415,7 @@ group: root mode: 0755 when: ansible_facts['service_mgr'] == "sysvinit" - notify: "(Handler) Start NGINX" + notify: "(Handler) Run NGINX" - name: "Upload OpenRC NGINX service file" copy: @@ -429,7 +429,7 @@ - name: "Enable OpenRC NGINX service" command: rc-update add nginx default when: ansible_facts['service_mgr'] == "openrc" - notify: "(Handler) Start NGINX" + notify: "(Handler) Run NGINX" when: not nginx_result.stat.exists - name: "Cleanup downloads" diff --git a/tasks/opensource/install-suse.yml b/tasks/opensource/install-suse.yml index 7544969..f63853f 100644 --- a/tasks/opensource/install-suse.yml +++ b/tasks/opensource/install-suse.yml @@ -10,4 +10,4 @@ state: "{{ nginx_state }}" disable_recommends: no update_cache: yes - notify: "(Handler) Start NGINX" + notify: "(Handler) Run NGINX" diff --git a/tasks/plus/install-alpine.yml b/tasks/plus/install-alpine.yml index 0e96dd7..9ad1017 100644 --- a/tasks/plus/install-alpine.yml +++ b/tasks/plus/install-alpine.yml @@ -11,4 +11,4 @@ name: "nginx-plus{{ nginx_version | default('') }}" repository: "{{ repository }}" state: "{{ nginx_state }}" - notify: "(Handler) Start NGINX" + notify: "(Handler) Run NGINX" diff --git a/tasks/plus/install-debian.yml b/tasks/plus/install-debian.yml index 4edbff4..77493f9 100644 --- a/tasks/plus/install-debian.yml +++ b/tasks/plus/install-debian.yml @@ -23,4 +23,4 @@ apt: name: "nginx-plus{{ nginx_version | default('') }}" state: "{{ nginx_state }}" - notify: "(Handler) Start NGINX" + notify: "(Handler) Run NGINX" diff --git a/tasks/plus/install-freebsd.yml b/tasks/plus/install-freebsd.yml index d35e6b6..cc8b869 100644 --- a/tasks/plus/install-freebsd.yml +++ b/tasks/plus/install-freebsd.yml @@ -25,4 +25,4 @@ pkgng: name: "nginx-plus{{ nginx_version | default('') }}" state: "{{ nginx_state }}" - notify: "(Handler) Start NGINX" + notify: "(Handler) Run NGINX" diff --git a/tasks/plus/install-redhat.yml b/tasks/plus/install-redhat.yml index 39b2a38..0a6f3cb 100644 --- a/tasks/plus/install-redhat.yml +++ b/tasks/plus/install-redhat.yml @@ -18,4 +18,4 @@ disablerepo: "*" enablerepo: "nginx-plus" update_cache: yes - notify: "(Handler) Start NGINX" + notify: "(Handler) Run NGINX" diff --git a/tasks/plus/install-suse.yml b/tasks/plus/install-suse.yml index a2abfe4..64ce7f4 100644 --- a/tasks/plus/install-suse.yml +++ b/tasks/plus/install-suse.yml @@ -16,4 +16,4 @@ name: "nginx-plus{{ nginx_version | default('') }}" state: "{{ nginx_state }}" update_cache: yes - notify: "(Handler) Start NGINX" + notify: "(Handler) Run NGINX" diff --git a/tasks/plus/setup-license.yml b/tasks/plus/setup-license.yml index 6c49cb7..011e1f9 100644 --- a/tasks/plus/setup-license.yml +++ b/tasks/plus/setup-license.yml @@ -19,8 +19,8 @@ decrypt: yes mode: 0444 loop: - - "{{ nginx_license.certificate }}" - - "{{ nginx_license.key }}" + - "{{ nginx_license['certificate'] }}" + - "{{ nginx_license['key'] }}" when: ansible_facts['os_family'] != "Alpine" - name: "(Alpine Linux) Set up NGINX Plus license" @@ -33,14 +33,14 @@ - name: "(Alpine Linux) Copy NGINX Plus key" copy: - src: "{{ nginx_license.key }}" + src: "{{ nginx_license['key'] }}" dest: /etc/apk/cert.key decrypt: yes mode: 0444 - name: "(Alpine Linux) Copy NGINX Plus certificate" copy: - src: "{{ nginx_license.certificate }}" + src: "{{ nginx_license['certificate'] }}" dest: /etc/apk/cert.pem decrypt: yes mode: 0444