Skip to content

Instantly share code, notes, and snippets.

@nitanka
Last active January 12, 2023 16:40
Show Gist options
  • Save nitanka/7843fc1b72aba0faf8260aecf735221f to your computer and use it in GitHub Desktop.
Save nitanka/7843fc1b72aba0faf8260aecf735221f to your computer and use it in GitHub Desktop.
Ansible: Adding EBS volume to an EC2 instance
---
- name: Creating a Volume
hosts: localhost
become: yes
tasks:
- name: Creating a Volume
ec2_vol:
aws_access_key: "{{ lookup('env', 'AWS_ACCESS_KEY_ID') }}"
aws_secret_key: "{{ lookup('env', 'AWS_SECRET_ACCESS_KEY') }}"
instance: '<instance-id>'
volume_size: <volume-size>
device_name: <device-name> #/dev/xvdf, /dev/xvdb
region: '<region-name>'
volume_type: <volume-type> #example gp2
register: ec2_vol
- name: Printing the volume information
debug: var=ec2_vol
- name: creating mount point (directory)
file:
state: directory
path: "< path >"
- name: formatting the volume
filesystem:
dev: "< MOUNT_VOLUME >"
fstype: "< VOLUME_FORMAT >" #ext4, ext3 etc
#the device mount depends on the system
- name: mounting the filesystem
mount:
name: "< path to mount point >"
src: "< MOUNT_VOLUME >"
fstype: "< VOLUME_FORMAT >" #ext4, ext3 etc
state: mounted
@amythtech
Copy link

I tried this but i get this error : fatal: [localhost]: FAILED! => {"changed": false, "msg": "Device /dev/xvdf not found.
Does this mounting works. Volume is created as expected but formatting/ mounting fails

@nitanka
Copy link
Author

nitanka commented Mar 3, 2020

Did you check, whether the disk name is same as /dev/xvdf?

@amythtech
Copy link

I got the issue.. actually the file, filesystem and mount module need to run on ec2 instances. So i passed ec2 instances by adding them to dynamic host inventory.
How did you manage to format mount the filesystem on ec2 instances?

@nitanka
Copy link
Author

nitanka commented Mar 4, 2020

on the above code, we can see in creating volume task, we are passing the device name, I am using the same device name in formatting the volume task

@amythtech
Copy link

I think you are running the ansible in ec2 instance. I was running on my local machine.

@nitanka
Copy link
Author

nitanka commented Mar 5, 2020

ya, the above ansible script is for managing ec2 instances.

@ThomasG77
Copy link

ThomasG77 commented Jun 24, 2021

I don't get any /dev/xvdf but /dev/nvme1n1 within ec2 instance when running lsblk (related to https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/device_naming.html#available-ec2-device-names and https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-using-volumes.html). To deduce it, I use something like below using facts from Ansible and matching volume id to device

   - name: Collect only facts about hardware
      setup:
        gather_subset:
        - hardware

    - name: Output dev name
      debug:
        var: "{{ hostvars[inventory_hostname].ansible_devices | dict2items | community.general.json_query(jmesquery) | first}}"
      vars:
        jmesquery: "[?length(value.links.ids)>`0` && contains(map(&contains(@, `{{ ec2_vol.volume_id.replace('vol-', '') }}`), value.links.ids), `true`)].key"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment