AWS : determine EBS volume physical name in order to format it

Here’s my situation : I’m working on different type of EC2 that will always have extra disks attached that I need to format and mount.
Depending on the EC2 architecture, disk will appear as xdX or nvmeX (X is not even ordered)
Let’s say I have an extra volume dedicated to DATA and identified as “vol-02a758d5dd2b3f387” by AWS.
Ansible fact shows this way :

"ansible_device_links": {
            "ids": {
                "nvme3n1": [
                    "nvme-Amazon_Elastic_Block_Store_vol02a758d5dd2b3f387",
                    "nvme-nvme.1d0f-766f6c3032613735386435646432623366333837-416d617a6f6e20456c617374696320426c6f636b2053746f7265-00000001"
                ]

Great, but the info I want is “nvme3n1”, how can I determine this ID only with the AWS EBS volume ID ?

Have you tried amazon.aws.ec2_vol_info module – Gather information about EC2 volumes in AWS — Ansible Documentation? Looks like in the examples that it can gather info when you provide a volume-id

1 Like

Yes, I did and it does give a lot a info but not the one I want.
The craziest is when you attache a EBS volume to a EC2, you can supply a “device” value such as “/dev/sdX” but on the EC2 it will never show with this id, it arrvies as “/dev/nvmeXn1” → This is the value I want in order to format it.

I can retrieve this “device” information through the anazon.aws.ec2_vol_info module :

"attachment_set": [
                    {
                        "attach_time": "2023-11-21T16:08:01+00:00",
                        "delete_on_termination": false,
                        "device": "/dev/sdf",
                        "instance_id": "i-XXXXXXXXXXXX",
                        "status": "attached"
                    }

But it’s useless for me because it does not appear this way on the system…

1 Like

Do you actually need that information, or do you just need a path you can use to format and mount it?

Either way, /dev/disk/by-id is probably the easiest way to go.

- hosts: localhost
  vars:
    ebs_id: vol0f4f7e914b7f7f53c
  tasks:
    - name: Format with XFS
      filesystem:
        dev: /dev/disk/by-id/nvme-Amazon_Elastic_Block_Store_{{ ebs_id }}
        fstype: xfs
        force: false

    - name: Mount on /home
      ansible.posix.mount:
        name: /home
        src: /dev/disk/by-id/nvme-Amazon_Elastic_Block_Store_{{ ebs_id }}
        fstype: xfs
        state: mounted

    - name: Figure out the pretty name for the volume
      ansible.builtin.stat:
        path: /dev/disk/by-id/nvme-Amazon_Elastic_Block_Store_{{ ebs_id }}
      register: result

    - debug:
        msg: "{{ ebs_id }} is {{ result.stat.lnk_source }}"
PLAY [localhost] ***************************************************************

TASK [Format with XFS] *********************************************************
ok: [localhost]

TASK [Mount on /home] **********************************************************
ok: [localhost]

TASK [Figure out the pretty name for the volume] *******************************
ok: [localhost]

TASK [debug] *******************************************************************
ok: [localhost] =>
    msg: vol0f4f7e914b7f7f53c is /dev/nvme2n1

You can also install amazon-ec2-utils if you want the device names specified on attachment to actually be created.

2 Likes

thanks so much, this is exactly what I wanted to do and I did not look on this side !!!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.