2016-09-23 21:38:31 +02:00
|
|
|
# (c) 2016, Toshio Kuratomi <tkuratomi@ansible.com>
|
|
|
|
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
- name: Create the mount point
|
|
|
|
file:
|
|
|
|
state: "directory"
|
2016-11-22 01:23:40 +01:00
|
|
|
path: "{{ output_dir }}/mount_dest"
|
2016-09-23 21:38:31 +02:00
|
|
|
|
|
|
|
- name: Create a directory to bind mount
|
|
|
|
file:
|
|
|
|
state: "directory"
|
2016-11-22 01:23:40 +01:00
|
|
|
path: "{{ output_dir }}/mount_source"
|
2016-09-23 21:38:31 +02:00
|
|
|
|
|
|
|
- name: Put something in the directory so we see that it worked
|
|
|
|
copy:
|
|
|
|
content: "Testing\n"
|
2016-11-22 01:23:40 +01:00
|
|
|
dest: "{{ output_dir }}/mount_source/test_file"
|
2016-09-23 21:38:31 +02:00
|
|
|
register: orig_info
|
|
|
|
|
|
|
|
# The opts type of bind mount only works on Linux
|
|
|
|
- name: Bind mount a filesystem (Linux)
|
|
|
|
mount:
|
2016-11-22 01:23:40 +01:00
|
|
|
src: "{{ output_dir }}/mount_source"
|
|
|
|
name: "{{ output_dir }}/mount_dest"
|
2016-09-23 21:38:31 +02:00
|
|
|
state: "mounted"
|
|
|
|
fstype: "None"
|
|
|
|
opts: "bind"
|
|
|
|
when: ansible_system == 'Linux'
|
|
|
|
register: bind_result_linux
|
|
|
|
|
|
|
|
# Nullfs is freebsd only
|
|
|
|
- name: Bind mount a filesystem (FreeBSD)
|
|
|
|
mount:
|
2016-11-22 01:23:40 +01:00
|
|
|
src: "{{ output_dir }}/mount_source"
|
|
|
|
name: "{{ output_dir }}/mount_dest"
|
2016-09-23 21:38:31 +02:00
|
|
|
state: "mounted"
|
|
|
|
fstype: "nullfs"
|
|
|
|
when: ansible_system == 'FreeBSD'
|
|
|
|
register: bind_result_freebsd
|
|
|
|
|
|
|
|
- name: get checksum for bind mounted file
|
|
|
|
stat:
|
2016-11-22 01:23:40 +01:00
|
|
|
path: "{{ output_dir }}/mount_dest/test_file"
|
2016-09-23 21:38:31 +02:00
|
|
|
when: ansible_system in ('FreeBSD', 'Linux')
|
|
|
|
register: dest_stat
|
|
|
|
|
|
|
|
- name: assert the bind mount was successful
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "(ansible_system == 'Linux' and bind_result_linux['changed']) or (ansible_system == 'FreeBSD' and bind_result_freebsd['changed'])"
|
|
|
|
- "dest_stat['stat']['exists']"
|
|
|
|
- "orig_info['checksum'] == dest_stat['stat']['checksum']"
|
|
|
|
when: ansible_system in ('FreeBSD', 'Linux')
|
|
|
|
|
|
|
|
# The opts type of bind mount only works on Linux
|
|
|
|
- name: Bind mount a filesystem (Linux)
|
|
|
|
mount:
|
2016-11-22 01:23:40 +01:00
|
|
|
src: "{{ output_dir }}/mount_source"
|
|
|
|
name: "{{ output_dir }}/mount_dest"
|
2016-09-23 21:38:31 +02:00
|
|
|
state: "mounted"
|
|
|
|
fstype: "None"
|
|
|
|
opts: "bind"
|
|
|
|
when: ansible_system == 'Linux'
|
|
|
|
register: bind_result_linux
|
|
|
|
|
|
|
|
# Nullfs is freebsd only
|
|
|
|
- name: Bind mount a filesystem (FreeBSD)
|
|
|
|
mount:
|
2016-11-22 01:23:40 +01:00
|
|
|
src: "{{ output_dir }}/mount_source"
|
|
|
|
name: "{{ output_dir }}/mount_dest"
|
2016-09-23 21:38:31 +02:00
|
|
|
state: "mounted"
|
|
|
|
fstype: "nullfs"
|
|
|
|
when: ansible_system == 'FreeBSD'
|
|
|
|
register: bind_result_freebsd
|
|
|
|
|
|
|
|
- name: Make sure we didn't mount a second time
|
|
|
|
assert:
|
|
|
|
that:
|
2016-11-20 01:24:16 +01:00
|
|
|
- "(ansible_system == 'Linux' and not bind_result_linux['changed']) or (ansible_system == 'FreeBSD' and not bind_result_freebsd['changed'])"
|
|
|
|
when: ansible_system in ('FreeBSD', 'Linux')
|
|
|
|
|
|
|
|
# The opts type of bind mount only works on Linux
|
|
|
|
- name: Remount filesystem with different opts (Linux)
|
|
|
|
mount:
|
2016-11-22 01:23:40 +01:00
|
|
|
src: "{{ output_dir }}/mount_source"
|
|
|
|
name: "{{ output_dir }}/mount_dest"
|
2016-11-20 01:24:16 +01:00
|
|
|
state: "mounted"
|
|
|
|
fstype: "None"
|
|
|
|
opts: "bind,ro"
|
|
|
|
when: ansible_system == 'Linux'
|
|
|
|
register: bind_result_linux
|
|
|
|
|
|
|
|
# Nullfs is freebsd only
|
|
|
|
- name: Remount filesystem with different opts (FreeBSD)
|
|
|
|
mount:
|
2016-11-22 01:23:40 +01:00
|
|
|
src: "{{ output_dir }}/mount_source"
|
|
|
|
name: "{{ output_dir }}/mount_dest"
|
2016-11-20 01:24:16 +01:00
|
|
|
state: "mounted"
|
|
|
|
fstype: "nullfs"
|
|
|
|
opts: "ro"
|
|
|
|
when: ansible_system == 'FreeBSD'
|
|
|
|
register: bind_result_freebsd
|
|
|
|
|
|
|
|
- name: Get mount options
|
|
|
|
shell: mount | grep mount_dest | grep -E -w '(ro|read-only)' | wc -l
|
|
|
|
register: remount_options
|
|
|
|
|
|
|
|
- name: Make sure the filesystem now has the new opts
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "(ansible_system == 'Linux' and bind_result_linux['changed']) or (ansible_system == 'FreeBSD' and bind_result_freebsd['changed'])"
|
|
|
|
- "'1' in remount_options.stdout"
|
|
|
|
- "1 == remount_options.stdout_lines | length"
|
|
|
|
when: ansible_system in ('FreeBSD', 'Linux')
|
2016-09-23 21:38:31 +02:00
|
|
|
|
|
|
|
- name: Unmount the bind mount
|
|
|
|
mount:
|
2016-11-22 01:23:40 +01:00
|
|
|
name: "{{ output_dir }}/mount_dest"
|
2016-09-23 21:38:31 +02:00
|
|
|
state: "absent"
|
|
|
|
when: ansible_system in ('Linux', 'FreeBSD')
|
|
|
|
register: unmount_result
|
|
|
|
|
|
|
|
- name: Make sure the file no longer exists in dest
|
|
|
|
stat:
|
2016-11-22 01:23:40 +01:00
|
|
|
path: "{{ output_dir }}/mount_dest/test_file"
|
2016-09-23 21:38:31 +02:00
|
|
|
when: ansible_system in ('FreeBSD', 'Linux')
|
|
|
|
register: dest_stat
|
|
|
|
|
|
|
|
- name: Check that we unmounted
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "unmount_result['changed']"
|
|
|
|
- "not dest_stat['stat']['exists']"
|
|
|
|
when: ansible_system in ('FreeBSD', 'Linux')
|
2018-07-31 23:09:38 +02:00
|
|
|
|
|
|
|
- name: Create fstab record for the first swap file
|
|
|
|
mount:
|
|
|
|
name: none
|
|
|
|
src: /tmp/swap1
|
|
|
|
opts: sw
|
|
|
|
fstype: swap
|
|
|
|
state: present
|
|
|
|
register: swap1_created
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Try to create fstab record for the first swap file again
|
|
|
|
mount:
|
|
|
|
name: none
|
|
|
|
src: /tmp/swap1
|
|
|
|
opts: sw
|
|
|
|
fstype: swap
|
|
|
|
state: present
|
|
|
|
register: swap1_created_again
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Check that we created the swap1 record
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "swap1_created['changed']"
|
|
|
|
- "not swap1_created_again['changed']"
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Create fstab record for the second swap file
|
|
|
|
mount:
|
|
|
|
name: none
|
|
|
|
src: /tmp/swap2
|
|
|
|
opts: sw
|
|
|
|
fstype: swap
|
|
|
|
state: present
|
|
|
|
register: swap2_created
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Try to create fstab record for the second swap file again
|
|
|
|
mount:
|
|
|
|
name: none
|
|
|
|
src: /tmp/swap1
|
|
|
|
opts: sw
|
|
|
|
fstype: swap
|
|
|
|
state: present
|
|
|
|
register: swap2_created_again
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Check that we created the swap2 record
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "swap2_created['changed']"
|
|
|
|
- "not swap2_created_again['changed']"
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Remove the fstab record for the first swap file
|
|
|
|
mount:
|
|
|
|
name: none
|
|
|
|
src: /tmp/swap1
|
|
|
|
state: absent
|
|
|
|
register: swap1_removed
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Try to remove the fstab record for the first swap file again
|
|
|
|
mount:
|
|
|
|
name: none
|
|
|
|
src: /tmp/swap1
|
|
|
|
state: absent
|
|
|
|
register: swap1_removed_again
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Check that we removed the swap1 record
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "swap1_removed['changed']"
|
|
|
|
- "not swap1_removed_again['changed']"
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Remove the fstab record for the second swap file
|
|
|
|
mount:
|
|
|
|
name: none
|
|
|
|
src: /tmp/swap2
|
|
|
|
state: absent
|
|
|
|
register: swap2_removed
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Try to remove the fstab record for the second swap file again
|
|
|
|
mount:
|
|
|
|
name: none
|
|
|
|
src: /tmp/swap2
|
|
|
|
state: absent
|
|
|
|
register: swap2_removed_again
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Check that we removed the swap2 record
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "swap2_removed['changed']"
|
|
|
|
- "not swap2_removed_again['changed']"
|
|
|
|
when: ansible_system in ('Linux')
|
2019-02-28 20:09:30 +01:00
|
|
|
|
|
|
|
- name: Create fstab record with missing last two fields
|
|
|
|
copy:
|
|
|
|
dest: /etc/fstab
|
|
|
|
content: |
|
|
|
|
//nas/photo /home/jik/pictures cifs defaults,credentials=/etc/security/nas.creds,uid=jik,gid=users,forceuid,forcegid,noserverino,_netdev
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Try to change the fstab record with the missing last two fields
|
|
|
|
mount:
|
|
|
|
src: //nas/photo
|
|
|
|
path: /home/jik/pictures
|
|
|
|
fstype: cifs
|
|
|
|
opts: defaults,credentials=/etc/security/nas.creds,uid=jik,gid=users,forceuid,forcegid,noserverino,_netdev,x-systemd.mount-timeout=0
|
|
|
|
state: present
|
|
|
|
register: optional_fields_update
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Get the content of the fstab file
|
|
|
|
shell: cat /etc/fstab
|
|
|
|
register: optional_fields_content
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Check if the line containing the missing last two fields was changed
|
|
|
|
assert:
|
|
|
|
that:
|
|
|
|
- "optional_fields_update['changed']"
|
|
|
|
- "' 0 0' in optional_fields_content.stdout"
|
|
|
|
- "1 == optional_fields_content.stdout_lines | length"
|
|
|
|
when: ansible_system in ('Linux')
|
2019-07-19 08:59:27 +02:00
|
|
|
|
|
|
|
- name: Block to test remounted option
|
|
|
|
block:
|
|
|
|
- name: Create empty file
|
|
|
|
command: dd if=/dev/zero of=/tmp/myfs.img bs=1048576 count=20
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Format FS
|
|
|
|
filesystem:
|
|
|
|
fstype: ext3
|
|
|
|
dev: /tmp/myfs.img
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Mount the FS for the first time
|
|
|
|
mount:
|
|
|
|
path: /tmp/myfs
|
|
|
|
src: /tmp/myfs.img
|
|
|
|
fstype: ext2
|
|
|
|
state: mounted
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Get the last write time
|
|
|
|
shell: "dumpe2fs /tmp/myfs.img 2>/dev/null | grep -i last\ write\ time: |cut -d: -f2-"
|
|
|
|
register: last_write_time
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Wait 2 second
|
|
|
|
pause:
|
|
|
|
seconds: 2
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Test if the FS is remounted
|
|
|
|
mount:
|
|
|
|
path: /tmp/myfs
|
|
|
|
state: remounted
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Get again the last write time
|
|
|
|
shell: "dumpe2fs /tmp/myfs.img 2>/dev/null | grep -i last\ write\ time: |cut -d: -f2-"
|
|
|
|
register: last_write_time2
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Fail if they are the same
|
|
|
|
fail:
|
|
|
|
msg: "Filesytem was not remounted, testing of the module failed!"
|
|
|
|
when: last_write is defined and last_write_time2 is defined and last_write_time.stdout == last_write_time2.stdout and ansible_system in ('Linux')
|
|
|
|
always:
|
|
|
|
- name: Umount the test FS
|
|
|
|
mount:
|
|
|
|
path: /tmp/myfs
|
|
|
|
src: /tmp/myfs.img
|
|
|
|
opts: loop
|
|
|
|
state: absent
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
|
|
|
|
- name: Remove the test FS
|
|
|
|
file:
|
|
|
|
path: "{{ item }}"
|
|
|
|
state: absent
|
|
|
|
loop:
|
|
|
|
- /tmp/myfs.img
|
|
|
|
- /tmp/myfs
|
|
|
|
when: ansible_system in ('Linux')
|
|
|
|
...
|