Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reading json like variable in ansible

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!

like image 897
tkyass Avatar asked Apr 19 '16 20:04

tkyass


People also ask

Can Ansible read JSON?

JSON, which is widely used in Ansible, is one of them.

Can Ansible use JSON format?

By default, an Ansible inventory file uses the INI configuration format. You can also use JSON (JavaScript Object Notation) configuration format for Ansible inventory files as well.

What is JSON Ansible?

Ansible json_query is an on-demand feature that every ansible user wants to explore. In this post we are going to how Ansible JSON processing works. During the infrastructure automation, we might end up in a situation where we need to process Huge datasets, especially in JSON format. We might…


3 Answers

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
like image 174
Strahinja Kustudic Avatar answered Oct 04 '22 07:10

Strahinja Kustudic


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

like image 29
VaibhavKrishna Avatar answered Oct 04 '22 08:10

VaibhavKrishna


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] }}"
like image 29
The Chad Avatar answered Oct 04 '22 08:10

The Chad