parsing output from json

30 views
Skip to first unread message

akku

unread,
Jul 25, 2017, 5:03:25 PM7/25/17
to Ansible Project

ansible --version
ansible 2.2.1.0
config file = /etc/ansible/ansible.cfg

Am using find modules to get largest files and its size the file, I am getting a big jason format output, How can i parse one particular string from the output.

I need to fetch only the "path" and "size" values from here. Can anyone help me on this?


  • hosts: localhost
    connection: local
    become: true
    vars:
    var1: '{{ choice }}'

    var2: '{{ fspath }}'

    gather_facts: true
    tasks:
    • find: paths="/var/log" age="0d" size="1m" patterns=".old,.log" recurse=yes
      register: logfile
      when: var1 == "getLogs"

    • name: "display the big files

    • debug: var=logfile
      =================================================================
      output:

      "logfile": {
      "changed": false,
      "examined": 62,
      "files": [
      {
      "atime": 1492683835.282168,
      "ctime": 1501012553.91998,
      "dev": 51713,
      "gid": 0,
      "inode": 25975339,
      "isblk": false,
      "ischr": false,
      "isdir": false,
      "isfifo": false,
      "isgid": false,
      "islnk": false,
      "isreg": true,
      "issock": false,
      "isuid": false,
      "mode": "0644",
      "mtime": 1501012553.91998,
      "nlink": 1,
      "path": "/var/log/ansible.log",
      "rgrp": true,
      "roth": true,
      "rusr": true,
      "size": 2070175,
      "uid": 0,
      "wgrp": false,
      "woth": false,
      "wusr": true,
      "xgrp": false,
      "xoth": false,
      "xusr": false

Kai Stian Olstad

unread,
Jul 26, 2017, 1:50:14 AM7/26/17
to ansible...@googlegroups.com
On 25. juli 2017 23:03, akku wrote:
> Am using find modules to get largest files and its size the file, I am
> getting a big jason format output, How can i parse one particular string
> from the output.
> I need to fetch only the "path" and "size" values from here. Can anyone
> help me on this?
> ------------------------------
>
> - hosts: localhost
> connection: local
> become: true
> vars:
> var1: '{{ choice }}'var2: '{{ fspath }}'gather_facts: true
> tasks:
> -
>
> find: paths="/var/log" age="0d" size="1m" patterns="*.old,*.log"
> recurse=yes
> register: logfile
> when: var1 == "getLogs"

How, depends on what you are going to do with path and size, so this is
one of several ways.

- debug: msg="{{ item.path }} - {{ item.size }}"
with_items: "{{ logfile.files }}"


--
Kai Stian Olstad

akku

unread,
Jul 26, 2017, 8:03:25 AM7/26/17
to Ansible Project, ansible-pr...@olstad.com

yes... with this it prints the path and size along with the whole output.  but  i need only the parsed output.

Kai Stian Olstad

unread,
Jul 26, 2017, 8:45:01 AM7/26/17
to ansible...@googlegroups.com
On 26.07.2017 14:03, akku wrote:
> yes... with this it prints the path and size along with the whole
> output.
> but i need only the parsed output.

You don't say what you are going to use this for.
If you would like it in a file, use the template module.

If you would like to have it displayed nice on the screen you are
probably using the wrong tool.
Ansible is not a reporting tool but a configuration management tool.
But you could write your one callback plugin to make the output the way
you want it to be.

--
Kai Stian Olstad

akku

unread,
Jul 26, 2017, 4:55:19 PM7/26/17
to Ansible Project, ansible-pr...@olstad.com
The idea is to delete the file..
I need the formatted output, because my associated application is expecting such an input from ansible..
thanks for the info..
Reply all
Reply to author
Forward
0 new messages