diff --git a/docs/usage.rst b/docs/usage.rst index fa585956bdbca059c377fad9d1a3f00ea5c359a7..f32494bf123f203c8861b408654ac708537f4087 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -1649,7 +1649,7 @@ on the safe side: - settings.py - urls.py notify: - - Restart website wiki.example.com + - Restart wiki - name: Deploy project database and deploy static files django_manage: command="{{ item }}" @@ -1674,6 +1674,16 @@ on the safe side: register: wiki_superuser changed_when: wiki_superuser.stdout == "Created superuser." + :file:`~/mysite/roles/wiki/handlers/main.yml` + :: + + --- + + - name: Restart wiki + service: + name: wiki.example.com + state: restarted + 5. There is a couple of files that we are deploying through the above tasks. Let's create them as well. diff --git a/roles/wsgi_website/handlers/main.yml b/roles/wsgi_website/handlers/main.yml index e0ae79be23ae5d6d26bc5ef0b0f885d962b7234a..85cf33f3ed06ea1e3d29e3b6013a820062f21df3 100644 --- a/roles/wsgi_website/handlers/main.yml +++ b/roles/wsgi_website/handlers/main.yml @@ -1,6 +1,7 @@ --- -- name: "Restart website {{ fqdn }}" +- name: Restart WSGI services service: - name: "{{ fqdn }}" + name: "{{ item }}" state: restarted + with_items: "{{ wsgi_services_to_restart }}" diff --git a/roles/wsgi_website/tasks/main.yml b/roles/wsgi_website/tasks/main.yml index 0bbea9ad15eb05557c3db6cbfe8fa07d326a5b19..1459a1109aeb52e59904954928962fcd782ab5a7 100644 --- a/roles/wsgi_website/tasks/main.yml +++ b/roles/wsgi_website/tasks/main.yml @@ -74,8 +74,9 @@ name: "{{ item }}" state: present with_items: "{{ packages }}" + register: install_extra_packages notify: - - "Restart website {{ fqdn }}" + - Restart WSGI services - name: Set-up MariaDB mysql_config symbolic link for compatibility (workaround for Debian bug 766996) file: @@ -134,8 +135,9 @@ - package: futures version: "{{ futures_version }}" when: "not wsgi_requirements" + register: install_wsgi_server notify: - - "Restart website {{ fqdn }}" + - Restart WSGI services - include: requirements.yml when: "wsgi_requirements" @@ -148,8 +150,9 @@ state: present virtualenv: "{{ home }}/virtualenv" with_items: "{{ virtualenv_packages }}" + register: install_additional_packages_in_virtualenv notify: - - "Restart website {{ fqdn }}" + - Restart WSGI services - name: Deploy systemd socket configuration for website template: @@ -158,9 +161,10 @@ owner: root group: root mode: 0644 + register: deploy_systemd_socket_configuration notify: - Reload systemd - - "Restart website {{ fqdn }}" + - Restart WSGI services - name: Deploy systemd service configuration for website template: @@ -169,9 +173,10 @@ owner: root group: root mode: 0644 + register: deploy_systemd_service_configuration notify: - Reload systemd - - "Restart website {{ fqdn }}" + - Restart WSGI services - name: Enable the website service service: @@ -234,6 +239,32 @@ notify: - Restart nginx +- name: Set-up empty list of WSGI services to restart + set_fact: + wsgi_services_to_restart: [] + when: "wsgi_services_to_restart is not defined" + tags: + - handlers + +- name: Add service to list of WSGI services to restart + set_fact: + wsgi_services_to_restart: "{{ wsgi_services_to_restart + [ fqdn ] }}" + when: | + fqdn not in wsgi_services_to_restart and + ((install_extra_packages is defined and install_extra_packages.changed) or + (install_wsgi_server is defined and install_wsgi_server.changed) or + (install_additional_packages_in_virtualenv is defined and install_additional_packages_in_virtualenv.changed) or + (deploy_systemd_socket_configuration is defined and deploy_systemd_socket_configuration.changed) or + (deploy_systemd_service_configuration is defined and deploy_systemd_service_configuration.changed) or + (install_gunciron_via_requirements is defined and install_gunciron_via_requirements.changed) or + (handlers | default(False) | bool() == True)) + tags: + - handlers + # [ANSIBLE0016] Tasks that run when changed should likely be handlers + # This specific task is used in order to work around inability of Ansible + # to provide properly parametrised handlers for reusable roles. + - skip_ansible_lint + - name: Explicitly run all handlers include: ../handlers/main.yml when: "handlers | default(False) | bool() == True" diff --git a/roles/wsgi_website/tasks/requirements.yml b/roles/wsgi_website/tasks/requirements.yml index c81d7ccaec7e5a5aee1df320d6518f69313b03a7..9e7ddb29004fd92baa49f791fac5e4318f8f6589 100644 --- a/roles/wsgi_website/tasks/requirements.yml +++ b/roles/wsgi_website/tasks/requirements.yml @@ -36,5 +36,6 @@ requirements: "{{ home }}/.wsgi_requirements.txt" state: present virtualenv: "{{ home }}/virtualenv" + register: install_gunciron_via_requirements notify: - - "Restart website {{ fqdn }}" + - Restart WSGI services diff --git a/testsite/playbooks/roles/wsgihello/handlers/main.yml b/testsite/playbooks/roles/wsgihello/handlers/main.yml new file mode 100644 index 0000000000000000000000000000000000000000..776c5423f205f7a2d58b729eecc80a760b6d16e9 --- /dev/null +++ b/testsite/playbooks/roles/wsgihello/handlers/main.yml @@ -0,0 +1,6 @@ +--- + +- name: Restart wsgihello + service: + name: "wsgi.{{ testsite_domain }}" + state: restarted diff --git a/testsite/playbooks/roles/wsgihello/tasks/main.yml b/testsite/playbooks/roles/wsgihello/tasks/main.yml index dc51d2fa715b29a5a5787ddb7e4c0cf1a080f596..49a51830e2f415960999f2c20443704cfe121165 100644 --- a/testsite/playbooks/roles/wsgihello/tasks/main.yml +++ b/testsite/playbooks/roles/wsgihello/tasks/main.yml @@ -8,4 +8,4 @@ copy: src="hello.wsgi" dest="/var/www/wsgi.{{ testsite_domain }}/code/wsgi.py" owner="admin-wsgi_{{ testsite_domain_underscores }}" group="web-wsgi_{{ testsite_domain_underscores }}" mode=640 notify: - - Restart website wsgi.{{ testsite_domain }} \ No newline at end of file + - Restart wsgihello diff --git a/testsite/playbooks/roles/wsgihello2/handlers/main.yml b/testsite/playbooks/roles/wsgihello2/handlers/main.yml new file mode 100644 index 0000000000000000000000000000000000000000..6265784eed77061f10a33b6437882d668336a49c --- /dev/null +++ b/testsite/playbooks/roles/wsgihello2/handlers/main.yml @@ -0,0 +1,6 @@ +--- + +- name: Restart wsgihello2 + service: + name: "wsgireq.{{ testsite_domain }}" + state: restarted diff --git a/testsite/playbooks/roles/wsgihello2/tasks/main.yml b/testsite/playbooks/roles/wsgihello2/tasks/main.yml index 4f96ffdaac8e9347c0a6ecd966c40897cd97ca8e..8fed7495cb887880b46857e71d5304cd73c245de 100644 --- a/testsite/playbooks/roles/wsgihello2/tasks/main.yml +++ b/testsite/playbooks/roles/wsgihello2/tasks/main.yml @@ -29,10 +29,10 @@ register: pip_sync_result changed_when: "pip_sync_result.stdout != 'Everything up-to-date'" notify: - - Restart website wsgireq.{{ testsite_domain }} + - Restart wsgihello2 - name: Deploy WSGI application copy: src="hello.wsgi" dest="/var/www/wsgireq.{{ testsite_domain }}/code/wsgi.py" owner="admin-wsgireq_{{ testsite_domain_underscores }}" group="web-wsgireq_{{ testsite_domain_underscores }}" mode=640 notify: - - Restart website wsgireq.{{ testsite_domain }} \ No newline at end of file + - Restart wsgihello2