Ansible - 通过循环注册 GET 响应,多个相同的字典

Posted

技术标签:

【中文标题】Ansible - 通过循环注册 GET 响应,多个相同的字典【英文标题】:Ansible - register GET response over loop, multiple same dict's 【发布时间】:2021-10-13 04:54:57 【问题描述】:

我正在尝试从已循环的已注册 GET 响应中获取 ID 值。但是因为它是循环的,所以寄存器 var 现在多次包含相同的结构。

“get_input_id”变量是从之前完成的已注册 GET 响应中提取的。

我需要将提取器 ID 与主机文件中定义的提取器标题进行匹配并验证。所以我以后可以做一个 PUT 请求,我必须使用 input_id 和 extractor_id

任何人有任何线索让我得到这个工作?,我正在考虑将字典转换为列表,然后迭代它们从 json -> 提取器 -> id 获取 ID

  - name: "API GET Input Extractors"
    vars:
      get_input_titles: " get_graylog_inputs.json.inputs | selectattr('title', '==', input.title) | map(attribute='title') | first "
      get_input_id: " get_graylog_inputs.json.inputs | selectattr('title', '==', input.title) | map(attribute='id') | first "
    uri:
      url: http:// ansible_host :9000/api/system/inputs/ get_input_id /extractors
      url_username : " system.users.triple_admin.api_token "
      url_password: token
      method: GET
      return_content: yes
    register: get_input_extractors
    loop: " inputs "
    loop_control:
      loop_var: input
    when: "input.title in get_input_titles"
    tags:
      - get_input_extractors

  - name: debug
    debug:
      msg: " get_input_extractors "

  - name: debug
    vars:
      get_input_extractor_id: " get_input_extractors.json.extractors | selectattr('title', '==', input.1.title) | map(attribute='id') "
    debug:
      msg: " get_input_extractor_id "
    loop: " inputs|subelements('extractors') "
    loop_control:
      loop_var: input

