首页 > 解决方案 > How to Json query in Ansible

问题描述

I am using ec2_lc_facts ansible module and registering the output to a variable called lc_facts. I am able to grab the block_device_mappings value using json query "{{ lc_facts.launch_configurations|json_query('[*].block_device_mappings') }}" but want to grab the volume_size and volume_type from the following output. Please help.

 "lc_facts": {
        "changed": false, 
        "failed": false, 
        "launch_configurations": [
            {
                "block_device_mappings": [
                    {
                        "device_name": "/dev/sda1", 
                        "ebs": {
                            "delete_on_termination": true, 
                            "volume_size": 40, 
                            "volume_type": "gp2"
                        }
                    }
                ]
            }
       ]
  }

标签: pythonarraysjsonansiblejmespath

解决方案


The query below

- debug:
    msg: "{{ lc_facts.launch_configurations|json_query('[*].block_device_mappings[0].ebs.[volume_size, volume_type]') }}"

gives

    "msg": [
    [
        40, 
        "gp2"
    ]
]

To get hashes use this one

- debug:                                                                                                                          
    msg: "{{ lc_facts.launch_configurations|json_query('[*].block_device_mappings[0].ebs.{size: volume_size, type: volume_type}') }}" 

推荐阅读