Q: Get list of filesystems and mountpoints using Ansible and df
without Python installed
As already mentioned within the other answer, there are many options possible.
the output has several lines, one for each file system, and I'm not sure how to handle it on Ansible directly
The data processing can be done on Remote Nodes (data pre-processing, data cleansing), Control Node (data post-processing) or partially on each of them.
Whereby I prefer the answer of @Vladimir Botka and recommend to use it since the data processing is done on the Control Node and in Python
How can this be achieved without the usage of that Python script? (annot.: or any Python installed on the Remote Node)
a lazy approach could be
---
- hosts: test
become: false
gather_facts: false # is necessary because setup.py depends on Python too
tasks:
- name: Gather raw 'df' output with pre-processing
raw: "df --type=ext4 -h --output=source,target | tail -n +2 | sed 's/ */,/g'"
register: result
- name: Show result as CSV
debug:
msg: "{{ inventory_hostname }},{{ item }}"
loop_control:
extended: true
label: "{{ ansible_loop.index0 }}"
loop: "{{ result.stdout_lines }}"
resulting into an output of
TASK [Show result as CSV] *************
ok: [test.example.com] => (item=0) =>
msg: test.example.com,/dev/sda3,/
ok: [test.example.com] => (item=1) =>
msg: test.example.com,/dev/sda4,/tmp
ok: [test.example.com] => (item=2) =>
msg: test.example.com,/dev/sdc1,/var
ok: [test.example.com] => (item=3) =>
msg: test.example.com,/dev/sda2,/boot
As noted before, the data pre-processing parts
- Remove first line from output via
| tail -n +2
- Remove multiple whitepspaces and replace via
| sed 's/ */,/g'
could be processed in Ansible and Python on the Control Node, in example
- Remove first line in
stdout_lines
(annot.: and as a general approach for --no-headers
even for commands which do not provide such)
- Simply split
item
on space and re-join all elements of the list with comma (,
) via {{ item | split | join(',') }}
- name: Gather raw 'df' output without pre-processing
raw: "df --type=ext4 -h --output=source,target"
register: result
- name: Show result as CSV
debug:
msg: "{{ inventory_hostname }},{{ item | split | join(',')}}"
loop_control:
extended: true
label: "{{ ansible_loop.index0 }}"
loop: "{{ result.stdout_lines[1:] }}" # with --no-headers
resulting into the same output as before.
Documentation Links
for Ansible
and Linux commands
Further Reading