以下是已注册 var "get_input_extractor" 的精简版(第一个调试任务) 编辑:更具代表性的寄存器 var 输出 这个在没有 json 属性的结果中包含一个列表对象


    "changed": false,
    "msg": "All items completed",
    "results": [
        "ansible_loop_var": "input",
        "json": 
            "extractors": [
                "condition_type": "regex",
                "condition_value": "^.*(ASA-\\d-106023).*$",
                "converter_exceptions": 0,
                "converters": [],
                "creator_user_id": "triple_admin",
                "cursor_strategy": "copy",
                "exceptions": 0,
                "extractor_config": 
                    "grok_pattern": "%CISCOTAG:logid: %CISCO_ACTION:action %WORD:proto src %NOTSPACE:srcint:%IPV4:srcip/%BASE10NUM:srcport dst %NOTSPACE:dstint:%IPV4:dstip/%BASE10NUM:dstport by access-group %QUOTEDSTRING:acl"
                ,
                "id": "a2d14160-f8f9-11eb-8d07-005056991190",
                "metrics": 
                    "condition": 
                        "duration_unit": "microseconds",
                        "rate": 
                            "fifteen_minute": 0,
                            "five_minute": 0,
                            "mean": 0,
                            "one_minute": 0,
                            "total": 0
                        ,
                        "rate_unit": "events/second",
                        "time": 
                            "95th_percentile": 0,
                            "98th_percentile": 0,
                            "99th_percentile": 0,
                            "max": 0,
                            "mean": 0,
                            "min": 0,
                            "std_dev": 0
                        
                    ,
                    "condition_hits": 0,
                    "condition_misses": 0,
                    "converters": 
                        "duration_unit": "microseconds",
                        "rate": 
                            "fifteen_minute": 0,
                            "five_minute": 0,
                            "mean": 0,
                            "one_minute": 0,
                            "total": 0
                        ,
                        "rate_unit": "events/second",
                        "time": 
                            "95th_percentile": 0,
                            "98th_percentile": 0,
                            "99th_percentile": 0,
                            "max": 0,
                            "mean": 0,
                            "min": 0,
                            "std_dev": 0
                        
                    ,
                    "execution": 
                        "duration_unit": "microseconds",
                        "rate": 
                            "fifteen_minute": 0,
                            "five_minute": 0,
                            "mean": 0,
                            "one_minute": 0,
                            "total": 0
                        ,
                        "rate_unit": "events/second",
                        "time": 
                            "95th_percentile": 0,
                            "98th_percentile": 0,
                            "99th_percentile": 0,
                            "max": 0,
                            "mean": 0,
                            "min": 0,
                            "std_dev": 0
                        
                    ,
                    "total": 
                        "duration_unit": "microseconds",
                        "rate": 
                            "fifteen_minute": 0,
                            "five_minute": 0,
                            "mean": 0,
                            "one_minute": 0,
                            "total": 0
                        ,
                        "rate_unit": "events/second",
                        "time": 
                            "95th_percentile": 0,
                            "98th_percentile": 0,
                            "99th_percentile": 0,
                            "max": 0,
                            "mean": 0,
                            "min": 0,
                            "std_dev": 0
                        
                    
                ,
                "order": 0,
                "source_field": "full_message",
                "target_field": "",
                "title": "ASA106023 TCP/UDP Denies",
                "type": "grok"
            ],
            "total": 1
        ,
        "msg": "OK (1780 bytes)",
        "redirected": false,
        "status": 200,
        "url": "http://192.168.21.82:9000/api/system/inputs/6104170beca15547502665d6/extractors",
        "x_graylog_node_id": "ba52ad48-0b13-419d-b957-d47d8911b413",
        "x_runtime_microseconds": "2296"
    , 
        "ansible_loop_var": "input",
        "json": 
            "extractors": [
                "condition_type": "regex",
                "condition_value": "^.*(ASA-\\d-106023).*$",
                "converter_exceptions": 0,
                "converters": [],
                "creator_user_id": "triple_admin",
                "cursor_strategy": "copy",
                "exceptions": 0,
                "extractor_config": 
                    "grok_pattern": "%CISCOTAG:logid: %CISCO_ACTION:action %WORD:proto src %NOTSPACE:srcint:%IPV4:srcip/%BASE10NUM:srcport dst %NOTSPACE:dstint:%IPV4:dstip/%BASE10NUM:dstport by access-group %QUOTEDSTRING:acl"
                ,
                "id": "bdd27c20-f8fb-11eb-8d07-005056991190",
                "metrics": 
                    "condition": 
                        "duration_unit": "microseconds",
                        "rate": 
                            "fifteen_minute": 0,
                            "five_minute": 0,
                            "mean": 0,
                            "one_minute": 0,
                            "total": 0
                        ,
                        "rate_unit": "events/second",
                        "time": 
                            "95th_percentile": 0,
                            "98th_percentile": 0,
                            "99th_percentile": 0,
                            "max": 0,
                            "mean": 0,
                            "min": 0,
                            "std_dev": 0
                        
                    ,
                    "condition_hits": 0,
                    "condition_misses": 0,
                    "converters": 
                        "duration_unit": "microseconds",
                        "rate": 
                            "fifteen_minute": 0,
                            "five_minute": 0,
                            "mean": 0,
                            "one_minute": 0,
                            "total": 0
                        ,
                        "rate_unit": "events/second",
                        "time": 
                            "95th_percentile": 0,
                            "98th_percentile": 0,
                            "99th_percentile": 0,
                            "max": 0,
                            "mean": 0,
                            "min": 0,
                            "std_dev": 0
                        
                    ,
                    "execution": 
                        "duration_unit": "microseconds",
                        "rate": 
                            "fifteen_minute": 0,
                            "five_minute": 0,
                            "mean": 0,
                            "one_minute": 0,
                            "total": 0
                        ,
                        "rate_unit": "events/second",
                        "time": 
                            "95th_percentile": 0,
                            "98th_percentile": 0,
                            "99th_percentile": 0,
                            "max": 0,
                            "mean": 0,
                            "min": 0,
                            "std_dev": 0
                        
                    ,
                    "total": 
                        "duration_unit": "microseconds",
                        "rate": 
                            "fifteen_minute": 0,
                            "five_minute": 0,
                            "mean": 0,
                            "one_minute": 0,
                            "total": 0
                        ,
                        "rate_unit": "events/second",
                        "time": 
                            "95th_percentile": 0,
                            "98th_percentile": 0,
                            "99th_percentile": 0,
                            "max": 0,
                            "mean": 0,
                            "min": 0,
                            "std_dev": 0
                        
                    
                ,
                "order": 0,
                "source_field": "full_message",
                "target_field": "",
                "title": "ASA106023 TCP/UDP Denies",
                "type": "grok"
            ],
            "total": 1
        ,
        "msg": "OK (1780 bytes)",
        "redirected": false,
        "status": 200,
        "url": "http://192.168.21.82:9000/api/system/inputs/6104190eeca1554750266814/extractors",
        "x_graylog_node_id": "ba52ad48-0b13-419d-b957-d47d8911b413",
        "x_runtime_microseconds": "2251"
    , 
        "ansible_loop_var": "input",
        "changed": false,
        "input": 
            "configuration": 
                "allow_override_date": false,
                "bind_address": "0.0.0.0",
                "expand_structured_data": false,
                "force_rdns": false,
                "number_worker_threads": 8,
                "override_source": null,
                "port": 5996,
                "recv_buffer_size": null,
                "store_full_message": true
            ,
            "extractors": [
                "condition_type": "regex",
                "condition_value": "^.*(ASA-\\d-106023).*$",
                "converters": ,
                "cut_or_copy": "copy",
                "extractor_config": 
                    "pattern": "%CISCOTAG:logid: %CISCO_ACTION:action %WORD:proto src %NOTSPACE:srcint:%IPV4:srcip/%BASE10NUM:srcport dst %NOTSPACE:dstint:%IPV4:dstip/%BASE10NUM:dstport by access-group %QUOTEDSTRING:acl",
                    "type": "grok_pattern"
                ,
                "extractor_type": "grok",
                "order": 0,
                "source_field": "full_message",
                "target_field": "",
                "title": "ASA106023 TCP/UDP Denies"
            ],
            "global": false,
            "running": true,
            "title": "ansible test input",
            "type": "org.graylog2.inputs.syslog.udp.SyslogUDPInput"
        ,
        "skip_reason": "Conditional result was False",
        "skipped": true
    ]

