Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mount all ephemeral drive using ansible

I wan to spin up ec2 instance using ansible and then mount all available ephemeral drive to mount point.

I checked ec2 module but there isn't a way to activate all available ephemeral drive and then mount these drive.

If i want to do this then I need to write task specific to instance type like following.

- ec2:
key_name: mykey
group: webserver
instance_type: c3.xlarge
image: ami-123456
wait: yes
wait_timeout: 500
volumes:
  - device_name: /dev/xvda
    volume_type: standard
    volume_size: 100
  - device_name: /dev/xvdb
    volume_type: ephemeral
    volume_size: 40
  - device_name: /dev/xvd
    volume_type: ephemeral
    volume_size: 40
vpc_subnet_id: subnet-29e63245
assign_public_ip: yes
exact_count: 1

Is there any way to parameterize following part

volumes:
  - device_name: /dev/xvda
    volume_type: standard
    volume_size: 100
  - device_name: /dev/xvdb
    volume_type: ephemeral
    volume_size: 40
  - device_name: /dev/xvd
    volume_type: ephemeral
    volume_size: 40

any one know how to do this ?

Thanks

like image 904
roy Avatar asked Feb 11 '16 20:02

roy


1 Answers

My solution for that was to have a pre-defined list of the volume types per instance, usually in group_vars/all but could be anywhere else as long as it is visible to your role. Something like:

one_ephemeral:
  - { device_name: /dev/sdb, ephemeral: ephemeral0 }
two_ephemeral:
  - { device_name: /dev/sdb, ephemeral: ephemeral0 }
  - { device_name: /dev/sdc, ephemeral: ephemeral1 }
three_ephemeral:
  - { device_name: /dev/sdb, ephemeral: ephemeral0 }
  - { device_name: /dev/sdc, ephemeral: ephemeral1 }
  - { device_name: /dev/sdd, ephemeral: ephemeral2 }
four_ephemeral:
  - { device_name: /dev/sdb, ephemeral: ephemeral0 }
  - { device_name: /dev/sdc, ephemeral: ephemeral1 }
  - { device_name: /dev/sdd, ephemeral: ephemeral2 }
  - { device_name: /dev/sde, ephemeral: ephemeral3 }


ephemeral_volumes:
  c1.medium: "{{ one_ephemeral }}"
  c1.xlarge: "{{ four_ephemeral }}"
  c3.large: "{{ two_ephemeral }}"
  c3.xlarge: "{{ two_ephemeral }}"
  c3.2xlarge: "{{ two_ephemeral }}"
  c3.4xlarge: "{{ two_ephemeral }}"
  c3.8xlarge: "{{ two_ephemeral }}"
  i2.xlarge: "{{ one_ephemeral }}"
  i2.2xlarge: "{{ two_ephemeral }}"
  i2.4xlarge: "{{ four_ephemeral }}"
  m1.small: "{{ one_ephemeral }}"
  m1.medium: "{{ one_ephemeral }}"
  m1.large: "{{ two_ephemeral }}"
  m1.xlarge: "{{ four_ephemeral }}"
  m2.xlarge: "{{ one_ephemeral }}"
  m2.2xlarge: "{{ one_ephemeral }}"
  m2.4xlarge: "{{ two_ephemeral }}"
  m3.medium: "{{ one_ephemeral }}"
  m3.large: "{{ one_ephemeral }}"
  m3.xlarge: "{{ two_ephemeral }}"
  m3.2xlarge: "{{ two_ephemeral }}"
  r3.large: "{{ one_ephemeral }}"
  r3.xlarge: "{{ one_ephemeral }}"
  r3.2xlarge: "{{ one_ephemeral }}"
  r3.4xlarge: "{{ one_ephemeral }}"
  r3.8xlarge: "{{ two_ephemeral }}"

Then, when calling ec2 or ec2_lc modules I concatenate that to the list of volumes - something like:

    volumes: "{{ instance_conf.volumes + (ephemeral_volumes[instance_conf.instance_type]|default([])) }}"

Here's how it fits in the module's call:

 local_action:
    module: ec2_lc
    image_id: "{{ instance_conf.ami }}"
    instance_type: "{{ instance_conf.instance_type }}"
    instance_profile_name: "{{ instance_conf.instance_profile }}"
    key_name: "{{ instance_conf.key_name | default(omit) }}"
    name: "{{ instance_conf.name }}-lc"
    region: "{{ instance_conf.region }}"
    assign_public_ip: "{{ instance_conf.assign_public_ip | default(omit) }}"
    security_groups: "{{ instance_conf.security_groups }}"
    volumes: "{{ instance_conf.volumes + (ephemeral_volumes[instance_conf.instance_type]|default([])) }}"
    state: present
    user_data: "{{ lookup('template', instance_conf.userdata_template | default('userdata.sh.j2')) }}"
like image 113
Filipe Avatar answered Oct 09 '22 23:10

Filipe