How to detect AWS instance type

I have an old playbook that will add an addition volume and it works fine on t2 instances. With the move to t3 instances we also now have nvme based ebs.

The play book uses lvm to setup the second volume we add the volume as xvdf so /dev/xvdf.
Now on t3 instances when we add the volume as xvdf the volume now shows up as /dev/nvme1n1.

Has anyone figured out a playbook to handle both situations?

Hi,

Depending what you’re after, something like this to figure out the instance type:

  • name: determine if we’re on a nitro hypervisor
    set_fact:
    on_nitro: “{{ ansible_ec2_instance_type.startswith(‘t3’) or ansible_ec2_instance_type.startswith(‘c5’) or ansible_ec2_instance_type.startswith(‘m5’) or ansible_ec2_instance_type.startswith(‘r5’) }}”

and then use a condition like:

when: on_nitro

to do the nvme-specific initialisation.

If you want to extract the actual volume id the way to go is to use something like this:
VOL=$(nvme id-ctrl {{ disk.path }} | grep sn | awk ‘{print $3}’); echo “${VOL/vol/vol-}”

where disk path is something like /dev/nvme1n1, that gives you volume id. If you want the original letters you might have to parse it out of:

nvme id-ctrl /dev/nvme1n1 -v

more details here: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/nvme-ebs-volumes.html

kind regards
Pshem