From c59df1fd13bdb58aed931958b1c2e163d5aa517d Mon Sep 17 00:00:00 2001 From: vincent Date: Sat, 12 Jan 2019 19:12:55 +0100 Subject: [PATCH] create auto fs service --- defaults/main.yml | 14 +++++++++++++ handlers/main.yml | 37 ++++++++++++++++++++++++++++++++++ meta/main.yml | 0 tasks/main.yml | 31 ++++++++++++++++++++++++++++ templates/systemd.automount.j2 | 9 +++++++++ templates/systemd.mount.j2 | 12 +++++++++++ 6 files changed, 103 insertions(+) delete mode 100644 meta/main.yml create mode 100644 templates/systemd.automount.j2 create mode 100644 templates/systemd.mount.j2 diff --git a/defaults/main.yml b/defaults/main.yml index e69de29..89ab376 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -0,0 +1,14 @@ +systemd_mounts: {} +systemd_mounts_enabled: [] + +systemd_mounts_manage_service: {} +systemd_mounts_allow_reload: {} +autofs_mounts_manage_service: {} +autofs_mounts_allow_reload: {} + + + +systemd_mount_packages: + - cifs-utils + - nfs-utils + - davfs2 \ No newline at end of file diff --git a/handlers/main.yml b/handlers/main.yml index e69de29..51d5955 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -0,0 +1,37 @@ + +- name: Reload systemd + systemd: + daemon_reload: yes + when: systemd_mounts_manage_service + +- name: Start systemd mount + systemd: + name: "{{ item.value.mount[1:] | replace('-', '\\x2d') | replace('/', \"-\") }}.mount" + state: started + with_dict: "{{ systemd_mounts }}" + when: systemd_mounts_manage_service + +- name: Start systemd automount + systemd: + name: "{{ item.value.mount[1:] | replace('-', '\\x2d') | replace('/', \"-\") }}.automount" + state: started + with_dict: "{{ systemd_mounts }}" + when: systemd_mounts_manage_service + +- name: Enable systemd mount + systemd: + name: "{{ item.value.mount[1:] | replace('-', '\\x2d') | replace('/', \"-\") }}.mount" + enabled: yes + with_dict: "{{ systemd_mounts }}" + notify: + - Start systemd mount + when: item.value.automount == false + +- name: Enable systemd automount + systemd: + name: "{{ item.value.mount[1:] | replace('-', '\\x2d') | replace('/', \"-\") }}.automount" + enabled: yes + with_dict: "{{ systemd_mounts }}" + notify: + - Start systemd automount + when: item.value.automount == true diff --git a/meta/main.yml b/meta/main.yml deleted file mode 100644 index e69de29..0000000 diff --git a/tasks/main.yml b/tasks/main.yml index e69de29..d0505a7 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -0,0 +1,31 @@ + +# +# tasks for mounts +# systemd uses - for dir separator, so dirs with dashes need escaped according to systemd-escape rules +# + +- name: SYSTEMD MOUNT | Install needed packages + package: + name: "{{ item }}" + state: installed + with_items: "{{ systemd_mount_packages }}" + +- name: SYSTEMD MOUNT | Setup systemd Service for mountpoints + template: + src: systemd.mount.j2 + dest: "/etc/systemd/system/{{ item.value.mount[1:] | replace('-', '\\x2d') | replace('/', '-') }}.mount" + with_dict: "{{ systemd_mounts }}" + notify: + - Reload systemd + - Enable systemd mount + when: item.key in systemd_mounts_enabled + +- name: SYSTEMD MOUNT | Setup systemd Service for automount + template: + src: systemd.automount.j2 + dest: "/etc/systemd/system/{{ item.value.mount[1:] | replace('-', '\\x2d') | replace('/', '-') }}.automount" + with_dict: "{{ systemd_mounts }}" + notify: + - Reload systemd + - Enable systemd automount + when: item.value.automount is defined and item.value.automount == true and item.key in systemd_mounts_enabled \ No newline at end of file diff --git a/templates/systemd.automount.j2 b/templates/systemd.automount.j2 new file mode 100644 index 0000000..ec0850d --- /dev/null +++ b/templates/systemd.automount.j2 @@ -0,0 +1,9 @@ +[Unit] +Description=Automount {{ item.key }} +After=network.target multi-user.target + +[Automount] +Where={{ item.value.mount }} + +[Install] +WantedBy=default.target \ No newline at end of file diff --git a/templates/systemd.mount.j2 b/templates/systemd.mount.j2 new file mode 100644 index 0000000..a515d02 --- /dev/null +++ b/templates/systemd.mount.j2 @@ -0,0 +1,12 @@ +[Unit] +Description=Mount {{ item.key }} +After=network.target multi-user.target + +[Mount] +What={{ item.value.share }} +Where={{ item.value.mount }} +Type={{ item.value.type | default ('mounts_type') }} +Options={{ item.value.options | join(',') | default ('mounts_options') }} + +[Install] +WantedBy=default.target \ No newline at end of file