Bootstrap FreeKB - Ansible - Getting Started parsing JSON
Ansible - Getting Started parsing JSON

Updated:   |  Ansible articles

Almost always, you will be working with JSON when obtaining data using a module and then using the register parameter to store the JSON in a variable. In this example, the find module is used to store the JSON in the "out" variable and debug is used to display the JSON.

---
- hosts: localhost
  tasks:
  - name: store the contents of the /tmp directory in the 'out' variable
    find:
      paths: /tmp
    register: out

  - name: display the contents of the 'out' variable
    debug:
      var: out
...

 

Which will produce JSON like this.

ok: [localhost] => {
    "out": {
        "changed": false,
        "examined": 1,
        "failed": false,
        "files": [
            {
              "atime": 1583384414.8106842,
              "mtime": 1583388631.5600421,
              "dev": 64768,
              "gid": 0,
              "inode": 131881,
              "isblk": false,
              "ischr": false,
              "isdir": false,
              "isfifo": false,
              "isgid": false,
              "islnk": false,
              "isreg": true,
              "issock": false,
              "isuid": false,
              "mode": "0664",
              "mtime": 158338361.5600421,
              "nlink": 1,
              "path": "/tmp/foo.txt",
              "rgrp": true,
              "roth": true,
              "rusr": true,
              "size": 8,
              "uid": 0,
              "wgrp": false,
              "woth": false,
              "wusr": true,
              "xgrp": false,
              "xoth": false,
              "xusr": false
           }
        ],
        "matched": 1,
        "msg": ""
    }
}

 

Here is how you can print the value of the "changed" key.

---
- hosts: localhost
  tasks:
  - name: store the contents of the /tmp directory in the 'out' variable
    find:
      paths: /tmp
    register: out

  - name: print the value in the "changed" key
    debug:
      msg: "{{ out.changed }}"
...

 

Which should return something like this.

TASK [determine if 'changed' is true of false] 
ok: [localhost] => {
    "msg": false
}

 

Notce that the "files" key is a list, as incidated by the [ ] (bracket) characters. Here is one way that you could return the value of a key inside the "files" list.

---
- hosts: localhost
  tasks:
  - name: store the contents of the /tmp directory in the 'out' variable
    find:
      paths: /tmp
    register: out

  - name: return the value of the path key
    debug:
      msg: "{{ out.files[0].path}}"
...

 

Or, you could loop the "files" list.

---
- hosts: localhost
  tasks:
  - name: store the contents of the /tmp directory in the 'out' variable
    find:
      paths: /tmp
    register: out

  - name: return the value of the path key
    debug:
      msg: "{{ item.path}}"
    with_items: "{{ out.files }}"
...

 

Which should return something like this.

TASK [return the value of the path key] 
ok: [localhost] => {
    "msg": "/tmp/foo.txt"
}

 

Or, json_query could be used.

---
- hosts: localhost
  tasks:
  - name: store the contents of the /tmp directory in the 'out' variable
    find:
      paths: /tmp
    register: out

  - name: return the value of the path key
    debug:
      var: item
    with_items: "{{ out | json_query('files[*].path') }}"
...

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 99efbb in the box below so that we can be sure you are a human.