下面是第二个调试任务的输出,我试图从每个输入提取器中提取“ID”值。

ok: [graylog1.tc2.triple-it.nl] => (item=['title': 'test_input_api', 'running': True, 'type': 'org.graylog2.inputs.syslog.udp.SyslogUDPInput', 'global': False, 'configuration': 'allow_override_date': False, 'bind_address': '0.0.0.0', 'expand_structured_data': False, 'force_rdns': False, 'number_worker_threads': 8, 'override_source': None, 'port': 5999, 'recv_buffer_size': None, 'store_full_message': True, 'extractors': ['title': 'ASA106023 TCP/UDP Denies', 'cut_or_copy': 'copy', 'source_field': 'full_message', 'target_field': '', 'extractor_type': 'grok', 'extractor_config': 'type': 'grok_pattern', 'pattern': '%CISCOTAG:logid: %CISCO_ACTION:action %WORD:proto src %NOTSPACE:srcint:%IPV4:srcip/%BASE10NUM:srcport dst %NOTSPACE:dstint:%IPV4:dstip/%BASE10NUM:dstport by access-group %QUOTEDSTRING:acl', 'converters': , 'condition_type': 'regex', 'condition_value': '^.*(ASA-\\d-106023).*$', 'order': 0], 'title': 'ASA106023 TCP/UDP Denies', 'cut_or_copy': 'copy', 'source_field': 'full_message', 'target_field': '', 'extractor_type': 'grok', 'extractor_config': 'type': 'grok_pattern', 'pattern': '%CISCOTAG:logid: %CISCO_ACTION:action %WORD:proto src %NOTSPACE:srcint:%IPV4:srcip/%BASE10NUM:srcport dst %NOTSPACE:dstint:%IPV4:dstip/%BASE10NUM:dstport by access-group %QUOTEDSTRING:acl', 'converters': , 'condition_type': 'regex', 'condition_value': '^.*(ASA-\\d-106023).*$', 'order': 0]) => 
    "msg": "<generator object do_map at 0x7fcf25a8aa98>"

