【问题标题】:how to get specific attribute value from stdout json in ansible如何从ansible中的stdout json获取特定的属性值
【发布时间】:2020-08-22 16:57:25
【问题描述】:

instance_tags.stdout 输出看起来像

休闲 sn-p 的预期输出是 App Server-Development

- name: get instance tags
  win_shell: aws ec2 --region us-east-1 describe-tags \ --filters "Name=resource-id,Values={{instance_id}}"
  register: instance_tags


- name: echo instance tags
  debug:
    msg: "{{instance_tags.stdout | json_query('Tags[?Key==''AMSPatchGroup''].Value')}}"

但实际输出是

【问题讨论】:

  • 你能分享以下调试的输出吗:msg: "{{instance_tags.stdout.Tags[0] }}"
  • 请不要将代码sn-ps作为图片发布;它在how to ask 页面中明确提及

标签: ansible ansible-facts ansible-template json-query


【解决方案1】:

你至少想要instance_tags.stdout | from_json | ...,因为.stdout 是一个字符串,而不是一个实际的结构

您可以通过一个简单的实验看到这种行为,该实验显示 {{ '{"a":"b"}' | json_query("a") }} 返回 "" 就像您的经验一样,但 {{ '{"a":"b"}' | from_json | json_query("a") }} 产生 "b" 就像人们所期望的那样

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多