23

I'm new to ansible and I'm having a problem reading a value from json file in ansible role. my variable has a value like the following:

{
  "queue": {
    "first": {
      "car": "bmw",
      "year": "1990",
      "model": "x3",
      "color": "blue"
    },
    "second": {
      "car": "bmw",
      "year": "2000",
      "model": "318",
      "color": "red"
    }
  }
}

I'm trying to print the color's value only to compare it with some other variable. I used with_dict to iterate over the json object (stored in variable called jsonVar) like the following:

- name: test loop
  with_dict: "{{jsonVar}}"
  shell:  |
        if echo "blue" | grep -q "${{item.value.color}}" ; then
           echo "success"

so far there is no luck in getting the comparison of color's value from json to "blue" from the if statement. I was wondering if I'm doing something wrong? thanks in advance!

tkyass
  • 2,968
  • 8
  • 38
  • 57

3 Answers3

24

You can read a json file using a lookup plugin called file and pass it to the from_json jinja2 filter. You also had mistake in the with_dict loop, since you have to loop over the jsonVar['queue'], not just jsonVar. Here is a complete code which works:

---
- hosts: your_host
  vars:
    jsonVar: "{{ lookup('file', 'var.json') | from_json }}"
  tasks:
    - name: test loop
      with_dict: "{{ jsonVar['queue'] }}"
      shell: |
        if echo "blue" | grep -q "{{ item.value.color }}" ; then
            echo "success"
        fi
Strahinja Kustudic
  • 4,175
  • 1
  • 24
  • 18
  • thanks @Strahinja Kustudic for your reply! I was wondering if I can achieve my goal without using file plugin? or from_json – tkyass Apr 22 '16 at 19:26
  • If you want to convert the json into a dictionary, you have to use `from_json`. You don't have to use the `file` lookup plugin though, you can use a the `command` task, e.g. `command: cat var.json` with a combination of `register: var` and then use `set_fact: jsonVar: {{ var.stdout | from_json }}`. – Strahinja Kustudic Apr 26 '16 at 08:06
  • @StrahinjaKustudic Is `from_json` documented anywhere? I stumbled upon this answer and realized that `lookup` might also support `from_yaml` and it worked!!! Thanks... – Ajoy Mar 26 '17 at 10:37
  • 1
    Both `from_json` and `from_yaml` are in the official documentation http://docs.ansible.com/ansible/playbooks_filters.html#filters-for-formatting-data – Strahinja Kustudic Mar 27 '17 at 13:10
14

You can use the | json_query filter.

http://docs.ansible.com/ansible/playbooks_filters.html#json-query-filter

But make sure the file you are inputting is also in appropriate format,if not then you can use two filter, first one to convert into appropriate filter and second one to perform json query.

ex:- {{ variable_name | from_json | json_query('')}}

In you case, I think this would help:

tasks: print the color
set_fact:
  color1 : "{{ jsonVar | from_json | json_query('queue.[0].['color']')}}"
  color2 : "{{ jsonVar | from_json | json_query('queue.[1].['color']')}}"

But make note of the requirements like Ansible version

VaibhavKrishna
  • 141
  • 1
  • 2
4

The 2020 answer since I just had to figure it out. Assumes the file name is vars.json and lives in the same directory as your yml.

---
- hosts: all
  gather_facts: no
  vars:
    position: second
    field: year
  tasks:
  - name: Include Site Vars
    include_vars: vars.json

  - name: Debug JSON Vars
    debug:
      msg: "{{ queue[position][field] }}"
The Chad
  • 51
  • 1