From b479992a77c3b382ee1420e77609f8c831736313 Mon Sep 17 00:00:00 2001 From: Jordan Webb Date: Fri, 18 Dec 2020 12:41:48 -0600 Subject: [PATCH] Restart tunnels if their configuration has changed If an existing tunnel confiugration gets rewritten, restart the tunnel. Also, only reload systemd when the unit file changes; it is not neccessary to daemon-reload just to enable a new instance. --- tasks/add-tunnel.yml | 12 ++++++++++++ tasks/configure-tunnels.yml | 14 ++------------ tasks/configure.yml | 8 +++++++- 3 files changed, 21 insertions(+), 13 deletions(-) create mode 100644 tasks/add-tunnel.yml diff --git a/tasks/add-tunnel.yml b/tasks/add-tunnel.yml new file mode 100644 index 0000000..d71d25e --- /dev/null +++ b/tasks/add-tunnel.yml @@ -0,0 +1,12 @@ +- name: Create config file for service '{{ item.key }}' + template: + src: config.yml.j2 + dest: "{{ config_dir_tunnels }}/{{ item.key }}.yml" + register: tunnel_template +- name: Restart systemd service {{ item.key }} + systemd: + name: "{{ systemd_filename }}@{{ item.key }}" + state: restarted + enabled: yes + no_block: no + when: tunnel_template.changed diff --git a/tasks/configure-tunnels.yml b/tasks/configure-tunnels.yml index 67bdbdb..bc3fb6f 100644 --- a/tasks/configure-tunnels.yml +++ b/tasks/configure-tunnels.yml @@ -1,13 +1,3 @@ -- name: Create config file for service '{{ item.key }}' - template: - src: config.yml.j2 - dest: "{{ config_dir_tunnels }}/{{ item.key }}.yml" +- name: Configure tunnels + include_tasks: add-tunnel.yml with_dict: "{{ tunnels }}" -- name: Start systemd service {{ item.key }} - systemd: - name: "{{ systemd_filename }}@{{ item.key }}" - state: started - enabled: yes - daemon_reload: yes - no_block: no - with_dict: "{{ tunnels }}" \ No newline at end of file diff --git a/tasks/configure.yml b/tasks/configure.yml index 295eba1..7cc5843 100644 --- a/tasks/configure.yml +++ b/tasks/configure.yml @@ -1,4 +1,10 @@ - name: Install cloudflared service for service '{{ tunnel_name }}'' template: src: cloudflared.service.j2 - dest: "{{ systemd_target_dir }}/{{ systemd_filename }}@.service" \ No newline at end of file + dest: "{{ systemd_target_dir }}/{{ systemd_filename }}@.service" + register: service_template +- name: Reload systemd + systemd: + daemon_reload: yes + no_block: no + when: service_template.changed