ok: [graylog1.tc2.triple-it.nl] => (item=['title': 'test_input_api_2', 'running': True, 'type': 'org.graylog2.inputs.syslog.udp.SyslogUDPInput', 'global': False, 'configuration': 'allow_override_date': False, 'bind_address': '0.0.0.0', 'expand_structured_data': False, 'force_rdns': False, 'number_worker_threads': 8, 'override_source': None, 'port': 5995, 'recv_buffer_size': None, 'store_full_message': True, 'extractors': ['title': 'ASA106023 TCP/UDP Denies', 'cut_or_copy': 'copy', 'source_field': 'full_message', 'target_field': '', 'extractor_type': 'grok', 'extractor_config': 'type': 'grok_pattern', 'pattern': '%CISCOTAG:logid: %CISCO_ACTION:action %WORD:proto src %NOTSPACE:srcint:%IPV4:srcip/%BASE10NUM:srcport dst %NOTSPACE:dstint:%IPV4:dstip/%BASE10NUM:dstport by access-group %QUOTEDSTRING:acl', 'converters': , 'condition_type': 'regex', 'condition_value': '^.*(ASA-\\d-106023).*$', 'order': 0], 'title': 'ASA106023 TCP/UDP Denies', 'cut_or_copy': 'copy', 'source_field': 'full_message', 'target_field': '', 'extractor_type': 'grok', 'extractor_config': 'type': 'grok_pattern', 'pattern': '%CISCOTAG:logid: %CISCO_ACTION:action %WORD:proto src %NOTSPACE:srcint:%IPV4:srcip/%BASE10NUM:srcport dst %NOTSPACE:dstint:%IPV4:dstip/%BASE10NUM:dstport by access-group %QUOTEDSTRING:acl', 'converters': , 'condition_type': 'regex', 'condition_value': '^.*(ASA-\\d-106023).*$', 'order': 0]) => 
    "msg": "<generator object do_map at 0x7fcf25a8aba0>"

ok: [graylog1.tc2.triple-it.nl] => (item=['title': 'ansible test input', 'running': True, 'type': 'org.graylog2.inputs.syslog.udp.SyslogUDPInput', 'global': False, 'configuration': 'allow_override_date': False, 'bind_address': '0.0.0.0', 'expand_structured_data': False, 'force_rdns': False, 'number_worker_threads': 8, 'override_source': None, 'port': 5996, 'recv_buffer_size': None, 'store_full_message': True, 'extractors': ['title': 'ASA106023 TCP/UDP Denies', 'cut_or_copy': 'copy', 'source_field': 'full_message', 'target_field': '', 'extractor_type': 'grok', 'extractor_config': 'type': 'grok_pattern', 'pattern': '%CISCOTAG:logid: %CISCO_ACTION:action %WORD:proto src %NOTSPACE:srcint:%IPV4:srcip/%BASE10NUM:srcport dst %NOTSPACE:dstint:%IPV4:dstip/%BASE10NUM:dstport by access-group %QUOTEDSTRING:acl', 'converters': , 'condition_type': 'regex', 'condition_value': '^.*(ASA-\\d-106023).*$', 'order': 0], 'title': 'ASA106023 TCP/UDP Denies', 'cut_or_copy': 'copy', 'source_field': 'full_message', 'target_field': '', 'extractor_type': 'grok', 'extractor_config': 'type': 'grok_pattern', 'pattern': '%CISCOTAG:logid: %CISCO_ACTION:action %WORD:proto src %NOTSPACE:srcint:%IPV4:srcip/%BASE10NUM:srcport dst %NOTSPACE:dstint:%IPV4:dstip/%BASE10NUM:dstport by access-group %QUOTEDSTRING:acl', 'converters': , 'condition_type': 'regex', 'condition_value': '^.*(ASA-\\d-106023).*$', 'order': 0]) => 
    "msg": "<generator object do_map at 0x7fcf25a8ad00>"

编辑:再次尝试提取 id 值并将其与 host_vars 中定义的 title 变量相关联(认为这是一种很好的方法!)。

  - name: Get all ids in uri results
    vars:
      get_input_extractor_title: " get_input_extractors.results | map(attribute='json.extractors') | flatten | selectattr('title', '==', input.1.title) | map(attribute='title') | list "
      get_input_extractor_id: " get_input_extractors.results | map(attribute='json.extractors') | flatten | selectattr('title', '==', input.1.title) | map(attribute='id') | list "
    debug:
      msg:
        - " get_input_extractor_id "
    loop: " inputs|subelements('extractors') "
    loop_control:
      loop_var: input

下面是注册每个唯一 ID 号的播放输出。

