使用过滤器从字典中获取值

问题描述 投票:0回答:2

我有一本字典,而不是字典列表,假设我从 JSON 文件 some.json 读取了一个字典有内容

{"a": "b"}

我想使用过滤器提取值,这样我就可以使用更多过滤器来处理它

# Expecting 'lookup('file', 'something.json') | from_json | ???' gives the value 'b'
- debug:
    msg: "{{ lookup('file', 'something.json') | from_json | ??? | urlencode() }}"

我搜索到所有其他示例都是字典列表,但我这里只有一个字典

ansible
2个回答
3
投票

鉴于以下文件

shell> cat something.json 
{"a": "b"}

问:“使用过滤器从字典中获取值。”

A:我读了你的问题:“在不知道密钥的情况下从哈希中获取值。”有很多选择:

  1. 将文件中的变量包含到字典中,例如d1,并显示第一个值
- hosts: localhost
  tasks:
    - include_vars:
        file: something.json
        name: d1
    - debug:
        msg: "{{ d1.values() | first }}"

给予

shell> ansible-playbook pb.yml

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

TASK [include_vars] **************************************************************************
ok: [localhost]

TASK [debug] *********************************************************************************
ok: [localhost] => 
  msg: b

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

  1. 您可以将声明放入 vars
- hosts: localhost
  vars:
    d1: "{{ lookup('file', 'something.json') }}"
    v1: "{{ d1.values() | first }}"
  tasks:
    - debug:
        var: v1
    - debug:
        var: d1 | type_debug
    - debug:
        var: d1

给予

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

TASK [debug] *********************************************************************************
ok: [localhost] => 
  v1: b

TASK [debug] *********************************************************************************
ok: [localhost] => 
  d1|type_debug: dict

TASK [debug] *********************************************************************************
ok: [localhost] => 
  d1:
    a: b

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

  1. 使用json_query
- hosts: localhost
  vars:
    d1: "{{ lookup('file', 'something.json') }}"
    v1: "{{ d1 | json_query('values(@)') | first }}"
  tasks:
    - debug:
        var: v1

给予

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

TASK [debug] *********************************************************************************
ok: [localhost] => 
  v1: b

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

以上所有选项均假设该文件在控制器上可用。如果文件位于远程主机:

  1. 使用吸食
- hosts: localhost
  vars:
    d1: "{{ out.content|b64decode }}"
    v1: "{{ d1.values() | first }}"
  tasks:
    - slurp:
        src: "{{ playbook_dir }}/something.json"
      register: out
    - debug:
        var: v1

给予

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

TASK [slurp] *********************************************************************************
ok: [localhost]

TASK [debug] *********************************************************************************
ok: [localhost] => 
  v1: b

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

  1. 您还可以注册 cat
  2. 的输出
- hosts: localhost
  vars:
    d1: "{{ out.stdout }}"
    v1: "{{ d1.values() | first }}"
  tasks:
    - command:
        cmd: "cat something.json"
        chdir: "{{ playbook_dir }}"
      register: out
    - debug:
        var: v1

给予

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

TASK [command] *******************************************************************************
changed: [localhost]

TASK [debug] *********************************************************************************
ok: [localhost] => 
  v1: b

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


0
投票

谢谢,Zeitounator,我使用了

- debug:
    msg: "{{ lookup('file', 'something.json') | from_json | dict2items | map(attribute='value')  | urlencode() }}"
© www.soinside.com 2019 - 2024. All rights reserved.