filesystem: fix 355 state absent (#1149) (#1184)

* add support for filesystem removal (fix #355)

- Add 'state' option, defaults to 'present'.
- When state=absent, ignore other options (even 'dev' if the device
  doesn't exist)

* test filesystem state=absent (+ check_mode + idempotency)

* fix doc-required-mismatch

* add changelog fragment

* fix blkid return code

* ext4dev may be deprecated

* base checks on UUID instead

* Update changelogs/fragments/1149-filesystem-fix-355-state-absent.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

* Update plugins/modules/system/filesystem.py (version_added)

Co-authored-by: Felix Fontein <felix@fontein.de>

* use array for new run_command() calls; do not wipefs if no fs found

* use dd as a fallback

* do not use bare 'except' (pep8)

* force string type

* use dd anyway (wipefs not supported everywhere, possibly buggy with vfat, etc.)

* do not truncate regular files; update changelog fragment

* doc: update state description and an example; notice check_mode support

* do not wipe mounted fs, fail instead

* back to wipefs implementation

* update test's main conditions

* update changelog fragment

* explicit types

* fail state=absent on freebsd

* remove doc-missing-type exceptions (2.9, 2.10, 2.11)

Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit a5ca990857)

Co-authored-by: quidame <quidame@poivron.org>
This commit is contained in:
patchback[bot] 2020-10-27 05:47:47 +01:00 committed by GitHub
commit c4e93b0b5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 194 additions and 34 deletions

View file

@ -43,7 +43,12 @@
- 'not (item.0.key == "f2fs" and ansible_distribution == "Ubuntu" and ansible_distribution_version is version("14.04", "<="))'
- 'not (item.1 == "overwrite_another_fs" and ansible_system == "FreeBSD")'
- 'not (item.1 == "remove_fs" and ansible_system == "FreeBSD")' # util-linux not available on FreeBSD
# On CentOS 6 shippable containers, wipefs seems unable to remove vfat signatures
- 'not (item.1 == "remove_fs" and item.0.key == "vfat" and ansible_distribution == "CentOS" and
ansible_distribution_version is version("7.0", "<"))'
# The xfsprogs package on newer versions of OpenSUSE (15+) require Python 3, we skip this on our Python 2 container
# OpenSUSE 42.3 Python2 and the other py3 containers are not affected so we will continue to run that
- 'not (item.0.key == "xfs" and ansible_os_family == "Suse" and ansible_python.version.major == 2 and ansible_distribution_major_version|int != 42)'
loop: "{{ query('dict', tested_filesystems)|product(['create_fs', 'overwrite_another_fs'])|list }}"
loop: "{{ query('dict', tested_filesystems)|product(['create_fs', 'overwrite_another_fs', 'remove_fs'])|list }}"

View file

@ -0,0 +1,98 @@
---
# We assume 'create_fs' tests have passed.
- name: filesystem creation
filesystem:
dev: '{{ dev }}'
fstype: '{{ fstype }}'
- name: get filesystem UUID with 'blkid'
command:
cmd: 'blkid -c /dev/null -o value -s UUID {{ dev }}'
changed_when: false
register: blkid_ref
- name: Assert that a filesystem exists on top of the device
assert:
that:
- blkid_ref.stdout | length > 0
# Test check_mode first
- name: filesystem removal (check mode)
filesystem:
dev: '{{ dev }}'
state: absent
register: wipefs
check_mode: yes
- name: get filesystem UUID with 'blkid' (should remain the same)
command:
cmd: 'blkid -c /dev/null -o value -s UUID {{ dev }}'
changed_when: false
register: blkid
- name: Assert that the state changed but the filesystem still exists
assert:
that:
- wipefs is changed
- blkid.stdout == blkid_ref.stdout
# Do it
- name: filesystem removal
filesystem:
dev: '{{ dev }}'
state: absent
register: wipefs
- name: get filesystem UUID with 'blkid' (should be empty)
command:
cmd: 'blkid -c /dev/null -o value -s UUID {{ dev }}'
changed_when: false
failed_when: false
register: blkid
- name: Assert that the state changed and the device has no filesystem
assert:
that:
- wipefs is changed
- blkid.stdout | length == 0
- blkid.rc == 2
# Do it again
- name: filesystem removal (idempotency)
filesystem:
dev: '{{ dev }}'
state: absent
register: wipefs
- name: Assert that the state did not change
assert:
that:
- wipefs is not changed
# and again
- name: filesystem removal (idempotency, check mode)
filesystem:
dev: '{{ dev }}'
state: absent
register: wipefs
check_mode: yes
- name: Assert that the state did not change
assert:
that:
- wipefs is not changed
# By the way, test removal of a filesystem on unexistent device
- name: filesystem removal (unexistent device)
filesystem:
dev: '/dev/unexistent_device'
state: absent
register: wipefs
- name: Assert that the state did not change
assert:
that:
- wipefs is not changed