大网数据广告活动
服务器运维问答

How to parse a map?

· 52CE 我爱测

I'm trying to create a list in ansible which consists of some docker container information. First, I'm running a command module which returns this in stdout: "map[key1:value1 key2:value2 key3:value3]" How can I parse this further…

问题原文

原帖链接 · dywan666 · CC BY-SA 4.0

I'm trying to create a list in ansible which consists of some docker container information. First, I'm running a command module which returns this in stdout:

"map[key1:value1 key2:value2 key3:value3]"

How can I parse this further to get the values based on the key that I provide? When I use a map filter, I get this:

"msg": "<generator object do_map at 0x7f3845b8a740>"

If I run the list filter, I just get output as every single character in the map, so ["m", "a", "p", "[", "k", ...]

What filter should I use?

已采纳解答

原帖链接 · Vladimir Botka · CC BY-SA 4.0

For example

    - set_fact:
        x: "{{ _dict|from_yaml }}"
      vars:
        _regex: '^(.*)\[(.*)\](.*)$'
        _key: "{{ stdout|regex_replace(_regex, '\\1') }}"
        _val: "{{ stdout|regex_replace(_regex, '\\2') }}"
        _dict: |
          {{ _key }}:
          {% for i in _val.split() %}
            {{ i|regex_replace(':', ': ') }}
          {% endfor %}

gives

  x:
    map:
      key1: value1
      key2: value2
      key3: value3

If there were more lines, e.g.

    stdout_lines:
      - "map1[key1:value1 key2:value2 key3:value3]"
      - "map2[key1:value1 key2:value2 key3:value3]"
      - "map3[key1:value1 key2:value2 key3:value3]"

it would be possible to combine the dictionary, e.g.

    - set_fact:
        x: "{{ x|d({})|combine(_dict|from_yaml) }}"
      loop: "{{ stdout_lines }}"
      vars:
        _regex: '^(.*)\[(.*)\](.*)$'
        _key: "{{ item|regex_replace(_regex, '\\1') }}"
        _val: "{{ item|regex_replace(_regex, '\\2') }}"
        _dict: |
          {{ _key }}:
          {% for i in _val.split() %}
            {{ i|regex_replace(':', ': ') }}
          {% endfor %}

gives

  x:
    map1:
      key1: value1
      key2: value2
      key3: value3
    map2:
      key1: value1
      key2: value2
      key3: value3
    map3:
      key1: value1
      key2: value2
      key3: value3

If the keys were repeating, e.g.

    stdout_lines:
      - "map[key1:value1 key2:value2 key3:value3]"
      - "map[key1:value1 key2:value2 key3:value3]"
      - "map[key1:value1 key2:value2 key3:value3]"

it would be possible to concatenate a list, e.g.

    - set_fact:
        x: "{{ x|d([]) + [_dict|from_yaml] }}"
      loop: "{{ stdout_lines }}"
      vars:
        _regex: '^(.*)\[(.*)\](.*)$'
        _key: "{{ item|regex_replace(_regex, '\\1') }}"
        _val: "{{ item|regex_replace(_regex, '\\2') }}"
        _dict: |
          {{ _key }}:
          {% for i in _val.split() %}
            {{ i|regex_replace(':', ': ') }}
          {% endfor %}

gives

  x:
  - map:
      key1: value1
      key2: value2
      key3: value3
  - map:
      key1: value1
      key2: value2
      key3: value3
  - map:
      key1: value1
      key2: value2
      key3: value3

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-06-21)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。