【问题标题】:How to I access JSON value from ansible output?如何从 ansible 输出访问 JSON 值?
【发布时间】:2021-05-02 01:10:48
【问题描述】:

我有这个 ansible playbook,它将使用 ec2_vol 模块创建卷并将其附加到 EC2 实例,我想使用 parted 模块对其进行分区。下面是我的 ec2_vol 模块,

EC2_VOL 模块:

      ec2_vol:
        aws_access_key: XXXXXXXX
        aws_secret_key: XXXXXXXX
        security_token: XXXXXXXX
        instance: "{{ item.instance_id }}"
        region: "{{ var_dict['REGION'] }}"
        device_name: "{{ VOL_NAME }}"
        volume_type: gp2 
        volume_size: '1000'
        delete_on_termination: false
      with_items: "{{ ec2_instances_list.instances}}"
      register: ec2_volumes    
    - debug:
        msg: "{{ ec2_volumes.results }}"

ec2_volumes 的结果:

以及从ec2_volumes.results 打印的结果。我想遍历 device_name 并对创建的设备进行分区

TASK [debug] *******************************************************************
ok: [localhost] => {
    "msg": [
        {
            "_ansible_ignore_errors": null, 
            "_ansible_item_label": {
                "ami_launch_index": 0, 
                "architecture": "x86_64", 
                "block_device_mappings": [
                    {
                        "device_name": "/dev/sda1", 
                        "ebs": {
                            "attach_time": "2021-04-30T15:47:07+00:00", 
                            "delete_on_termination": true, 
                            "status": "attached", 
                            "volume_id": "vol-XXXXXXXXXXXXXX"
                        }
                    }, 
                    {
                        "device_name": "/dev/sdb", 
                        "ebs": {
                            "attach_time": "2021-04-30T15:47:07+00:00", 
                            "delete_on_termination": false, 
                            "status": "attached", 
                            "volume_id": "vol-XXXXXXXXXXXXXXXX"
                        }
                    }
                ], 

这是我分开的模块代码:

    - name: Partition Additional volumes 
      parted:
        device: "{{ item.block_device_mappings[0].device_name }}"
        number: 1
        part_type: 'primary'
        state: present
        when: ("{{ ec2_volumes.device_name }}" != "/dev/sda1")
      with_items: "{{ ec2_volumes.results}}"

但仍然出现“block_device_mappings”没有属性的错误。

fatal: [localhost]: FAILED! => {"msg": "'list object' has no attribute 'block_device_mappings'"}

可能是什么原因?有人可以帮我解决这个问题吗?

编辑 1:

更新 parted 模块中的 playbook 后遇到另一个问题

    - name: Partition Additional volumes 
      parted:
        device: "{{ item._ansible_item_label.block_device_mappings[0].device_name }}"
        number: 1
        part_type: 'primary'
        state: present
        when: ("{{ ec2_volumes.device_name }}" != "/dev/sda1")
      with_items: "{{ ec2_volumes.results}}"
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'device_name'

【问题讨论】:

  • 请检查您的示例以使其成为mcve,因为您提供的数据与您正在使用的变量不匹配,也与您得到的错误不匹配。 (即,您的数据中没有results,无论是作为列表还是作为字典)。
  • @Zeitounator,你现在可以检查一下吗?我实际上是在调试 ec2_vol 中注册的结果
  • 您没有粘贴完整的可用数据输出,并且从可用的数据中,我在您生成的循环Item 中看不到任何可用的block_device_mapping....尽管item._ansible_item_label.block_device_mapping 存在。 ..但基本上是您上一个任务中上一个循环中的项目...
  • 在 ec2_vol 的输出中搜索 block_device_mappings。你会明白的。仅供参考,所有任务都在同一个剧本中。我只是分享了两者之间的输出以供您理解。
  • 完整的可用数据输出由 1052 行代码组成。我不能在这里发布,所以我在下一个任务中发布了我需要做的事情。如果您能提供帮助,请告诉我。

标签: json ansible nested-lists


【解决方案1】:

错误很明显:

失败了! => {"msg": "'list object' 没有属性 'block_device_mappings'"}

当您查看列表时,您会发现 block_device_mappings_ansible_item_label 的一个属性

    "msg": [
        {
            "_ansible_ignore_errors": null, 
            "_ansible_item_label": {
                "ami_launch_index": 0, 
                "architecture": "x86_64", 
                "block_device_mappings": [
                ...

修正参考


    device: "{{ item._ansible_item_label.block_device_mappings[0].device_name }}"

【讨论】:

  • 谢谢@Vladimir,它似乎工作了..但现在出现其他错误..fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'device_name'
  • 奇怪。 block_device_mappings.0block_device_mappings.1 都得到了 device_name。找出这个错误来自哪里。 edit 你的问题并提出minimal reproducible example
  • 复查ec2_volumes.device_name 存在。
猜你喜欢
  • 1970-01-01
  • 2019-03-11
  • 2016-03-11
  • 1970-01-01
  • 2023-03-25
  • 2020-04-19
  • 2018-11-20
  • 2022-11-11
  • 2017-02-03
相关资源
最近更新 更多