TASK [graylog/extractors : Get all ids in uri results] ******************************************************************************************************************************
Tuesday 10 August 2021  15:26:49 +0200 (0:00:00.290)       0:00:07.225 ********
ok: [graylog1.tc2.triple-it.nl] => (item=['title': 'test_input_api', 'running': True, 'type': 'org.graylog2.inputs.syslog.udp.SyslogUDPInput', 'global': False, 'configuration': 'allow_override_date': False, 'bind_address': '0.0.0.0', 'expand_structured_data': False, 'force_rdns': False, 'number_worker_threads': 8, 'override_source': None, 'port': 5999, 'recv_buffer_size': None, 'store_full_message': True, 'extractors': ['title': 'test_input_api TCP/UDP Denies', 'cut_or_copy': 'copy', 'source_field': 'full_message', 'target_field': '', 'extractor_type': 'grok', 'extractor_config': 'type': 'grok_pattern', 'pattern': '%CISCOTAG:logid: %CISCO_ACTION:action %WORD:proto src %NOTSPACE:srcint:%IPV4:srcip/%BASE10NUM:srcport dst %NOTSPACE:dstint:%IPV4:dstip/%BASE10NUM:dstport by access-group %QUOTEDSTRING:acl', 'converters': , 'condition_type': 'regex', 'condition_value': '^.*(ASA-\\d-106023).*$', 'order': 0, 'title': 'test_input_api BLABLA 2', 'cut_or_copy': 'copy', 'source_field': 'full_message', 'target_field': '', 'extractor_type': 'grok', 'extractor_config': 'type': 'grok_pattern', 'pattern': '%CISCOTAG:logid: %CISCO_ACTION:action %WORD:proto src %NOTSPACE:srcint:%IPV4:srcip/%BASE10NUM:srcport dst %NOTSPACE:dstint:%IPV4:dstip/%BASE10NUM:dstport by access-group %QUOTEDSTRING:acl', 'converters': , 'condition_type': 'regex', 'condition_value': '^.*(ASA-\\d-106023).*$', 'order': 0], 'title': 'test_input_api TCP/UDP Denies', 'cut_or_copy': 'copy', 'source_field': 'full_message', 'target_field': '', 'extractor_type': 'grok', 'extractor_config': 'type': 'grok_pattern', 'pattern': '%CISCOTAG:logid: %CISCO_ACTION:action %WORD:proto src %NOTSPACE:srcint:%IPV4:srcip/%BASE10NUM:srcport dst %NOTSPACE:dstint:%IPV4:dstip/%BASE10NUM:dstport by access-group %QUOTEDSTRING:acl', 'converters': , 'condition_type': 'regex', 'condition_value': '^.*(ASA-\\d-106023).*$', 'order': 0]) => 
    "msg": [
        [
            "6b09eeb0-f9d7-11eb-8d07-005056991190"
        ]
    ]

ok: [graylog1.tc2.triple-it.nl] => (item=['title': 'test_input_api', 'running': True, 'type': 'org.graylog2.inputs.syslog.udp.SyslogUDPInput', 'global': False, 'configuration': 'allow_override_date': False, 'bind_address': '0.0.0.0', 'expand_structured_data': False, 'force_rdns': False, 'number_worker_threads': 8, 'override_source': None, 'port': 5999, 'recv_buffer_size': None, 'store_full_message': True, 'extractors': ['title': 'test_input_api TCP/UDP Denies', 'cut_or_copy': 'copy', 'source_field': 'full_message', 'target_field': '', 'extractor_type': 'grok', 'extractor_config': 'type': 'grok_pattern', 'pattern': '%CISCOTAG:logid: %CISCO_ACTION:action %WORD:proto src %NOTSPACE:srcint:%IPV4:srcip/%BASE10NUM:srcport dst %NOTSPACE:dstint:%IPV4:dstip/%BASE10NUM:dstport by access-group %QUOTEDSTRING:acl', 'converters': , 'condition_type': 'regex', 'condition_value': '^.*(ASA-\\d-106023).*$', 'order': 0, 'title': 'test_input_api BLABLA 2', 'cut_or_copy': 'copy', 'source_field': 'full_message', 'target_field': '', 'extractor_type': 'grok', 'extractor_config': 'type': 'grok_pattern', 'pattern': '%CISCOTAG:logid: %CISCO_ACTION:action %WORD:proto src %NOTSPACE:srcint:%IPV4:srcip/%BASE10NUM:srcport dst %NOTSPACE:dstint:%IPV4:dstip/%BASE10NUM:dstport by access-group %QUOTEDSTRING:acl', 'converters': , 'condition_type': 'regex', 'condition_value': '^.*(ASA-\\d-106023).*$', 'order': 0], 'title': 'test_input_api BLABLA 2', 'cut_or_copy': 'copy', 'source_field': 'full_message', 'target_field': '', 'extractor_type': 'grok', 'extractor_config': 'type': 'grok_pattern', 'pattern': '%CISCOTAG:logid: %CISCO_ACTION:action %WORD:proto src %NOTSPACE:srcint:%IPV4:srcip/%BASE10NUM:srcport dst %NOTSPACE:dstint:%IPV4:dstip/%BASE10NUM:dstport by access-group %QUOTEDSTRING:acl', 'converters': , 'condition_type': 'regex', 'condition_value': '^.*(ASA-\\d-106023).*$', 'order': 0]) => 
    "msg": [
        []
    ]

