Home > Software engineering >  Ansible tagging AWS EBS volumes using ec2_instance_info module to pull info
Ansible tagging AWS EBS volumes using ec2_instance_info module to pull info

Time:11-20


I'm running ansible 2.9 I am trying pull ec2 info with the "ec2_instance_info" module.

Ultimately, I need to tag instance-attached EBS volumes with the instance_id, and EBS device_name.

These are the tasks that I have working:

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

This is the output:

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

I have the data I need. But, I can't figure out how to access the individual elements. json_query is new to me. I was originally trying something like this:

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

But that gives:

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

many of my nodes have 5 volumes. Some have only one, some have more.

I need to be able to reference:
instance_id: {{volume_id[1]}} {{dev_name[1]}}
instance_id: {{volume_id[2]}} {{dev_name[2]}}
instance_id: {{volume_id[?]}} {{dev_name[?]}}

etc...
Any help appreciated.

CodePudding user response:

json_query() is, as usual, an unnecessary complication. Use plain Jinja instead, with the subelements filter:

    - 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"
  • Related