Compare commits

..

1 commit

Author SHA1 Message Date
Adam Miller
13b5895af2
Merge 9a616f3cc0 into 05ee6ebc2a 2023-12-05 03:03:29 +00:00
3 changed files with 162 additions and 185 deletions

View file

@ -20,7 +20,9 @@
register: result
- name: Get the file content
ansible.builtin.command: fgrep DATA_BASIC "{{ output_dir | expanduser }}/authorized_keys"
ansible.builtin.shell:
cmd: set -o pipefail && cat "{{ output_dir | expanduser }}/authorized_keys" | fgrep DATA_BASIC
executable: /bin/bash
changed_when: false
register: content

View file

@ -240,7 +240,9 @@
- result.key_options == "no-agent-forwarding,no-X11-forwarding,permitopen=\"10.9.8.1:8080\",permitopen=\"10.9.8.1:9001\""
- name: Get the file content
ansible.builtin.command: fgrep DATA_BASIC "{{ output_dir | expanduser }}/authorized_keys"
ansible.builtin.shell:
cmd: set -o pipefail && cat "{{ output_dir | expanduser }}/authorized_keys" | fgrep DATA_BASIC
executable: /bin/bash
changed_when: false
register: content

View file

@ -1,119 +1,33 @@
- name: Install dependencies (Linux)
---
- name: Install dependencies
ansible.builtin.package:
name: e2fsprogs
state: present
when: ansible_system == 'Linux'
- name: Install dependencies (FreeBSD)
ansible.builtin.package:
name: bash
state: present
- name: Register facts on Linux
ansible.builtin.set_fact:
shell_executable: /bin/bash
ephemeral_device_a: /tmp/myfs_A.img
ephemeral_device_b: /tmp/myfs_B.img
ephemeral_fstype: ext3
ephemeral_fstab: /etc/fstab
when: ansible_system == 'Linux'
- name: Register facts on Solaris/SunOS
ansible.builtin.set_fact:
shell_executable: /usr/bin/bash
ephemeral_device_a: /dev/lofi/1
ephemeral_device_b: /dev/lofi/2
ephemeral_create_loop_dev_cmd: >-
lofiadm -a /tmp/myfs_A.img /dev/lofi/1 &&
lofiadm -a /tmp/myfs_B.img /dev/lofi/2
ephemeral_remove_loop_dev_cmd: >-
lofiadm -d /dev/lofi/1 &&
lofiadm -d /dev/lofi/2 || true
ephemeral_fstype: ufs
ephemeral_fstab: /etc/vfstab
when: ansible_system == 'SunOS'
- name: Register facts on FreeBSD
ansible.builtin.set_fact:
shell_executable: /usr/local/bin/bash
ephemeral_device_a: /dev/md1
ephemeral_device_b: /dev/md2
ephemeral_create_loop_dev_cmd: >-
mdconfig -a -t vnode -f /tmp/myfs_A.img -u /dev/md1 &&
mdconfig -a -t vnode -f /tmp/myfs_B.img -u /dev/md2
ephemeral_remove_loop_dev_cmd: >-
mdconfig -d -u /dev/md1 &&
mdconfig -d -u /dev/md2
ephemeral_fstype: ufs
ephemeral_fstab: /etc/fstab
when: ansible_system == 'FreeBSD'
- name: Register facts on NetBSD
ansible.builtin.set_fact:
shell_executable: /usr/local/bin/bash
ephemeral_device_a: /dev/vnd1
ephemeral_device_b: /dev/vnd2
ephemeral_create_loop_dev_cmd: >-
vnconfig /dev/vnd1 /tmp/myfs_A.img &&
vnconfig /dev/vnd2 /tmp/myfs_B.img
ephemeral_remove_loop_dev_cmd: >-
vnconfig -u /dev/vnd1 &&
vnconfig -u /dev/vnd2
ephemeral_fstype: ufs
ephemeral_fstab: /etc/fstab
when: ansible_system == 'NetBSD'
- name: Register format fs command on Non-Linux and Non-OpenBSD
ansible.builtin.set_fact:
ephemeral_format_fs_cmd: >-
newfs {{ ephemeral_device_a }} &&
newfs {{ ephemeral_device_b }}
when: ansible_system in ('SunOS', 'FreeBSD', 'NetBSD')
- name: Register facts on OpenBSD
ansible.builtin.set_fact:
shell_executable: /usr/local/bin/bash
ephemeral_device_a: /dev/vnd1c
ephemeral_device_b: /dev/vnd2c
ephemeral_create_loop_dev_cmd: >-
vnconfig vnd1 /tmp/myfs_A.img &&
vnconfig vnd2 /tmp/myfs_B.img
ephemeral_remove_loop_dev_cmd: >-
vnconfig -u vnd1 &&
vnconfig -u vnd2
ephemeral_format_fs_cmd: >-
newfs /dev/rvnd1c &&
newfs /dev/rvnd2c
ephemeral_fstype: ffs
ephemeral_fstab: /etc/fstab
when: ansible_system == 'OpenBSD'
- name: Create the mount point
ansible.builtin.file:
state: directory
path: '{{ output_dir }}/mount_dest'
mode: '0755'
path: "{{ output_dir }}/mount_dest"
mode: "0755"
- name: Create a directory to bind mount
ansible.builtin.file:
state: directory
path: '{{ output_dir }}/mount_source'
mode: '0755'
path: "{{ output_dir }}/mount_source"
mode: "0755"
- name: Put something in the directory so we see that it worked
ansible.builtin.copy:
content: 'Testing
'
dest: '{{ output_dir }}/mount_source/test_file'
mode: '0644'
content: "Testing\n"
dest: "{{ output_dir }}/mount_source/test_file"
mode: "0644"
register: orig_info
- name: Bind mount a filesystem (Linux)
ansible.posix.mount:
src: '{{ output_dir }}/mount_source'
name: '{{ output_dir }}/mount_dest'
src: "{{ output_dir }}/mount_source"
name: "{{ output_dir }}/mount_dest"
state: mounted
fstype: None
opts: bind
@ -122,8 +36,8 @@
- name: Bind mount a filesystem (FreeBSD)
ansible.posix.mount:
src: '{{ output_dir }}/mount_source'
name: '{{ output_dir }}/mount_dest'
src: "{{ output_dir }}/mount_source"
name: "{{ output_dir }}/mount_dest"
state: mounted
fstype: nullfs
when: ansible_system == 'FreeBSD'
@ -131,7 +45,7 @@
- name: Get checksum for bind mounted file
ansible.builtin.stat:
path: '{{ output_dir }}/mount_dest/test_file'
path: "{{ output_dir }}/mount_dest/test_file"
when: ansible_system in ('FreeBSD', 'Linux')
register: dest_stat
@ -145,8 +59,8 @@
- name: Bind mount a filesystem (Linux)
ansible.posix.mount:
src: '{{ output_dir }}/mount_source'
name: '{{ output_dir }}/mount_dest'
src: "{{ output_dir }}/mount_source"
name: "{{ output_dir }}/mount_dest"
state: mounted
fstype: None
opts: bind
@ -155,8 +69,8 @@
- name: Bind mount a filesystem (FreeBSD)
ansible.posix.mount:
src: '{{ output_dir }}/mount_source'
name: '{{ output_dir }}/mount_dest'
src: "{{ output_dir }}/mount_source"
name: "{{ output_dir }}/mount_dest"
state: mounted
fstype: nullfs
when: ansible_system == 'FreeBSD'
@ -170,8 +84,8 @@
- name: Remount filesystem with different opts (Linux)
ansible.posix.mount:
src: '{{ output_dir }}/mount_source'
name: '{{ output_dir }}/mount_dest'
src: "{{ output_dir }}/mount_source"
name: "{{ output_dir }}/mount_dest"
state: mounted
fstype: None
opts: bind,ro
@ -180,8 +94,8 @@
- name: Remount filesystem with different opts (FreeBSD)
ansible.posix.mount:
src: '{{ output_dir }}/mount_source'
name: '{{ output_dir }}/mount_dest'
src: "{{ output_dir }}/mount_source"
name: "{{ output_dir }}/mount_dest"
state: mounted
fstype: nullfs
opts: ro
@ -191,7 +105,7 @@
- name: Get mount options
ansible.builtin.shell:
cmd: set -o pipefail && mount | grep mount_dest | grep -E -w '(ro|read-only)' | wc -l
executable: "{{ shell_executable }}"
executable: /bin/bash
changed_when: false
register: remount_options
@ -199,20 +113,20 @@
ansible.builtin.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' in remount_options.stdout"
- 1 == remount_options.stdout_lines | length
when: ansible_system in ('FreeBSD', 'Linux')
- name: Unmount the bind mount
ansible.posix.mount:
name: '{{ output_dir }}/mount_dest'
name: "{{ output_dir }}/mount_dest"
state: absent
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'
path: "{{ output_dir }}/mount_dest/test_file"
when: ansible_system in ('FreeBSD', 'Linux')
register: dest_stat
@ -228,7 +142,7 @@
block:
- name: Create fstab record for the first swap file
ansible.posix.mount:
name: none
name: falsene
src: /tmp/swap1
opts: sw
fstype: swap
@ -237,7 +151,7 @@
- name: Try to create fstab record for the first swap file again
ansible.posix.mount:
name: none
name: falsene
src: /tmp/swap1
opts: sw
fstype: swap
@ -252,7 +166,7 @@
- name: Create fstab record for the second swap file
ansible.posix.mount:
name: none
name: falsene
src: /tmp/swap2
opts: sw
fstype: swap
@ -261,8 +175,8 @@
- name: Try to create fstab record for the second swap file again
ansible.posix.mount:
name: none
src: /tmp/swap2
name: falsene
src: /tmp/swap1
opts: sw
fstype: swap
state: present
@ -276,14 +190,14 @@
- name: Remove the fstab record for the first swap file
ansible.posix.mount:
name: none
name: falsene
src: /tmp/swap1
state: absent
register: swap1_removed
- name: Try to remove the fstab record for the first swap file again
ansible.posix.mount:
name: none
name: falsene
src: /tmp/swap1
state: absent
register: swap1_removed_again
@ -296,14 +210,14 @@
- name: Remove the fstab record for the second swap file
ansible.posix.mount:
name: none
name: falsene
src: /tmp/swap2
state: absent
register: swap2_removed
- name: Try to remove the fstab record for the second swap file again
ansible.posix.mount:
name: none
name: falsene
src: /tmp/swap2
state: absent
register: swap2_removed_again
@ -317,9 +231,7 @@
- name: Create fstab record with missing last two fields
ansible.builtin.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
'
content: "//nas/photo /home/jik/pictures cifs defaults,credentials=/etc/security/nas.creds,uid=jik,gid=users,forceuid,forcegid,noserverino,_netdev\n"
mode: "0644"
- name: Try to change the fstab record with the missing last two fields
@ -332,7 +244,7 @@
register: optional_fields_update
- name: Get the content of the fstab file
ansible.builtin.command: cat /etc/fstab
ansible.builtin.command: /bin/cat /etc/fstab
changed_when: false
register: optional_fields_content
@ -340,7 +252,7 @@
ansible.builtin.assert:
that:
- optional_fields_update['changed']
- ''' 0 0'' in optional_fields_content.stdout'
- "' 0 0' in optional_fields_content.stdout"
- 1 == optional_fields_content.stdout_lines | length
- name: Create empty file
@ -362,9 +274,8 @@
- name: Get the last write time
ansible.builtin.shell:
cmd: >-
set -o pipefail && dumpe2fs /tmp/myfs.img 2>/dev/null | grep -i "last write time:" | cut -d: -f2-
executable: "{{ shell_executable }}"
cmd: "set -o pipefail && dumpe2fs /tmp/myfs.img 2>/dev/null | grep -i last write time: |cut -d: -f2-"
executable: /bin/bash
changed_when: false
register: last_write_time
@ -379,9 +290,8 @@
- name: Get again the last write time
ansible.builtin.shell:
cmd: >-
set -o pipefail && dumpe2fs /tmp/myfs.img 2>/dev/null | grep -i "last write time:" |cut -d: -f2-
executable: "{{ shell_executable }}"
cmd: "set -o pipefail && dumpe2fs /tmp/myfs.img 2>/dev/null | grep -i last write time: |cut -d: -f2-"
executable: /bin/bash
changed_when: false
register: last_write_time2
@ -399,7 +309,7 @@
- name: Get remounted options (Linux only)
ansible.builtin.shell:
cmd: set -o pipefail && mount | grep myfs | grep -E -w 'noexec' | wc -l
executable: "{{ shell_executable }}"
executable: /bin/bash
changed_when: false
register: remounted_options
@ -407,7 +317,7 @@
ansible.builtin.assert:
that:
- "'1' in remounted_options.stdout"
- "1 == remounted_options.stdout_lines | length"
- 1 == remounted_options.stdout_lines | length
- name: Mount the FS again to test backup
ansible.posix.mount:
@ -433,7 +343,7 @@
- name: Remove the test FS
ansible.builtin.file:
path: '{{ item }}'
path: "{{ item }}"
state: absent
loop:
- /tmp/myfs.img
@ -474,7 +384,7 @@
- name: Remove the test FS
ansible.builtin.file:
path: '{{ item }}'
path: "{{ item }}"
state: absent
loop:
- /tmp/myfs.img
@ -493,7 +403,7 @@
dev: /tmp/myfs1.img
- name: Create custom fstab file without newline
ansible.builtin.copy:
content: '#TEST COMMENT WITHOUT NEWLINE'
content: "#TEST COMMENT WITHOUT NEWLINE"
dest: /tmp/test_fstab
mode: "0644"
- name: Mount the FS using the custom fstab
@ -511,7 +421,7 @@
fstab: /tmp/test_fstab
- name: Remove the test FS and the custom fstab
ansible.builtin.file:
path: '{{ item }}'
path: "{{ item }}"
state: absent
loop:
- /tmp/myfs1.img
@ -521,7 +431,6 @@
- name: Block to test ephemeral option
environment:
PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
when: ansible_system in ('Linux', 'SunOS', 'FreeBSD', 'NetBSD', 'OpenBSD')
block:
- name: Create empty file A
community.general.filesize:
@ -533,7 +442,81 @@
path: /tmp/myfs_B.img
size: 20M
##### FORMAT FS ON LINUX
- name: Register facts on Linux
ansible.builtin.set_fact:
ephemeral_device_a: /tmp/myfs_A.img
ephemeral_device_b: /tmp/myfs_B.img
ephemeral_fstype: ext3
ephemeral_fstab: /etc/fstab
when: ansible_system == 'Linux'
- name: Register facts on Solaris/SunOS
ansible.builtin.set_fact:
ephemeral_device_a: /dev/lofi/1
ephemeral_device_b: /dev/lofi/2
ephemeral_create_loop_dev_cmd: >
lofiadm -a /tmp/myfs_A.img /dev/lofi/1 &&
lofiadm -a /tmp/myfs_B.img /dev/lofi/2
ephemeral_remove_loop_dev_cmd: >
lofiadm -d /dev/lofi/1 &&
lofiadm -d /dev/lofi/2 || true
ephemeral_fstype: ufs
ephemeral_fstab: /etc/vfstab
when: ansible_system == 'SunOS'
- name: Register facts on FreeBSD
ansible.builtin.set_fact:
ephemeral_device_a: /dev/md1
ephemeral_device_b: /dev/md2
ephemeral_create_loop_dev_cmd: >
mdconfig -a -t vnode -f /tmp/myfs_A.img -u /dev/md1 &&
mdconfig -a -t vnode -f /tmp/myfs_B.img -u /dev/md2
ephemeral_remove_loop_dev_cmd: >
mdconfig -d -u /dev/md1 &&
mdconfig -d -u /dev/md2
ephemeral_fstype: ufs
ephemeral_fstab: /etc/fstab
when: ansible_system == 'FreeBSD'
- name: Register facts on NetBSD
ansible.builtin.set_fact:
ephemeral_device_a: /dev/vnd1
ephemeral_device_b: /dev/vnd2
ephemeral_create_loop_dev_cmd: >
vnconfig /dev/vnd1 /tmp/myfs_A.img &&
vnconfig /dev/vnd2 /tmp/myfs_B.img
ephemeral_remove_loop_dev_cmd: >
vnconfig -u /dev/vnd1 &&
vnconfig -u /dev/vnd2
ephemeral_fstype: ufs
ephemeral_fstab: /etc/fstab
when: ansible_system == 'NetBSD'
- name: Register format fs command on Non-Linux and Non-OpenBSD
ansible.builtin.set_fact:
ephemeral_format_fs_cmd: >
yes | newfs {{ ephemeral_device_a }} &&
yes | newfs {{ ephemeral_device_b }}
when: ansible_system in ('SunOS', 'FreeBSD', 'NetBSD')
- name: Register facts on OpenBSD
ansible.builtin.set_fact:
ephemeral_device_a: /dev/vnd1c
ephemeral_device_b: /dev/vnd2c
ephemeral_create_loop_dev_cmd: >
vnconfig vnd1 /tmp/myfs_A.img &&
vnconfig vnd2 /tmp/myfs_B.img
ephemeral_remove_loop_dev_cmd: >
vnconfig -u vnd1 &&
vnconfig -u vnd2
ephemeral_format_fs_cmd: >
yes | newfs /dev/rvnd1c &&
yes | newfs /dev/rvnd2c
ephemeral_fstype: ffs
ephemeral_fstab: /etc/fstab
when: ansible_system == 'OpenBSD'
##### FORMAT FS ON LINUX
- name: Block to format FS on Linux
when: ansible_system == 'Linux'
@ -548,61 +531,56 @@
fstype: ext3
dev: /tmp/myfs_B.img
##### FORMAT FS ON SOLARIS AND BSD
##### FORMAT FS ON SOLARIS AND BSD
- name: Create loop devices on Solaris and BSD
ansible.builtin.shell:
cmd: "set -o pipefail && {{ ephemeral_create_loop_dev_cmd }}"
executable: "{{ shell_executable }}"
ansible.builtin.command: "{{ ephemeral_create_loop_dev_cmd }}"
changed_when: true
when: ephemeral_create_loop_dev_cmd is defined
- name: Format FS A and B on Solaris and BSD
ansible.builtin.shell:
cmd: "set -o pipefail && {{ ephemeral_format_fs_cmd }}"
executable: "{{ shell_executable }}"
ansible.builtin.command: "{{ ephemeral_format_fs_cmd }}"
changed_when: true
when: ephemeral_format_fs_cmd is defined
##### TESTS
##### TESTS
- name: Create fstab if it does not exist
ansible.builtin.file:
path: "{{ ephemeral_fstab }}"
state: touch
mode: '0644'
mode: "0644"
- name: Get checksum of /etc/fstab before mounting anything
ansible.builtin.stat:
path: '{{ ephemeral_fstab }}'
path: "{{ ephemeral_fstab }}"
register: fstab_stat_before_mount
- name: Mount the FS A with ephemeral state
ansible.posix.mount:
path: /tmp/myfs
src: '{{ ephemeral_device_a }}'
fstype: '{{ ephemeral_fstype }}'
src: "{{ ephemeral_device_a }}"
fstype: "{{ ephemeral_fstype }}"
opts: rw
state: ephemeral
register: ephemeral_mount_info
- name: Put something in the directory so we can do additional checks later on
ansible.builtin.copy:
content: 'Testing'
content: Testing
dest: /tmp/myfs/test_file
mode: '0644'
mode: "0644"
- name: Get checksum of /etc/fstab after an ephemeral mount
ansible.builtin.stat:
path: '{{ ephemeral_fstab }}'
path: "{{ ephemeral_fstab }}"
register: fstab_stat_after_mount
- name: Get mountinfo
ansible.builtin.shell:
cmd: grep -c '/tmp/myfs' <(mount -v)
executable: "{{ shell_executable }}"
cmd: set -o pipefail && mount -v | awk '{print $3}' | grep '^/tmp/myfs$' | wc -l
executable: /bin/bash
register: check_mountinfo
failed_when: false
changed_when: false
- name: Assert the mount occured and the fstab is unchanged
@ -614,32 +592,31 @@
- name: Get first mount record
ansible.builtin.shell:
cmd: grep '/tmp/myfs' <(mount -v)
executable: "{{ shell_executable }}"
cmd: set -o pipefail && mount -v | grep '/tmp/myfs'
executable: /bin/bash
register: ephemeral_mount_record_1
changed_when: false
- name: Try to mount FS A where FS A is already mounted (should trigger remount and changed)
ansible.posix.mount:
path: /tmp/myfs
src: '{{ ephemeral_device_a }}'
fstype: '{{ ephemeral_fstype }}'
src: "{{ ephemeral_device_a }}"
fstype: "{{ ephemeral_fstype }}"
opts: ro
state: ephemeral
register: ephemeral_mount_info
- name: Get second mount record (should be different than the first)
ansible.builtin.shell:
cmd: grep '/tmp/myfs' <(mount -v)
executable: "{{ shell_executable }}"
cmd: set -o pipefail && mount -v | grep '/tmp/myfs'
executable: /bin/bash
register: ephemeral_mount_record_2
changed_when: false
- name: Get mountinfo
ansible.builtin.shell:
cmd: grep -c '/tmp/myfs' <(mount -v)
executable: "{{ shell_executable }}"
failed_when: false
cmd: set -o pipefail && mount -v | awk '{print $3}' | grep '^/tmp/myfs$' | wc -l
executable: /bin/bash
register: check_mountinfo
changed_when: false
@ -654,24 +631,23 @@
- name: Try to mount file B on file A mountpoint (should fail)
ansible.posix.mount:
path: /tmp/myfs
src: '{{ ephemeral_device_b }}'
fstype: '{{ ephemeral_fstype }}'
src: "{{ ephemeral_device_b }}"
fstype: "{{ ephemeral_fstype }}"
state: ephemeral
register: ephemeral_mount_b_info
ignore_errors: true
- name: Get third mount record (should be the same than the second)
ansible.builtin.shell:
cmd: grep '/tmp/myfs' <(mount -v)
executable: "{{ shell_executable }}"
cmd: set -o pipefail && mount -v | grep '/tmp/myfs'
executable: /bin/bash
register: ephemeral_mount_record_3
changed_when: false
- name: Get mountinfo
ansible.builtin.shell:
cmd: grep -c '/tmp/myfs' <(mount -v)
executable: "{{ shell_executable }}"
failed_when: false
cmd: set -o pipefail && mount -v | awk '{print $3}' | grep '^/tmp/myfs$' | wc -l
executable: /bin/bash
register: check_mountinfo
changed_when: false
@ -695,15 +671,14 @@
- name: Get fstab checksum after unmounting an ephemeral mount with state = unmounted
ansible.builtin.stat:
path: '{{ ephemeral_fstab }}'
path: "{{ ephemeral_fstab }}"
register: fstab_stat_after_unmount
- name: Get mountinfo
ansible.builtin.shell:
cmd: grep -c '/tmp/myfs' <(mount -v)
executable: "{{ shell_executable }}"
cmd: set -o pipefail && mount -v | awk '{print $3}' | grep '^/tmp/myfs$' | wc -l
executable: /bin/bash
register: check_mountinfo
failed_when: false
changed_when: false
- name: Try to stat our test file
@ -725,15 +700,13 @@
state: unmounted
- name: Remove loop devices on Solaris and BSD
ansible.builtin.shell:
cmd: "set -o pipefail && {{ ephemeral_remove_loop_dev_cmd }}"
executable: "{{ shell_executable }}"
changed_when: true
when: ephemeral_remove_loop_dev_cmd is defined
ansible.builtin.command: "{{ ephemeral_remove_loop_dev_cmd }}"
changed_when: true
- name: Remove the test FS
ansible.builtin.file:
path: '{{ item }}'
path: "{{ item }}"
state: absent
loop:
- /tmp/myfs_A.img