reading json like variable in ansible -


i'm new ansible , i'm having problem reading value json file in ansible role. variable has value following:

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

i'm trying print color's value compare other variable. used with_dict iterate on json object (stored in variable called jsonvar) following:

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

so far there no luck in getting comparison of color's value json "blue" if statement. wondering if i'm doing wrong? in advance!

you can read json file using lookup plugin called file , pass from_json jinja2 filter. had mistake in with_dict loop, since have loop on jsonvar['queue'], not jsonvar. here complete code 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 }}" ;             echo "success"         fi 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -