使用 ec2_instance_info 模块 Ansible 标记 AWS EBS 卷以提取信息

Ansible tagging AWS EBS volumes using ec2_instance_info module to pull info


我是 运行 ansible 2.9 我正在尝试使用 "ec2_instance_info" 模块提取 ec2 信息。

最终,我需要使用 instance_id 和 EBS device_name.

标记实例附加的 EBS 卷

这些是我正在处理的任务:

   - name: Gather ec2_metadata_facts
     action: ec2_metadata_facts

   - name: pull instance info with ec2_instance_info
     ec2_instance_info:
       region: "{{ lookup('env','AWS_DEFAULT_REGION') }}"
       aws_access_key: "{{ lookup('env','AWS_ACCESS_KEY_ID') }}"
       aws_secret_key: "{{ lookup('env','AWS_SECRET_ACCESS_KEY') }}"
       instance_ids: "{{ ansible_ec2_instance_id }}"
     register: ec2_node_info

#prints Too much info, but useful for debugging
#   - name: print ALL info from ec2_instance_info
#     debug:
#       msg: "dict: {{ec2_node_info}} "

   - name: print Flattened dev_name and vol_ID
     debug:
       msg: 
         - "{{ ec2_node_info | json_query(dev_name)|flatten(levels=1) }}"
         - "{{ ec2_node_info | json_query(vol_id)|flatten(levels=1)}}"
     vars:
       vol_id: "instances[*].block_device_mappings[*].ebs.volume_id"
       dev_name: "instances[*].block_device_mappings[*].device_name"

这是输出:

TASK [Gather ec2_metadata_facts (use -vv to show all)] ********************************************************
ok: [prd-node-01]

TASK [pull instance info with ec2_instance_info] ********************************************************
ok: [prd-node-01]

TASK [print Flattened dev_name and vol_ID] *********************************************************
ok: [prd-node-01] => 
  msg:
  - - /dev/sdf
    - /dev/sdg
    - /dev/sdh
    - /dev/sdi
    - /dev/sda1
  - - vol-0758995d43a43aa04
    - vol-0037fd24cc8229551
    - vol-0ab0ae909b39f2b32
    - vol-0987e6f6af374ec20
    - vol-0ebf1d896c94b1fbf

我有我需要的数据。但是,我不知道如何访问各个元素。 json_query 对我来说是新的。我最初是在尝试这样的事情:

   - name: Print Volume elements
     debug:
       msg: "{{item.device_name}} {{item.volume_id}}"
     loop: "{{ ec2_node_info.instances[0].block_device_mapping }}"

但这给出了:

TASK [Print Volume elements] ********************************************************
fatal: [prd-node-01]: FAILED! => 
  msg: '''dict object'' has no attribute ''block_device_mapping'''

我的许多节点都有 5 个卷。有的只有一个,有的有多个。

我需要能够参考:
instance_id:{{volume_id[1]}} {{dev_name[1]}}
instance_id:{{volume_id[2]}} {{dev_name[2]}}
instance_id: {{volume_id[?]}} {{dev_name[?]}}

等...
任何帮助表示赞赏。

像往常一样,

json_query() 是不必要的并发症。使用普通的 Jinja,使用 subelements 过滤器:

    - debug:
        msg: "{{ item.0.instance_id }} / {{ item.1.ebs.volume_id }} / {{ item.1.device_name }}"
      loop: "{{ ec2_node_info.instances | subelements('block_device_mappings') }}"
      loop_control:
        label: "{{ item.0.instance_id }} / {{ item.1.device_name }}"
TASK [debug] *******************************************************************
ok: [localhost] => (item=i-0fe0b60f708f8adf7 / /dev/xvda) => {
    "msg": "i-0fe0b60f708f8adf7 / vol-0a454b7f9f3d17dda / /dev/xvda"
}
ok: [localhost] => (item=i-0fe0b60f708f8adf7 / xvdd) => {
    "msg": "i-0fe0b60f708f8adf7 / vol-0f4f7e914b7f7f53c / xvdd"
}
ok: [localhost] => (item=i-0fe0b60f708f8adf7 / /dev/sdf) => {
    "msg": "i-0fe0b60f708f8adf7 / vol-0ef1bf1ddc3abe6ff / /dev/sdf"
}
ok: [localhost] => (item=i-052df13a8406e870d / /dev/xvda) => {
    "msg": "i-052df13a8406e870d / vol-0a0132a2ee3a087ce / /dev/xvda"
}
ok: [localhost] => (item=i-052df13a8406e870d / xvdd) => {
    "msg": "i-052df13a8406e870d / vol-0870514b73c8f7166 / xvdd"
}
ok: [localhost] => (item=i-052df13a8406e870d / xvde) => {
    "msg": "i-052df13a8406e870d / vol-062b98fc235ef094e / xvde"
}
ok: [localhost] => (item=i-052df13a8406e870d / xvdf) => {
    "msg": "i-052df13a8406e870d / vol-0db0eaa45ddfa3df4 / xvdf"