Home > Blockchain >  Ansible nested variables syntax issue, error "dict object has no attribute"
Ansible nested variables syntax issue, error "dict object has no attribute"

Time:05-27

I have the following data inside of Ansible variable server_info, set via register at the end of a task;

ok: [server123] => {
    "msg": {
        "changed": false,
        "failed": false,
        "instances": [
            {
                "ami_launch_index": 0,
                "architecture": "x86_64",
                "block_device_mappings": [
                    {
                        "device_name": "/dev/sda1",
                        "ebs": {
                            "attach_time": "2021-02-11T11:48:30 00:00",
                            "delete_on_termination": false,
                            "status": "attached",
                            "volume_id": "vol-5436546546"
                        }
                    },
                    {
                        "device_name": "/dev/sda2",
                        "ebs": {
                            "attach_time": "2021-02-11T11:48:30 00:00",
                            "delete_on_termination": false,
                            "status": "attached",
                            "volume_id": "vol-3546456535"
                        }
                    }
                ],
                "capacity_reservation_specification": {
                    "capacity_reservation_preference": "open"
                },
                "client_token": "",
                "cpu_options": {
                    "core_count": 2,
                    "threads_per_core": 1
                },
                "ebs_optimized": false,
                "ena_support": true,
                "enclave_options": {
                    "enabled": false
                },
                "hibernation_options": {
                    "configured": false
                },
                "hypervisor": "xen",
                "iam_instance_profile": {
                    "arn": "arn:aws:iam::6346346356:instance-profile/bla",
                    "id": "SXIUIAHFKBAFIUAEKBADF"
                },
                "image_id": "ami-65754765475476",
                "instance_id": "i-4657654765476547765",
                "instance_type": "t2.large",
                "key_name": "bla",
                "launch_time": "2021-02-15T08:51:44 00:00",
                "maintenance_options": {
                    "auto_recovery": "default"
                },

                etc... etc...

            }
        ]
    }
}

I am attempting to retrieve the value of "instance_id", however I get the following error;

- name: "Instance ID"
  debug:
    msg: "{{ server_info.instances.instance_id }}"

TASK [Instance ID] ************************************************************************************************************ fatal: [server123]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'instance_id'\n\nThe error appears to be in '/ansible/testing.yml': line 38, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: "Instance ID"\n ^ here\n"}

Where am I going wrong in my nested variable retrieval syntax?

Thank you.

CodePudding user response:

See if the below code works

  - name: get instance id 
    set_fact: 
      new_result: "{{ server_info.instances['instances'] }}"
    register: filterd_result
  
  - name: get all ids
    vars:
      reduce_query: >-
        [].{
        instance_id: instance_id
        }
      names: "{{ new_result | json_query(reduce_query)}}"
    register: result_names
    no_log: true
    debug:
      var: names

  - name: display result
    debug:
      msg: "{{ result_names.names | map(attribute='instance_id') | flatten }}"

CodePudding user response:

just modify your task:

- name: "Instance ID"
  debug:
    msg: "{{ server_info.instances.0.instance_id }}"

instances is an array with one item...

  • Related