Ansible - Getting Started parsing JSON

Let's say you've the following json.

{
  "hello": "world"
}

 

There are a number of different ways that Ansible can be used to parse JSON. For example, you may use the lookup plugin and the vars or set_fact module to store the contents of a JSON file in a variable. The lookup plugin is commonly used when you want to read a file on the control node (that's the Ansible server). In this example, the contents of foo.json are store in the "foo" variable.

- set_fact:
    foo: "{{ lookup('file', 'foo.json') }}"

 

The debug module can be used to output the raw json. Notice the double curley braces {{ ... }}. Jinja2 uses double curley braces for variables.

- name: output the contents of the 'foo' variable
  debug: 
    var: foo

 

Which should return the following.

TASK [output the contents of the 'foo' variable]
ok: [localhost] => {
    "foo": {
      "hello": "world"
  }
}

 


slurp module

Or, you may use the slurp module and the register parameter to store the contents of a JSON file in a variable. The slurp module is commonly used when you want to read a file on a managed node (e.g. target system). In this example, the contents of foo.json are store in the "foo" variable.

- name: store the contents of foo.json in the 'foo' variable
  slurp:
    src: /tmp/foo.json
  register: foo

 

The debug module can be used to output the raw json.

- name: output the contents of the 'foo' variable
  debug: 
    var: foo

 

Which should return JSON something like this. Notice in this example that "content" is "bmV3IGxpbmUK". This is because the content of the file is base 64 encoded. Refer to 

TASK [output the contents of the 'foo' variable]
ok: [server1.example.com] => {
    "msg": {
        "changed": false,
        "content": "bmV3IGxpbmUK",
        "encoding": "base64",
        "failed": false",
        "source": "/tmp/foo.json"
    }
}

 

If you want to display the content of the file, you would do something like this.

- name: output the value of the 'content' key in the 'foo' variable, decoding base 64
  debug:
    msg: "{{ foo['content'] | b64decode }}"

 

Which should now return the following.

TASK [output the value of the 'content' key in the 'foo' variable, decoding base 64]
ok: [server1.example.com] => {
    "msg": {
      "hello": "world"
    }
}

 


find module

Or, you may be using the find module and the register parameter to list every file and directory in the /tmp directory on a managed node (e.g. target server). In this example, everything below the /tmp directory, and the register parameter is used to store the results in the "out" variable.

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

 

The debug module can be used to display the contents of the "out" variable.

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

 

Which will produce JSON something like this.

ok: [server1.example.com] => {
    "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": ""
    }
}

 

Let's say you want the "changed" output. You would include changed, like this.

- name: determine if 'changed' is true of false
  debug:
    msg: "{{ out.changed }}"

 

Which should return something like this.

TASK [determine if 'changed' is true of false] 
ok: [server1.example.com] => {
    "msg": false
}

 

Let's say you want the "path" output. You would include files and path, like this.

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

 

Which should return something like this.

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

 


Key Values

Let's say you want to store the value (bar) in a variable. Refer to storing JSON values in a variable.

{
  "foo": "bar"
}

 


Loop through array

Let's say you have the following json. In this example, the [ ] characters mean the data is in an array. You can loop through a JSON array to do something with each key:value pair.

[
  {
    "foo": "Hello",
    "bar": "World"
  }
]

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |