1

After having loaded the following Ansible local facts :

    {
    "cdbs": {
        "e01ca601": {
            "char_set": "AL32UTF8",
            "home": "/u01/dbhome_1",
            "npdbs": "1",
            "pdbs": "pdb1"
        },
        "e01ca602": {
            "char_set": "WE8ISO8859P1",
            "home": "/u01/dbhome_2",
            "npdbs": "0",
            "pdbs": ""
        },
        "e01ca603": {
            "char_set": "AL32UTF8",
            "home": "/u01/dbhome_3",
            "npdbs": "0",
            "pdbs": ""
        },
        "e01ca604": {
            "char_set": "WE8ISO8859P1",
            "home": "/u01/dbhome_1",
            "npdbs": "0",
            "pdbs": ""
        },
        "e01ca605": {
            "char_set": "AL32UTF8",
            "home": "/u01/dbhome_2",
            "npdbs": "0",
            "pdbs": ""
        },
        "e01ca900": {
            "char_set": "WE8ISO8859P1",
            "home": "/u01/dbhome_3",
            "npdbs": "1",
            "pdbs": "pdb2"
        }
    },
    "pdbs": {
        "pdb1": {
            "cdb": "e01ca601",
            "creation_time": "2020-01-21 14:10:39"
        },
        "pdb2": {
            "cdb": "e01ca900",
            "creation_time": "2020-01-13 13:34:21"
        }
    }
}

I would like to use them in a filter to select only on e.g. cdbs.*.char_set == 'AL32UTF8', but am not able to figure out how to add the filter condition in a task:

- name: "Task1"
  vars:
    myquery : '[cdbs.*.char_set][0]'
  debug:
    msg:
      - "Query condition: {{ myquery }}"
      - "Query filter   : {{ ansible_local | json_query(myquery) }}"

In addition to this would it be possible to get a list of names of the items, i.e. e01ca605 etc ?

Any help would be appreciated!

Regards, Dirk

Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
dirk
  • 21
  • 2

3 Answers3

1

I got this (using a pipe) to work:

- name: "Task4"
  vars:
    myquery: cdbs.* | [?char_set=='AL32UTF8']
  debug:
    msg:
      - "{{ item }}"
  with_items: "{{ ansible_local | json_query(myquery)  }}"
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
dirk
  • 21
  • 2
0

Q: Select cdbs.*.char_set == 'AL32UTF8'

A: The tasks below

    - set_fact:
        sel_AL32UTF8: "{{ cdbs|json_query(myquery) }}"
      vars:
        myquery: "*|[?char_set=='AL32UTF8']"
    - debug:
        var: sel_AL32UTF8

give

   "sel_AL32UTF8": [
        {
            "char_set": "AL32UTF8", 
            "home": "/u01/dbhome_3", 
            "npdbs": "0", 
            "pdbs": ""
        }, 
        {
            "char_set": "AL32UTF8", 
            "home": "/u01/dbhome_1", 
            "npdbs": "1", 
            "pdbs": "pdb1"
        }, 
        {
            "char_set": "AL32UTF8", 
            "home": "/u01/dbhome_2", 
            "npdbs": "0", 
            "pdbs": ""
        }
    ]

Q: Get a list of names.

A: Apply the keys() method. For example

    - set_fact:
        cdbs_keys: "{{ cdbs.keys()|list }}"
    - debug:
        var: cdbs_keys

give

    "cdbs_keys": [
        "e01ca900", 
        "e01ca602", 
        "e01ca603", 
        "e01ca601", 
        "e01ca604", 
        "e01ca605"
    ]
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
  • Hello Vladimir, your proposed tasks work just fine! In the meantime I also got this (using a pipe) to work: - name: "Task4" vars: myquery: cdbs.* | [?char_set=='AL32UTF8'] debug: msg: - "{{ item }}" with_items: "{{ ansible_local | json_query(myquery) }}" However, would it be possible to get a list of names of the items, i.e. e01ca605 etc ? cheers, Dirk – dirk Jan 22 '20 at 15:40
  • Hello Dirk. Sure, I've updated the answer. Cheers, Vladimir. – Vladimir Botka Jan 22 '20 at 15:53
  • Hello Vladimir, thanks this works fine as well. The idea is to combine these two: get the list of keys for the filter condition. Do you have a solution for this as well? Thanks & cheers, Dirk – dirk Jan 23 '20 at 09:04
  • added solution to this question in the answer section – dirk Jan 23 '20 at 11:49
0

finally, combining Vladimir's replies the solution found is:

    - name: "Task10"
  vars:
    myquery: "[?value.char_set=='AL32UTF8']"
  debug: var=item.key
  with_items: "{{ ansible_local.cdbs|dict2items|json_query(myquery) }}"

resulting in a list of items

ok: [exa101vm01] => (item={'key': u'e01ca900', 'value': {u'home': u'/u01/dbhome_1', u'npdbs': u'1', u'char_set': u'AL32UTF8', u'pdbs': u'pdb1', u'archivelog_mode': u'NOARCHIVELOG'}}) => {
"ansible_loop_var": "item",
"item": {
    "key": "e01ca900",
    "value": {
        "archivelog_mode": "NOARCHIVELOG",
        "char_set": "AL32UTF8",
        "home": "/u01/dbhome_1",
        "npdbs": "1",
        "pdbs": "pdb1"
    }
},
"item.key": "e01ca900"
} 
... (cut here)

Syntax and possibilities seem quite powerful, however difficult to debug ;-)

Thanks for your help!

Cheers, Dirk

dirk
  • 21
  • 2