mirror of
https://github.com/ansible-collections/ansible.posix.git
synced 2026-11-21 14:48:44 +00:00
A new `absent_keep_mountpoint` option has been added to the state. This option keeps the mount point from being deleted when the state is absent. Co-authored-by: Michael Sprott <165604036+sprottm@users.noreply.github.com>
This commit is contained in:
co-authored by
Michael Sprott
parent
7811740296
commit
e03234e855
@@ -0,0 +1,2 @@
|
||||
minor_changes:
|
||||
- "Introduce an option to remove only the mount and fstab entry but not the mountpoint (https://github.com/ansible-collections/ansible.posix/issues/331)."
|
||||
@@ -88,6 +88,13 @@ options:
|
||||
fail if multiple devices are mounted on the same mount point. Using
|
||||
V(absent) with a mount point that is not registered in the I(fstab) has
|
||||
no effect, use V(unmounted) instead.
|
||||
- V(absent_keep_mountpoint) specifies that the mount point entry O(path) will be removed
|
||||
from I(fstab) and will also unmount the mounted device. The associated
|
||||
mountpoint will not be removed. A mounted device will be unmounted regardless
|
||||
of O(src) or its real source. V(absent_keep_mountpoint) does not unmount recursively,
|
||||
and the module will fail if multiple devices are mounted on the same mount
|
||||
point. Using V(absent_keep_mountpoint) with a mount point that is not registered in the
|
||||
I(fstab) has no effect, use V(unmounted) instead.
|
||||
- V(remounted) specifies that the device will be remounted for when you
|
||||
want to force a refresh on the mount itself (added in 2.9). This will
|
||||
always return RV(ignore:changed=true). If O(opts) is set, the options will be
|
||||
@@ -102,7 +109,7 @@ options:
|
||||
mountpoint.
|
||||
type: str
|
||||
required: true
|
||||
choices: [ absent, absent_from_fstab, mounted, present, unmounted, remounted, ephemeral ]
|
||||
choices: [ absent, absent_from_fstab, absent_keep_mountpoint, mounted, present, unmounted, remounted, ephemeral ]
|
||||
fstab:
|
||||
description:
|
||||
- File to use instead of C(/etc/fstab).
|
||||
@@ -170,6 +177,11 @@ EXAMPLES = r'''
|
||||
path: /tmp/mnt-pnt
|
||||
state: unmounted
|
||||
|
||||
- name: Unmount a mounted volume and remove its fstab entry
|
||||
ansible.posix.mount:
|
||||
path: /tmp/mnt-pnt
|
||||
state: absent_keep_mountpoint
|
||||
|
||||
- name: Remount a mounted volume
|
||||
ansible.posix.mount:
|
||||
path: /tmp/mnt-pnt
|
||||
@@ -774,7 +786,20 @@ def main():
|
||||
passno=dict(type='str', no_log=False, default='0'),
|
||||
src=dict(type='path'),
|
||||
backup=dict(type='bool', default=False),
|
||||
state=dict(type='str', required=True, choices=['absent', 'absent_from_fstab', 'mounted', 'present', 'unmounted', 'remounted', 'ephemeral']),
|
||||
state={
|
||||
'type': 'str',
|
||||
'required': True,
|
||||
'choices': [
|
||||
'absent',
|
||||
'absent_from_fstab',
|
||||
'absent_keep_mountpoint',
|
||||
'mounted',
|
||||
'present',
|
||||
'unmounted',
|
||||
'remounted',
|
||||
'ephemeral'
|
||||
]
|
||||
},
|
||||
),
|
||||
supports_check_mode=True,
|
||||
required_if=(
|
||||
@@ -883,7 +908,7 @@ def main():
|
||||
|
||||
if state == 'absent_from_fstab':
|
||||
name, changed, _dummy = unset_mount(module, args, backup=False)
|
||||
elif state == 'absent':
|
||||
elif state == 'absent' or state == 'absent_keep_mountpoint':
|
||||
name, changed, backup_lines = unset_mount(module, args, backup=True)
|
||||
|
||||
if changed and not module.check_mode:
|
||||
@@ -895,7 +920,7 @@ def main():
|
||||
write_fstab(module, backup_lines, args['fstab'])
|
||||
module.fail_json(msg="Error unmounting %s: %s" % (name, msg))
|
||||
|
||||
if os.path.exists(name):
|
||||
if os.path.exists(name) and state == 'absent':
|
||||
try:
|
||||
os.rmdir(name)
|
||||
except (OSError, IOError) as e:
|
||||
|
||||
@@ -297,10 +297,17 @@
|
||||
when: ansible_system in ('FreeBSD', 'Linux')
|
||||
register: dest_stat
|
||||
|
||||
- name: Check if the mountpoint still exists
|
||||
ansible.builtin.stat:
|
||||
path: '{{ output_dir }}/mount_dest'
|
||||
when: ansible_system in ('FreeBSD', 'Linux')
|
||||
register: mountpoint_stat
|
||||
|
||||
- name: Check that we did not unmount
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- unmount_result['changed']
|
||||
- mountpoint_stat['stat']['exists']
|
||||
- dest_stat['stat']['exists']
|
||||
when: ansible_system in ('FreeBSD', 'Linux')
|
||||
|
||||
@@ -318,10 +325,65 @@
|
||||
when: ansible_system in ('FreeBSD', 'Linux')
|
||||
register: dest_stat
|
||||
|
||||
- name: Check that the mountpoint no longer exists
|
||||
ansible.builtin.stat:
|
||||
path: '{{ output_dir }}/mount_dest'
|
||||
when: ansible_system in ('FreeBSD', 'Linux')
|
||||
register: mountpoint_stat
|
||||
|
||||
- name: Check that we unmounted
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- unmount_result['changed']
|
||||
- not mountpoint_stat['stat']['exists']
|
||||
- not dest_stat['stat']['exists']
|
||||
when: ansible_system in ('FreeBSD', 'Linux')
|
||||
|
||||
# unmount (absent_keep_mountpoint)
|
||||
- name: Bind mount a filesystem (Linux)
|
||||
ansible.posix.mount:
|
||||
src: '{{ output_dir }}/mount_source'
|
||||
name: '{{ output_dir }}/mount_dest'
|
||||
state: mounted
|
||||
fstype: None
|
||||
opts: bind
|
||||
when: ansible_system == 'Linux'
|
||||
register: bind_result_linux
|
||||
|
||||
- name: Bind mount a filesystem (FreeBSD)
|
||||
ansible.posix.mount:
|
||||
src: '{{ output_dir }}/mount_source'
|
||||
name: '{{ output_dir }}/mount_dest'
|
||||
state: mounted
|
||||
fstype: nullfs
|
||||
when: ansible_system == 'FreeBSD'
|
||||
register: bind_result_freebsd
|
||||
|
||||
- name: Unmount the bind mount while keeping the mountpoint
|
||||
ansible.posix.mount:
|
||||
name: '{{ output_dir }}/mount_dest'
|
||||
state: absent_keep_mountpoint
|
||||
when: ansible_system in ('Linux', 'FreeBSD')
|
||||
register: unmount_result
|
||||
|
||||
- name: Make sure the file no longer exists in dest
|
||||
ansible.builtin.stat:
|
||||
path: '{{ output_dir }}/mount_dest/test_file'
|
||||
when: ansible_system in ('FreeBSD', 'Linux')
|
||||
register: dest_stat
|
||||
|
||||
- name: Check if the mountpoint still exists
|
||||
ansible.builtin.stat:
|
||||
path: '{{ output_dir }}/mount_dest'
|
||||
when: ansible_system in ('FreeBSD', 'Linux')
|
||||
register: mountpoint_stat
|
||||
|
||||
- name: Check that we unmounted and that the mountpoint still exists
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- (ansible_system == 'Linux' and bind_result_linux['changed']) or (ansible_system == 'FreeBSD' and bind_result_freebsd['changed'])
|
||||
- unmount_result['changed']
|
||||
- mountpoint_stat['stat']['exists']
|
||||
- not dest_stat['stat']['exists']
|
||||
when: ansible_system in ('FreeBSD', 'Linux')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user