Iteration of nested hash in hiera for use in ec2_instances for block_devices

asked 2017-07-17 11:19:16 -0500

Trying to figure out how to loop through and create ec2instance from hiera using .each. Basically how can I add the array of blockdevices correctly.

Error: Parameter blockdevices failed on Ec2instance[eu-qa-web-01]: block device must be named at awseu/modules/eumgmt/manifests/controlinstances.pp:67

eumgmt::controlinstances::instances:

eu-qa-web-01:

ensure: 'present'
region: 'eu-west-1'
image_id: 'ami-0d063c6b'
instance_type: 't2.2xlarge'
security_groups: 'Legacy'
subnet: 'Legacy-NonProd'
key_name: 'VPC'
user_data: 'eu_mgmt/user-data.erb'
block_devices:
  - dev1:
      device_name: '/dev/sda1'
      volume_size: 300
      delete_on_termination: true
      volume_type: 'gp2'

eu-qa-db-01:

ensure: 'present'
region: 'eu-west-1'
image_id: 'ami-0d063c6b'
instance_type: 'm4.2xlarge'
security_groups: 'Legacy'
subnet: 'Legacy-NonProd'
key_name: 'VPC'
user_data: 'eu_mgmt/user-data.erb'
block_devices:
  - dev1:
      device_name: '/dev/sda1'
      volume_size: 600
      delete_on_termination: true
      volume_type: 'gp2'

eu-qa-db-02:

ensure: 'present'
region: 'eu-west-1'
image_id: 'ami-0d063c6b'
instance_type: 'm4.2xlarge'
security_groups: 'Legacy'
subnet: 'Legacy-NonProd'
key_name: 'VPC'
user_data: 'eu_mgmt/user-data.erb'
block_devices:
  - dev1:
      device_name: '/dev/sda1'
      volume_size: 300
      delete_on_termination: true
      volume_type: 'gp2'
  - dev2:
      device_name: '/dev/sdb'
      volume_size: 600
      delete_on_termination: true
      volume_type: 'gp2'

class eumgmt::controlinstances ( Hash $instances = {} ) {

$content = inline_template(" <%- require 'json' -%> <%= JSON.pretty_generate(@instances) %> ")
notice($instances)
notice($content)

$instances.each |String $key, Hash $value| {
  notice($key)
  notice($value)
  $value['block_devices'].each |$blk_value| {
  notice($blk_value)
   }
 }

$instances.each |String $key, Hash $instance_hash| {
# $instance_name is ec2 instance name
# $instance_hash is { instance_name =>  { ensure =>  value, region => value, image_id,etc
  $blk_devices = pick($instance_hash['block_devices'])
  ec2_instance{$key:
    ensure          => $instance_hash['ensure'],
    region          => $instance_hash['region'],
    image_id        => $instance_hash['image_id'],
    instance_type   => $instance_hash['instance_type'],
    security_groups => $instance_hash['security_groups'],
    subnet          => $instance_hash['subnet'],
    key_name        => $instance_hash['key_name'],
    user_data       => template($instance_hash['user_data']),
    block_devices   => $instance_hash['block_devices'][0],
    }
  } 
}
edit retag flag offensive close merge delete