ok: [graylog1.tc2.triple-it.nl] => (item=['title': 'test_input_api_2', 'running': True, 'type': 'org.graylog2.inputs.syslog.udp.SyslogUDPInput', 'global': False, 'configuration': 'allow_override_date': False, 'bind_address': '0.0.0.0', 'expand_structured_data': False, 'force_rdns': False, 'number_worker_threads': 8, 'override_source': None, 'port': 5995, 'recv_buffer_size': None, 'store_full_message': True, 'extractors': ['title': 'test_input_api_2 TCP/UDP Denies', 'cut_or_copy': 'copy', 'source_field': 'full_message', 'target_field': '', 'extractor_type': 'grok', 'extractor_config': 'type': 'grok_pattern', 'pattern': '%CISCOTAG:logid: %CISCO_ACTION:action %WORD:proto src %NOTSPACE:srcint:%IPV4:srcip/%BASE10NUM:srcport dst %NOTSPACE:dstint:%IPV4:dstip/%BASE10NUM:dstport by access-group %QUOTEDSTRING:acl', 'converters': , 'condition_type': 'regex', 'condition_value': '^.*(ASA-\\d-106023).*$', 'order': 0], 'title': 'test_input_api_2 TCP/UDP Denies', 'cut_or_copy': 'copy', 'source_field': 'full_message', 'target_field': '', 'extractor_type': 'grok', 'extractor_config': 'type': 'grok_pattern', 'pattern': '%CISCOTAG:logid: %CISCO_ACTION:action %WORD:proto src %NOTSPACE:srcint:%IPV4:srcip/%BASE10NUM:srcport dst %NOTSPACE:dstint:%IPV4:dstip/%BASE10NUM:dstport by access-group %QUOTEDSTRING:acl', 'converters': , 'condition_type': 'regex', 'condition_value': '^.*(ASA-\\d-106023).*$', 'order': 0]) => 
    "msg": [
        [
            "6b9edbb0-f9d7-11eb-8d07-005056991190"
        ]
    ]

ok: [graylog1.tc2.triple-it.nl] => (item=['title': 'ansible test input', 'running': True, 'type': 'org.graylog2.inputs.syslog.udp.SyslogUDPInput', 'global': False, 'configuration': 'allow_override_date': False, 'bind_address': '0.0.0.0', 'expand_structured_data': False, 'force_rdns': False, 'number_worker_threads': 8, 'override_source': None, 'port': 5996, 'recv_buffer_size': None, 'store_full_message': True, 'extractors': ['title': 'ansible test input TCP/UDP Denies', 'cut_or_copy': 'copy', 'source_field': 'full_message', 'target_field': '', 'extractor_type': 'grok', 'extractor_config': 'type': 'grok_pattern', 'pattern': '%CISCOTAG:logid: %CISCO_ACTION:action %WORD:proto src %NOTSPACE:srcint:%IPV4:srcip/%BASE10NUM:srcport dst %NOTSPACE:dstint:%IPV4:dstip/%BASE10NUM:dstport by access-group %QUOTEDSTRING:acl', 'converters': , 'condition_type': 'regex', 'condition_value': '^.*(ASA-\\d-106023).*$', 'order': 0], 'title': 'ansible test input TCP/UDP Denies', 'cut_or_copy': 'copy', 'source_field': 'full_message', 'target_field': '', 'extractor_type': 'grok', 'extractor_config': 'type': 'grok_pattern', 'pattern': '%CISCOTAG:logid: %CISCO_ACTION:action %WORD:proto src %NOTSPACE:srcint:%IPV4:srcip/%BASE10NUM:srcport dst %NOTSPACE:dstint:%IPV4:dstip/%BASE10NUM:dstport by access-group %QUOTEDSTRING:acl', 'converters': , 'condition_type': 'regex', 'condition_value': '^.*(ASA-\\d-106023).*$', 'order': 0]) => 
    "msg": [
        [
            "6c3464f0-f9d7-11eb-8d07-005056991190"
        ]
    ]

【问题讨论】:

【参考方案1】:

