If you are not familiar with modules, check out Ansible - Getting Started with Modules.
The filesystem module can be used to configure a partition to use a certain file system, such as XFS, EXT4, or LVM.
AVOID TROUBLE
The modification of a file system can only be done by root or by users or groups that have been granted sudo access, thus you'll want to either run this playbook as root, use the remote_user: root directive or use become.
The following playbook would set the /dev/sdb1 partition to have the LVM filesystem.
---
- hosts: all
remote_user: root
tasks:
- name: set /dev/sdb1 file system to LVM
filesystem:
dev: /dev/sdb1
fstype: lvm
...
opts can be used to include optional options, such as the -c (faster) or -cc (slow) flags to check for bad blocks before creating the filesystem.
---
- hosts: all
tasks:
- name: set /dev/sdb1 file system to LVM
filesystem:
dev: /dev/sdb1
fstype: lvm
opts: -cc
...
Ansible does not have a module that can be used to verify the file system type of a block device. For this reason, the shell module can be used to run the file command with the --special-files and --dereference flags to return the file system type.
---
- hosts: all
tasks:
- name: determine the file system type for /dev/sdb1
shell: file --special-files --deference /dev/sdb1
register: out
- name: return file system type
debug:
var: out
...
Which should return something like this.
TASK [return file system type]
ok: [server1.example.com] => {
"msg": {
"changed": true,
"cmd": "file --special-files --dereference /dev/sdb1",
"delta": "0:00:00.009100",
"end": "2021-07-13 03:11:04.371306",
"failed": false,
"rc": 0,
"start": "2021-07-13 03:11:04.362206",
"stderr": "",
"stderr_lines": [],
"stdout": "/dev/sdb1: SGI XFS filesystem data (blksz 4096, inosz 512, v2 dirs)",
"stdout_lines": [
"/dev/sdb1: SGI XFS filesystem data (blksz 4096, inosz 512, v2 dirs)"
]
}
}
Better yet, the split filter can be used to return only the filesystem type.
---
- hosts: all
tasks:
- name: determine the file system type for /dev/sdb1
shell: file --special-files --deference /dev/sdb1
register: out
- name: return file system type
debug:
msg: "{{ out.stdout.split(' ')[2] }}"
...
Which should now return just the following.
TASK [return file system type]
ok: [server1.example.com] => {
"msg": "XFS"
}
Or the blkid command.
---
- hosts: all
tasks:
- name: determine the file system type for /dev/sdb1
shell: blkid /dev/sdb1
register: out
- name: return file system type
debug:
var: out
...
Which should return something like this.
TASK [return file system type]
ok: [server1.example.com] => {
"out": {
"changed": true,
"cmd": "blkid /dev/sdb1",
"delta": "0:00:00.004848",
"end": "2021-07-13 03:00:30.533478",
"failed": false,
"rc": 0,
"start": "2021-07-13 03:00:30.528630",
"stderr": "",
"stderr_lines": [],
"stdout": "/dev/sdb1: UUID=\"f32f2c5c-e600-48bb-8847-357cf5ec562c\" BLOCK_SIZE=\"512\" TYPE=\"xfs\"",
"stdout_lines": [
"/dev/sdb1: UUID=\"f32f2c5c-e600-48bb-8847-357cf5ec562c\" BLOCK_SIZE=\"512\" TYPE=\"xfs\""
]
}
}
The split and regex_replace filters can be used to return only the file system type.
---
- hosts: all
tasks:
- name: determine the file system type for /dev/sdb1
shell: blkid /dev/sdb1
register: out
- name: return out
debug:
msg: "{{ out.stdout.split(' ')[3] | regex_replace('TYPE=\"', '') | regex_replace('\"', '') }}"
...
Which should now return just the following.
TASK [return file system type]
ok: [server1.example.com] => {
"msg": "xfs"
}
Did you find this article helpful?
If so, consider buying me a coffee over at