Change mount states to ensure uptime

The mount role was using the systemd module to start / stop mounts
however if a mount was restarted when it could have been reloaded
the role could create a fair amount of chaos in a running environment.
This change maps the mount states appropriately to the systemctl command
options to ensure we're not needlessly restarting mounts should the unit
files change. The `systemd_mount_states` has been added which will map
the normal Ansible states to suitable systemd mount states and the mount
state is being managed using the `systemctl` command instead of the
ansible module.

Change-Id: I5c7e5105e54d3ff9ad040f2a1d003d3dd12e4efb
Signed-off-by: Kevin Carter <kevin.carter@rackspace.com>
This commit is contained in:
Kevin Carter 2018-03-30 12:36:58 -05:00
parent 502662c562
commit d1dc867ef1
No known key found for this signature in database
GPG Key ID: 9443251A787B9FB3
3 changed files with 51 additions and 6 deletions

View File

@ -13,8 +13,13 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # 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 - 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 changed_when: false
register: mount_service_name register: mount_service_name
@ -46,20 +51,39 @@
tags: tags:
- systemd-mount - systemd-mount
- name: Load mount(s) - name: Load or Unload mount(s)
systemd: systemd:
daemon_reload: yes daemon_reload: yes
name: "{{ mount_service_name.stdout }}" name: "{{ mount_service_name.stdout }}"
enabled: "{{ item.enabled | default(true) }}" enabled: "{{ item.enabled | default(true) }}"
state: "{{ item.state | default(omit) }}"
when: when:
- item.state | default('unknown') != 'absent' - 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) - name: Unload mount(s)
systemd: systemd:
daemon_reload: yes daemon_reload: yes
name: "{{ mount_service_name.stdout }}" name: "{{ mount_service_name.stdout }}"
enabled: "stopped" enabled: false
no_block: yes no_block: yes
when: when:
- item.state | default('unknown') == 'absent' - item.state | default('unknown') == 'absent'

View File

@ -1,9 +1,9 @@
# {{ ansible_managed }} # {{ ansible_managed }}
[Unit] [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 }} What={{ item.what }}
{% if item.type == 'swap' %} {% if item.type == 'swap' %}
Priority={{ item.priority | default(0) }} Priority={{ item.priority | default(0) }}

21
vars/main.yml Normal file
View File

@ -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