From 9202b2b8d9ab6fb5a989341047277189f39acc95 Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Wed, 3 Apr 2019 11:19:06 +0300 Subject: [PATCH] Ensure systemd services are running when doing --tags=start Fixes #129 (Github Issue). Unfortunately, we rely on `service_facts`, which is only available in Ansible >= 2.5. There's little reason to stick to an old version such as Ansible 2.4: - some time has passed since we've raised version requirements - it's time to move into the future (a little bit) - we've recently (in 82b46400729d) improved the way one can run Ansible in a Docker container From now on, Ansible >= 2.5 is required. --- docs/ansible.md | 6 +++--- roles/matrix-base/tasks/sanity_check.yml | 4 ++-- roles/matrix-common-after/tasks/start.yml | 25 ++++++++++++++++++++++- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/docs/ansible.md b/docs/ansible.md index 13d297de1..0987ca7f4 100644 --- a/docs/ansible.md +++ b/docs/ansible.md @@ -8,9 +8,9 @@ If your local computer cannot run Ansible, you can also run Ansible on some serv ## Supported Ansible versions -Generally, Ansible 2.4 or later is required. +Ansible 2.5 or newer is required. -If you're on Ansible 2.5.x, then at least Ansible 2.5.2 is required. +If you're on Ansible 2.5.x, due to bugs in Ansible 2.5.0 and 2.5.1, at least Ansible 2.5.2 is required. ## Checking your Ansible version @@ -20,7 +20,7 @@ The playbook will try to detect it and tell you if you're on an unsupported vers To manually check which verison of Ansible you're on, run: `ansible --version`. -If you're on an old version of Ansible, you should upgrade to a newer version. +If you're on an old version of Ansible, you should [upgrade Ansible to a newer version](#upgrading-ansible) or [use Ansible via Docker](#using-ansible-via-docker). ## Upgrading Ansible diff --git a/roles/matrix-base/tasks/sanity_check.yml b/roles/matrix-base/tasks/sanity_check.yml index 0bff6a6b6..b2d8c249e 100644 --- a/roles/matrix-base/tasks/sanity_check.yml +++ b/roles/matrix-base/tasks/sanity_check.yml @@ -3,10 +3,10 @@ - set_fact: matrix_ansible_outdated_fail_msg: "You are running on Ansible {{ ansible_version.string }}, which is not supported. See our guide about Ansible: https://github.com/spantaleev/matrix-docker-ansible-deploy/blob/master/docs/ansible.md" -- name: Fail if running on Ansible < 2.4 +- name: Fail if running on Ansible < 2.5 fail: msg: "{{ matrix_ansible_outdated_fail_msg }}" - when: "ansible_version.major <= 2 and ansible_version.minor < 4" + when: "ansible_version.major <= 2 and ansible_version.minor < 5" # Ansible 2.5.0 and 2.5.1 are known to have a bug with `include_tasks` + `with_items`. # The bug has been fixed in Ansible 2.5.2. diff --git a/roles/matrix-common-after/tasks/start.yml b/roles/matrix-common-after/tasks/start.yml index 688b9244d..f0015428b 100644 --- a/roles/matrix-common-after/tasks/start.yml +++ b/roles/matrix-common-after/tasks/start.yml @@ -15,4 +15,27 @@ name: "{{ item }}" enabled: yes state: started - with_items: "{{ matrix_systemd_services_list }}" \ No newline at end of file + with_items: "{{ matrix_systemd_services_list }}" + +# If we check service state immediately, we may succeed, +# because it takes some time for the service to attempt to start and actually fail. +# +# Waiting too long (30s) may not work for a similar reason, +# as we may run into systemd's automatic restart logic retrying the service. +- name: Wait a bit, so that services can start (or fail) + wait_for: + timeout: 5 + delegate_to: 127.0.0.1 + become: false + +- name: Populate service facts + service_facts: + +- name: Fail if service isn't detected to be running + fail: + msg: >- + {{ item }} was not detected to be running. + It's possible that there's a configuration problem or another service on your server interferes with it (uses the same ports, etc.). + Try running `systemctl status {{ item }}` and `systemctl -fu {{ item }}` on the server to investigate. + with_items: "{{ matrix_systemd_services_list }}" + when: "ansible_facts.services[item + '.service']|default(none) is none or ansible_facts.services[item + '.service'].state != 'running'"