首先,由于您循环,您的寄存器包含一个results 属性,它是所有单个结果的列表。如果您在“ your_registered_var.results ”上循环,则可以将它们用作“正常”(即未循环)结果。有关该主题的更多信息,请参阅Referencing variables with a loop。

接下来,您请求的服务将返回带有Content-type: application/json 的正确json 内容,这意味着uri module 在每个结果中返回一个json 键,其中包含您可以用作对象的已解码的json 字符串。

从您的上述问题中,我了解到(我对此有疑问,因此如果不是这种情况,请编辑您的问题以更具体)您的要求是获取每个 ids 属性的列表extractors 列表中的提取器。为此,您基本上需要

在您的results 列表中创建一个包含所有json.extractors 属性的列表。 展平该列表,以便所有json 对象中的所有extractors 元素都位于一个级别列表中。 仅提取列表中所有剩余元素中的id 字段。

您不需要任何花哨的json_query 请求,甚至不需要循环,如下面的剧本所示:

- hosts: localhost
  gather_facts: false

  vars:
    # Your orig data as a single json line for legibility
    get_input_extractors: "changed": false, "msg": "All items completed", "results": ["ansible_loop_var": "input", "json": "extractors": ["condition_type": "regex", "condition_value": "^.*(ASA-\\d-106023).*$", "converter_exceptions": 0, "converters": [], "creator_user_id": "triple_admin", "cursor_strategy": "copy", "exceptions": 0, "extractor_config": "grok_pattern": "%CISCOTAG:logid: %CISCO_ACTION:action %WORD:proto src %NOTSPACE:srcint:%IPV4:srcip/%BASE10NUM:srcport dst %NOTSPACE:dstint:%IPV4:dstip/%BASE10NUM:dstport by access-group %QUOTEDSTRING:acl", "id": "a2d14160-f8f9-11eb-8d07-005056991190", "metrics": "condition": "duration_unit": "microseconds", "rate": "fifteen_minute": 0, "five_minute": 0, "mean": 0, "one_minute": 0, "total": 0, "rate_unit": "events/second", "time": "95th_percentile": 0, "98th_percentile": 0, "99th_percentile": 0, "max": 0, "mean": 0, "min": 0, "std_dev": 0, "condition_hits": 0, "condition_misses": 0, "converters": "duration_unit": "microseconds", "rate": "fifteen_minute": 0, "five_minute": 0, "mean": 0, "one_minute": 0, "total": 0, "rate_unit": "events/second", "time": "95th_percentile": 0, "98th_percentile": 0, "99th_percentile": 0, "max": 0, "mean": 0, "min": 0, "std_dev": 0, "execution": "duration_unit": "microseconds", "rate": "fifteen_minute": 0, "five_minute": 0, "mean": 0, "one_minute": 0, "total": 0, "rate_unit": "events/second", "time": "95th_percentile": 0, "98th_percentile": 0, "99th_percentile": 0, "max": 0, "mean": 0, "min": 0, "std_dev": 0, "total": "duration_unit": "microseconds", "rate": "fifteen_minute": 0, "five_minute": 0, "mean": 0, "one_minute": 0, "total": 0, "rate_unit": "events/second", "time": "95th_percentile": 0, "98th_percentile": 0, "99th_percentile": 0, "max": 0, "mean": 0, "min": 0, "std_dev": 0, "order": 0, "source_field": "full_message", "target_field": "", "title": "ASA106023 TCP/UDP Denies", "type": "grok"], "total": 1, "msg": "OK (1780 bytes)", "redirected": false, "status": 200, "url": "http://192.168.21.82:9000/api/system/inputs/6104170beca15547502665d6/extractors", "x_graylog_node_id": "ba52ad48-0b13-419d-b957-d47d8911b413", "x_runtime_microseconds": "2301", "ansible_loop_var": "input", "json": "extractors": ["condition_type": "regex", "condition_value": "^.*(ASA-\\d-106023).*$", "converter_exceptions": 0, "converters": [], "creator_user_id": "triple_admin", "cursor_strategy": "copy", "exceptions": 0, "extractor_config": "grok_pattern": "%CISCOTAG:logid: %CISCO_ACTION:action %WORD:proto src %NOTSPACE:srcint:%IPV4:srcip/%BASE10NUM:srcport dst %NOTSPACE:dstint:%IPV4:dstip/%BASE10NUM:dstport by access-group %QUOTEDSTRING:acl", "id": "bdd27c20-f8fb-11eb-8d07-005056991190", "metrics": "condition": "duration_unit": "microseconds", "rate": "fifteen_minute": 0, "five_minute": 0, "mean": 0, "one_minute": 0, "total": 0, "rate_unit": "events/second", "time": "95th_percentile": 0, "98th_percentile": 0, "99th_percentile": 0, "max": 0, "mean": 0, "min": 0, "std_dev": 0, "condition_hits": 0, "condition_misses": 0, "converters": "duration_unit": "microseconds", "rate": "fifteen_minute": 0, "five_minute": 0, "mean": 0, "one_minute": 0, "total": 0, "rate_unit": "events/second", "time": "95th_percentile": 0, "98th_percentile": 0, "99th_percentile": 0, "max": 0, "mean": 0, "min": 0, "std_dev": 0, "execution": "duration_unit": "microseconds", "rate": "fifteen_minute": 0, "five_minute": 0, "mean": 0, "one_minute": 0, "total": 0, "rate_unit": "events/second", "time": "95th_percentile": 0, "98th_percentile": 0, "99th_percentile": 0, "max": 0, "mean": 0, "min": 0, "std_dev": 0, "total": "duration_unit": "microseconds", "rate": "fifteen_minute": 0, "five_minute": 0, "mean": 0, "one_minute": 0, "total": 0, "rate_unit": "events/second", "time": "95th_percentile": 0, "98th_percentile": 0, "99th_percentile": 0, "max": 0, "mean": 0, "min": 0, "std_dev": 0, "order": 0, "source_field": "full_message", "target_field": "", "title": "ASA106023 TCP/UDP Denies", "type": "grok"], "total": 1, "msg": "OK (1780 bytes)", "redirected": false, "status": 200, "url": "http://192.168.21.82:9000/api/system/inputs/6104190eeca1554750266814/extractors", "x_graylog_node_id": "ba52ad48-0b13-419d-b957-d47d8911b413", "x_runtime_microseconds": "2263"]

  tasks:
    - name: Get all ids in uri results
      debug:
        msg: " get_input_extractors.results | map(attribute='json.extractors') 
          | flatten | map(attribute='id') "
        # Note: if running ansible < 2.10 your will need to
        # explicitely turn the result into a list
        # msg: " get_input_extractors.results | map(attribute='json.extractors') 
        #   | flatten | map(attribute='id') | list "

