Automate Storage and Mount Configuration

AnsibleBeginner
Practice Now

Introduction

Mount automation should be repeatable and safe. For a lab VM, tmpfs provides a harmless filesystem that still exercises mount point and fstab management.

In this lab, you will create a mount point, mount tmpfs with ansible.posix.mount, verify findmnt and /etc/fstab, and prove the playbook is idempotent.

Create a Mount Automation Project

In this step, you will create a project for automating a safe tmpfs mount.

cd /home/labex/project
mkdir -p rhce-ansible/{inventory,playbooks,reports}
cd rhce-ansible
cat > ansible.cfg <<'EOF'
[defaults]
inventory = ./inventory/hosts
host_key_checking = False
retry_files_enabled = False
EOF
cat > inventory/hosts <<'EOF'
[managed]
host ansible_connection=local
EOF

The mount target is a real system mount point, but it uses tmpfs so no disk partitioning is needed. This keeps the lab safe while still exercising mount and /etc/fstab behavior.

Define Mount Variables

In this step, you will define the mount point, filesystem type, and options. The size=16m option keeps the tmpfs small. Before running commands in this step, return to the Ansible project directory. This keeps the commands stable even when LabEx runs each step separately.

cd /home/labex/project/rhce-ansible
mkdir -p inventory/group_vars
cat > inventory/group_vars/managed.yml <<'EOF'
mount_point: /mnt/rhce-ansible-data
mount_src: tmpfs
mount_fstype: tmpfs
mount_opts: size=16m,mode=0755
EOF

The variables describe both the live mount and the persistent entry. tmpfs is the source and filesystem type, while size=16m,mode=0755 controls the in-memory filesystem size and mount-point permissions.

cat inventory/group_vars/managed.yml

Write the Mount Playbook

In this step, you will use ansible.posix.mount. The state: mounted setting both mounts the filesystem now and ensures an /etc/fstab entry exists. Before running commands in this step, return to the Ansible project directory. This keeps the commands stable even when LabEx runs each step separately.

cd /home/labex/project/rhce-ansible
cat > playbooks/manage-mount.yml <<'EOF'
---
- name: Configure a safe tmpfs mount
  hosts: managed
  become: true
  gather_facts: false
  tasks:
    - name: Ensure mount point exists
      ansible.builtin.file:
        path: "{{ mount_point }}"
        state: directory
        mode: '0755'

    - name: Mount and persist tmpfs
      ansible.posix.mount:
        path: "{{ mount_point }}"
        src: "{{ mount_src }}"
        fstype: "{{ mount_fstype }}"
        opts: "{{ mount_opts }}"
        state: mounted

    - name: Write evidence inside mounted filesystem
      ansible.builtin.copy:
        dest: "{{ mount_point }}/mounted-by-ansible.txt"
        content: |
          mounted=true
        mode: '0644'
EOF

state: mounted is doing two things: it mounts the filesystem now and ensures an /etc/fstab entry exists for future boots. The final copy task writes a small marker inside the mounted filesystem so you can prove the mount is usable.

ansible-playbook playbooks/manage-mount.yml --syntax-check

Run and Inspect the Mount

In this step, you will run the playbook and inspect the live mount and persistent fstab entry.

cd /home/labex/project/rhce-ansible
ansible-playbook playbooks/manage-mount.yml | tee reports/mount-first.txt

The first run should create the mount point, mount tmpfs, update /etc/fstab, and write the marker file. You will check each of those results directly.

findmnt /mnt/rhce-ansible-data

findmnt confirms the live kernel mount table contains the mount point.

grep -F /mnt/rhce-ansible-data /etc/fstab

The /etc/fstab line confirms the persistent configuration exists.

cat /mnt/rhce-ansible-data/mounted-by-ansible.txt

Reading the marker file proves the mounted filesystem is writable and currently attached at the expected path.

Confirm Mount Automation Is Idempotent

In this step, you will rerun the playbook. The mount point, fstab entry, and evidence file should already match the desired state.

cd /home/labex/project/rhce-ansible
ansible-playbook playbooks/manage-mount.yml | tee reports/mount-second.txt

The second run checks whether Ansible can recognize the existing mount, fstab entry, and marker file without changing them.

grep -E 'host.*changed=0' reports/mount-second.txt

Summary

You configured a safe tmpfs mount with ansible.posix.mount, verified live and persistent mount state, and confirmed the playbook converges.