Created
April 13, 2022 10:14
-
-
Save cidrblock/532a0d875a457ee5144d0d53d1cc2504 to your computer and use it in GitHub Desktop.
Parse output using native parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x1 ➜ collection_development ansible-navigator run parse.yml -m stdout --ee false | |
PLAY [localhost] *************************************************************** | |
TASK [Define some data] ******************************************************** | |
ok: [localhost] | |
TASK [Pass text and template_path] ********************************************* | |
ok: [localhost] | |
TASK [Print out the parsed values] ********************************************* | |
ok: [localhost] => (item=/dev/mapper/cl-root) => { | |
"msg": "The file system at /dev/mapper/cl-root mounted at / has 30328352 blocks total, 9429572 blocks used, 20898780 blocks available, and is 32% used," | |
} | |
ok: [localhost] => (item=/foo/bar) => { | |
"msg": "The file system at /foo/bar mounted at /foo/bar has 1 blocks total, 2 blocks used, 3 blocks available, and is 4% used," | |
} | |
PLAY RECAP ********************************************************************* | |
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- example: "/dev/mapper/cl-root 30328352 9429572 20898780 32% /" | |
getval: (?P<file_system>\S+)\s+(?P<blocks>\d+)\s+(?P<used>\d+)\s+(?P<available>\d+)\s+(?P<percent_used>\S+)\s+(?P<mount>\S+) | |
result: | |
file_systems: | |
- file_system: "{{ file_system }}" | |
blocks: "{{ blocks }}" | |
used: "{{ used }}" | |
available: "{{ available }}" | |
percent_used: "{{ percent_used }}" | |
mount: "{{ mount }}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- hosts: localhost | |
gather_facts: false | |
tasks: | |
- name: Define some data | |
ansible.builtin.set_fact: | |
stdout: | | |
Filesystem 1K-blocks Used Available Use% Mounted on | |
/dev/mapper/cl-root 30328352 9429572 20898780 32% / | |
/foo/bar 1 2 3 4% /foo/bar | |
- name: Pass text and template_path | |
ansible.utils.cli_parse: | |
text: "{{ stdout }}" | |
parser: | |
name: ansible.netcommon.native | |
template_path: parser_definition.yml | |
set_fact: parsed | |
- name: Print out the parsed values | |
ansible.builtin.debug: | |
msg: >- | |
The file system at {{ file_system.file_system }} | |
mounted at {{ file_system.mount }} has | |
{{ file_system.blocks }} blocks total, | |
{{ file_system.used }} blocks used, | |
{{ file_system.available }} blocks available, | |
and is {{ file_system.percent_used }} used, | |
loop: "{{ parsed.file_systems }}" | |
loop_control: | |
loop_var: file_system | |
label: "{{ file_system.file_system }}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment