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 :
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 :
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.