Search code examples
jsonamazon-web-servicesdictionaryansibleamazon-ecs

Ansible fetching Stdout key value


I need some help regarding, how to fetch specific value using Ansible

My task:

- name: 'check Describe Information'
  debug: 
  var: describeresult.stdout

I need IP address value from below stdout, what should I put in debug var to fetch IP address

TASK [check Describe Information] **********************************************
task path: /home/tom/Getipaddress.yml:28
ok: [127.0.0.1] => {
    "describeresult.stdout": {
        "failures": [], 
        "tasks": [
            {
                "attachments": [
                    {
                        "details": [
                            {
                                "name": "subnetId", 
                                "value": "subnet-xxxxxxxxxxxxxx"
                            }, 
                            {
                                "name": "networkInterfaceId", 
                                "value": "eni-xxxxxxxxxxxxxxxx"
                            }, 
                            {
                                "name": "macAddress", 
                                "value": "xxxxxxxxxxxxxxxxxx"
                            }, 
                            {
                                "name": "privateIPv4Address", 
                                "value": "xxxxxxxxxxxxxxxxxx"
                            }

Plus I am using AWS ECS command to generate the above output with --output as a JSON not sure how to use --query to filter or fetch above IP address only


Solution

  • Use json_query. For example

        - set_fact:
            my_privateIPv4Address: "{{ describeresult.stdout.tasks|
                                       json_query(query) }}"
          vars:
            query: "[].attachments[].details[?name=='privateIPv4Address'].value"
    

    json_query returns by default a list.