diff --git a/roles/autossh/defaults/main.yaml b/roles/autossh/defaults/main.yaml index e319a85..de193f8 100644 --- a/roles/autossh/defaults/main.yaml +++ b/roles/autossh/defaults/main.yaml @@ -4,6 +4,7 @@ autossh_package_state: present autossh_service_name: autossh@.service autossh_systemd_unit_path: /etc/systemd/system/autossh@.service +autossh_systemd_unit_state: present autossh_etc_path: /etc/autossh autossh_run_path: /run/autossh diff --git a/roles/autossh/tasks/main.yaml b/roles/autossh/tasks/main.yaml index 4039483..d681419 100644 --- a/roles/autossh/tasks/main.yaml +++ b/roles/autossh/tasks/main.yaml @@ -71,8 +71,30 @@ notify: reload autossh instances - name: manage services - service: + systemd: name: "autossh@{{ item.name }}.service" - state: "{{ item.state | default('started') }}" - enabled: "{{ item.enabled | default(true) }}" + state: "{{ (item.state | default('present') == 'absent') | ternary('stopped', 'started') }}" + enabled: "{{ item.state | default('present') != 'absent' }}" + daemon_reload: true loop: "{{ autossh_config | default([]) }}" + +- name: remove systemd unit + file: + path: "{{ autossh_systemd_unit_path }}" + state: absent + notify: + - autossh daemon-reload + when: + - ansible_service_mgr == 'systemd' + - autossh_systemd_unit_state == "absent" + no_log: true + +- name: configure autossh tunnels + file: + path: "{{ autossh_etc_path }}/{{ item.name }}" + state: absent + loop: "{{ autossh_config | default([]) }}" + when: + - ansible_service_mgr == 'systemd' + - item.state is defined + - item.state == "absent"