这给出了:

$ ansible-playbook bla.yml

PLAY [localhost] ***********************************************************************************************************************************************************************************************************************

TASK [Get all ids in uri results] ******************************************************************************************************************************************************************************************************
ok: [localhost] => 
    "msg": [
        "a2d14160-f8f9-11eb-8d07-005056991190",
        "bdd27c20-f8fb-11eb-8d07-005056991190"
    ]


PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

【讨论】:

当我复制你建议的解决方案 1on1 作为测试时,我得到以下结果:ok: [localhost] =&gt; "msg": "&lt;generator object do_map at 0x7f6f3e416ba0&gt;" 当我将它应用到我的剧本时,我得到一个错误,我真的没有得到,因为 dict 肯定包含 results.json.extractors。 FAILED! =&gt; "msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'json'\n\nThe error appears to be in '/home/d.denouden/ansible/roles/graylog/extractors/tasks/get_extractors.yml': 我已经解决了在你的例子中我会得到generator object do_map at 0x7f6f3e416ba0 消息的问题,方法是在最后给调试消息一个| list。我仍然不确定为什么我不能直接在注册的 var 上运行完全相同的东西,仍然收到消息说字典中没有属性 json。 list 过滤器对于 ansible 是的,我正在运行 2.9.12。我发现我的剧本有问题。在registered_var-&gt;results 中还有一个没有 json 属性的列表对象。我已经更新了问题中的 registered_var 输出以包含此部分。所以我需要能够从他们可用的地方获取 id。 我想我明白了,更新问题!【参考方案2】:

这是您要查找的查询吗?

    - set_fact:
        get_input_extractor_id: " get_input_extractors.results|
                                    json_query(query) "
      vars:
        query: '[].json.extractors[].id'

【讨论】:

以上是关于Ansible - 通过循环注册 GET 响应,多个相同的字典的主要内容,如果未能解决你的问题,请参考以下文章

ansible常用的定义变量方式和变量注册facts缓存和template的使用

带有端口转发的 Ansible 多主机

010.Ansible_palybook 循环语句

consul+ansible+shell批量下发注册node_exporter

通过 Ansible 实现向目标多集群部署应用

Ansible WinRM Server 在步骤 5 之后没有响应 CredSSP 令牌