diff --git a/tasks/systemd_mounts.yml b/tasks/systemd_mounts.yml index 563c7c0..558f2e3 100644 --- a/tasks/systemd_mounts.yml +++ b/tasks/systemd_mounts.yml @@ -13,8 +13,13 @@ # See the License for the specific language governing permissions and # limitations under the License. +- name: Set mount facts + set_fact: + systemd_mount_suffix: "{{ (item.type == 'swap') | ternary('swap', 'mount') }}" + systemd_mount_item: "{{ item.where | default(item.what) }}" + - name: Escape mount service file name - command: systemd-escape -p --suffix="{{ (item.type == 'swap') | ternary('swap', 'mount') }}" "{{ item.where | default(item.what) }}" + command: systemd-escape -p --suffix="{{ systemd_mount_suffix }}" "{{ systemd_mount_item }}" changed_when: false register: mount_service_name @@ -46,20 +51,39 @@ tags: - systemd-mount -- name: Load mount(s) +- name: Load or Unload mount(s) systemd: daemon_reload: yes name: "{{ mount_service_name.stdout }}" enabled: "{{ item.enabled | default(true) }}" - state: "{{ item.state | default(omit) }}" when: - item.state | default('unknown') != 'absent' +# NOTE(cloudnull): The systemd module is not used to start the +# mount because we don't want to inavertently +# "restart" a mount unnecessarily. To ensure +# we're able to load new options without +# requiring a mount restart the systemctl +# command is used with the "reload-or-restart" +# argument. Additionally this command escapes +# the name of the mount. +- name: Set the state of the mount + shell: >- + systemctl + {{ systemd_mount_states[item.state] }} + $(systemd-escape -p --suffix="{{ systemd_mount_suffix }}" "{{ systemd_mount_item }}") + args: + warn: no + when: + - item.state is defined + tags: + - skip_ansible_lint + - name: Unload mount(s) systemd: daemon_reload: yes name: "{{ mount_service_name.stdout }}" - enabled: "stopped" + enabled: false no_block: yes when: - item.state | default('unknown') == 'absent' diff --git a/templates/systemd-mount.j2 b/templates/systemd-mount.j2 index 9773ab4..d9b7bd1 100644 --- a/templates/systemd-mount.j2 +++ b/templates/systemd-mount.j2 @@ -1,9 +1,9 @@ # {{ ansible_managed }} [Unit] -Description=Auto mount for {{ item.where | default(item.what) }} +Description=Auto mount for {{ systemd_mount_item }} -[{{ (item.type == 'swap') | ternary('Swap', 'Mount') }}] +[{{ systemd_mount_suffix | capitalize }}] What={{ item.what }} {% if item.type == 'swap' %} Priority={{ item.priority | default(0) }} diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..f36ae87 --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,21 @@ +--- +# Copyright 2018, Rackspace US, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +systemd_mount_states: + reloaded: reload + restarted: reload-or-restart + started: reload-or-restart + stopped: stopped + absent: stopped