Ansible 嵌套变量语法问题,错误 "dict object has no attribute"

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

我在 Ansible 变量 server_info 中有以下数据,在任务结束时通过 register 设置;

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...

            }
        ]
    }
}

我正在尝试检索“instance_id”的值,但是出现以下错误;

- 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"}

我的嵌套变量检索语法哪里出错了?

谢谢。

看看下面的代码是否有效

  - 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 }}"

只需修改您的任务:

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

我假设实例是一个只有一条记录的数组...

如果数组中有更多记录,您可以使用 map:

- name: find all instances
  debug:
      msg: 
        - "==========================="
        - "List of instances id"
        - "==========================="
        - '{{ server_info.instances | map(attribute="instance_id")|list}}'
        - ""