在 Ansible 中替换字典列表中的值 [英] Replace value in list of dict in Ansible

查看:22
本文介绍了在 Ansible 中替换字典列表中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试替换键extension_last_heartbeat_time"的值;(日期)带有静态字符串";在这个字典列表中

<预><代码>[{vcenter":vcenter-A",vcenter_extension_info":[{extension_company":VMware Inc.",extension_key":com.vmware.vim.sms",extension_label":VMware vCenter 存储监控服务",extension_last_heartbeat_time":2020-11-03T09:05:41.676497+00:00",extension_type":",extension_version":5.5";},{extension_company":VMware Inc.",extension_key":com.vmware.vim.vsm",extension_last_heartbeat_time":2020-11-03T09:05:41.678007+00:00",extension_type":",extension_version":6.5";},{extension_company":空,extension_key":虚拟中心",extension_last_heartbeat_time":2020-11-03T09:05:41.684018+00:00",extension_type":",extension_version":1.0";}]},{vcenter":vcenter-B",vcenter_extension_info":[{extension_company":VMware Inc.",extension_key":com.vmware.vim.sms",extension_last_heartbeat_time":2020-08-17T13:12:10.529370+00:00",extension_type":",extension_version":5.5";},{extension_company":VMware Inc.",extension_key":com.vmware.vim.vsm",extension_last_heartbeat_time":2020-08-17T13:12:10.530946+00:00",extension_type":",extension_version":6.5";},{extension_company":空,extension_key":虚拟中心",extension_last_heartbeat_time":2020-08-17T13:12:10.537281+00:00",extension_version":1.0";}]}]

我试过了:

  • name:匿名化上次心跳时间设置事实:字典:"{{ 字典 |map('regex_replace', '[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.*', '') |列表}}"

但它删除了很大一部分数据

解决方案

Jinja2 数据结构是活的",因此它们在所有 jinja2 评估上下文中都会发生变化.诀窍在于(至少在这篇文章中)set 语句创建了新的 local 变量,并且没有按照用户期望的方式分配,从而导致一些愚蠢的行为.这就是为什么必须使用 dict.update 作为覆盖此 dict 成员"的原因.解决办法

- set_fact:# 你没有说哪个变量包含最外层的`list[dict]`# 所以根据需要改变它vcenter_data: |{%- for i in vcenter_data -%}{%- for e in i.vcenter_extension_info -%}{%- set _ = e.update({extension_last_heartbeat_time":new_extension_last_heartbeat_time}) -%}{%- endfor -%}{%- endfor -%}{{ vCenter_data }}变量:new_extension_last_heartbeat_time: '1111-22-33'

I'm trying to replace the value of the key "extension_last_heartbeat_time" (date) with a static string "<LAST_HEARTBEAT_TIME>" in this list of dicts

[
        {
            "vcenter": "vcenter-A",
            "vcenter_extension_info": [
                {
                    "extension_company": "VMware Inc.",
                    "extension_key": "com.vmware.vim.sms",
                    "extension_label": "VMware vCenter Storage Monitoring Service",
                    "extension_last_heartbeat_time": "2020-11-03T09:05:41.676497+00:00",
                    "extension_type": "",
                    "extension_version": "5.5"
                },
                {
                    "extension_company": "VMware Inc.",
                    "extension_key": "com.vmware.vim.vsm",
                    "extension_last_heartbeat_time": "2020-11-03T09:05:41.678007+00:00",
                    "extension_type": "",
                    "extension_version": "6.5"
                },
                {
                    "extension_company": null,
                    "extension_key": "VirtualCenter",
                    "extension_last_heartbeat_time": "2020-11-03T09:05:41.684018+00:00",
                    "extension_type": "",
                    "extension_version": "1.0"
                }
            ]
        },
        {
            "vcenter": "vcenter-B",
            "vcenter_extension_info": [
                {
                    "extension_company": "VMware Inc.",
                    "extension_key": "com.vmware.vim.sms",
                    "extension_last_heartbeat_time": "2020-08-17T13:12:10.529370+00:00",
                    "extension_type": "",
                    "extension_version": "5.5"
                },
                {
                    "extension_company": "VMware Inc.",
                    "extension_key": "com.vmware.vim.vsm",
                    "extension_last_heartbeat_time": "2020-08-17T13:12:10.530946+00:00",
                    "extension_type": "",
                    "extension_version": "6.5"
                },
                {
                    "extension_company": null,
                    "extension_key": "VirtualCenter",
                    "extension_last_heartbeat_time": "2020-08-17T13:12:10.537281+00:00",
                    "extension_version": "1.0"
                }
            ]
        }
]

I tried :

  • name: anonymize last heartbeat time set_fact: dict: "{{ dict | map('regex_replace', '[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.*', '<LAST_HEARTBEAT_TIME>') | list }}"

But it removes a big part of the data

解决方案

Jinja2 data structures are "live", so they are subject to mutation in all jinja2 evaluation contexts. The trick is that (at least as of this post) the set statement creates new local variables, and does not assign the way a user might expect, leading to some silliness. That's why one must use dict.update as a "overwrite this dict member" work-around

- set_fact:
    # you didn't say what variable contained that outermost `list[dict]`
    # so change that as needed
    vcenter_data: |
        {%- for i in vcenter_data -%}
        {%-   for e in i.vcenter_extension_info -%}
        {%-     set _ = e.update({
            "extension_last_heartbeat_time": new_extension_last_heartbeat_time}) -%}
        {%-   endfor -%}
        {%- endfor -%}
        {{ vcenter_data }}
  vars:
    new_extension_last_heartbeat_time: '1111-22-33'

这篇关于在 Ansible 中替换字典列表中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