如何使用ansible挂载所有短暂的驱动器

时间:2016-02-11 20:17:47

标签: amazon-ec2 automation ansible ansible-playbook

我想使用ansible启动ec2实例,然后将所有可用的临时驱动器挂载到挂载点。

我检查了ec2 module但是没有办法激活所有可用的临时驱动器然后安装这些驱动器。

如果我想这样做,那么我需要编写特定于实例类型的任务,如下所示。

- 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

有没有办法参数化以下部分

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

任何人都知道如何做到这一点?

由于

2 个答案:

答案 0 :(得分:5)

我的解决方案是为每个实例提供一个预定义的卷类型列表,通常在group_vars/all中,但只要您的角色可见,就可以在其他任何位置。类似的东西:

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 }}"

然后,当调用ec2或ec2_lc模块时,我将它连接到卷列表 - 如:

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

以下是它如何适应模块的调用:

 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')) }}"

答案 1 :(得分:1)

Here is an updated ec2 instance list

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 }
six_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 }
  - { device_name: /dev/sdf, ephemeral: ephemeral4 }
  - { device_name: /dev/sdg, ephemeral: ephemeral5 }
eight_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 }
  - { device_name: /dev/sdf, ephemeral: ephemeral4 }
  - { device_name: /dev/sdg, ephemeral: ephemeral5 }
  - { device_name: /dev/sdh, ephemeral: ephemeral6 }
  - { device_name: /dev/sdi, ephemeral: ephemeral7 }
twelve_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 }
  - { device_name: /dev/sdf, ephemeral: ephemeral4 }
  - { device_name: /dev/sdg, ephemeral: ephemeral5 }
  - { device_name: /dev/sdh, ephemeral: ephemeral6 }
  - { device_name: /dev/sdi, ephemeral: ephemeral7 }
  - { device_name: /dev/sdj, ephemeral: ephemeral8 }
  - { device_name: /dev/sdk, ephemeral: ephemeral9 }
  - { device_name: /dev/sdl, ephemeral: ephemeral10 }
  - { device_name: /dev/sdm, ephemeral: ephemeral11 }
twenty_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 }
  - { device_name: /dev/sdf, ephemeral: ephemeral4 }
  - { device_name: /dev/sdg, ephemeral: ephemeral5 }
  - { device_name: /dev/sdh, ephemeral: ephemeral6 }
  - { device_name: /dev/sdi, ephemeral: ephemeral7 }
  - { device_name: /dev/sdj, ephemeral: ephemeral8 }
  - { device_name: /dev/sdk, ephemeral: ephemeral9 }
  - { device_name: /dev/sdl, ephemeral: ephemeral10 }
  - { device_name: /dev/sdm, ephemeral: ephemeral11 }
  - { device_name: /dev/sdn, ephemeral: ephemeral12 }
  - { device_name: /dev/sdo, ephemeral: ephemeral13 }
  - { device_name: /dev/sdp, ephemeral: ephemeral14 }
  - { device_name: /dev/sdq, ephemeral: ephemeral15 }
  - { device_name: /dev/sdr, ephemeral: ephemeral16 }
  - { device_name: /dev/sds, ephemeral: ephemeral17 }
  - { device_name: /dev/sdt, ephemeral: ephemeral18 }
  - { device_name: /dev/sdu, ephemeral: ephemeral19 }
  - { device_name: /dev/sdv, ephemeral: ephemeral20 }
  - { device_name: /dev/sdw, ephemeral: ephemeral21 }
  - { device_name: /dev/sdx, ephemeral: ephemeral22 }
  - { device_name: /dev/sdy, ephemeral: ephemeral23 }


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 }}"
  cc2.8xlarge: "{{ four_ephemeral }}"
  cg1.4xlarge: "{{ two_ephemeral }}"
  cr1.8xlarge: "{{ two_ephemeral }}"
  d2.xlarge: "{{ three_ephemeral }}"
  d2.2xlarge: "{{ six_ephemeral }}"
  d2.4xlarge: "{{ twelve_ephemeral }}"
  d2.8xlarge: "{{ twenty_four_ephemeral }}"
  g2.2xlarge: "{{ one_ephemeral }}"
  hi1.xlarge: "{{ two_ephemeral }}"
  hs1.xlarge: "{{ twenty_four_ephemeral }}"
  i2.xlarge: "{{ one_ephemeral }}"
  i2.2xlarge: "{{ two_ephemeral }}"
  i2.4xlarge: "{{ four_ephemeral }}"
  i2.8xlarge: "{{ eight_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 }}"
  x1.16xlarge: "{{ one_ephemeral }}"
  x1.32xlarge: "{{ two_ephemeral }}"