简体   繁体   English

Ansible 剧本 json_query

[英]Ansible playbook json_query

After having loaded the following Ansible local facts :加载以下 Ansible 本地事实后:

    {
    "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 eg cdbs.*.char_set == 'AL32UTF8', but am not able to figure out how to add the filter condition in a task:我想在过滤器中使用它们以仅选择例如 cdbs.*.char_set == 'AL32UTF8',但我无法弄清楚如何在任务中添加过滤条件:

- 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, ie e01ca605 etc ?除此之外,是否有可能获得项目名称的列表,即 e01ca605 等?

Any help would be appreciated!任何帮助,将不胜感激!

Regards, Dirk问候, 德克

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)  }}"

Q: Select cdbs.*.char_set == 'AL32UTF8'问:选择 cdbs.*.char_set == 'AL32UTF8'

A: The tasks below A:下面的任务

    - 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. A:应用keys()方法。 For example例如

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

give

    "cdbs_keys": [
        "e01ca900", 
        "e01ca602", 
        "e01ca603", 
        "e01ca601", 
        "e01ca604", 
        "e01ca605"
    ]

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干杯,德克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM