如何从 Ansible 调试的字典键值中删除不需要的字符串

How to remove unwanted string from dictionary key value from ansible debug

当我 运行 一个 ansible 剧本

时,我有以下输出
ok: [localhost] => {
    "msg": [
        {
            "last-heard-processing-time": "18611 secs ago(took 15 secs)",
            "name": "XXXX",
            "status": "conn:Getting ALL"
        },
        {
            "last-heard-processing-time": "20375 secs ago(took 17 secs)",
            "name": "AAAA",
            "status": "conn:Getting ALL"
        },
        {
            "last-heard-processing-time": "20292 secs ago(took 11 secs)",
            "name": "BBBB",
            "status": "conn:Getting ALL"
        },
        {
            "last-heard-processing-time": "4 secs ago(took 13 secs)",
            "name": "CCCC",
            "status": "conn:idle"
        },
        {
            "last-heard-processing-time": "51 secs ago(took 10 secs)",
            "name": "DDDD",
            "status": "conn:idle"
        },
        {
            "last-heard-processing-time": "53 secs ago(took 10 secs)",
            "name": "EEEEE",
            "status": "conn:idle"
        },
        {
            "last-heard-processing-time": "9 secs ago(took 10 secs)",
            "name": "FFFF",
            "status": "conn:idle"
        }
    ]
}

需要专家的一些 help/hint/clue(我是 ansible 的新手)我需要使用哪个过滤器来删除 'last-heard-processing-time' 键值中的额外字符串,以便我可以得到低于输出,我只是需要最后听到的处理时间键值中的第一个数字

ok: [localhost] => {
    "msg": [
        {
            "last-heard-processing-time": "18611",
            "name": "XXXX",
            "status": "conn:Getting ALL"
        },
        {
            "last-heard-processing-time": "20375",
            "name": "AAAA",
            "status": "conn:Getting ALL"
        },
        {
            "last-heard-processing-time": "20292",
            "name": "BBBB",
            "status": "conn:Getting ALL"
        },
        {
            "last-heard-processing-time": "4",
            "name": "CCCC",
            "status": "conn:idle"
        },
        {
            "last-heard-processing-time": "51",
            "name": "DDDD",
            "status": "conn:idle"
        },
        {
            "last-heard-processing-time": "53",
            "name": "EEEEE",
            "status": "conn:idle"
        },
        {
            "last-heard-processing-time": "9",
            "name": "FFFF",
            "status": "conn:idle"
        }
    ]
}

我正在尝试使用 regex_replace

msg: "{{output.msg| regex_replace('\d+\ssecs\sago\(took\s\d+\ssecs\)', '\d+')}}"

获取错误

An exception occurred during task execution. To see the full traceback, use -vvv. The error was: re.error: bad escape \d at position 0
fatal: [localhost]: FAILED! => {}

创建第一个数字的列表,例如

    - set_fact:
        time: "{{ output.msg|
                  map(attribute='last-heard-processing-time')|
                  map('regex_replace', '^(\d+)\s+.*$', '\1') }}"

给予

  time:
  - '18611'
  - '20375'
  - '20292'
  - '4'
  - '51'
  - '53'
  - '9'

然后合并更新的字典并连接列表

    - set_fact:
        out: "{{ out|d([]) +
                 [item.0|combine({'last-heard-processing-time': item.1})] }}"
      with_together:
        - "{{ output.msg }}"
        - "{{ time }}"

给予

  out:
  - last-heard-processing-time: '18611'
    name: XXXX
    status: conn:Getting ALL
  - last-heard-processing-time: '20375'
    name: AAAA
    status: conn:Getting ALL
  - last-heard-processing-time: '20292'
    name: BBBB
    status: conn:Getting ALL
  - last-heard-processing-time: '4'
    name: CCCC
    status: conn:idle
  - last-heard-processing-time: '51'
    name: DDDD
    status: conn:idle
  - last-heard-processing-time: '53'
    name: EEEEE
    status: conn:idle
  - last-heard-processing-time: '9'
    name: FFFF
    status: conn:idle

更新后的时间属性值为字符串。如果您需要整数,请添加 int 过滤器。

    - set_fact:
        time: "{{ output.msg|
                  map(attribute='last-heard-processing-time')|
                  map('regex_replace', '^(\d+)\s+.*$', '\1')|
                  map('int') }}"