Ask Your Question
0

how can I access to resource name from a template?

asked 2015-09-23 12:06:38 -0500

vbarba gravatar image

I have this class declared:

class imageservers {
ec2_instance { 'vbarba-imageserver-01':
        ensure                  => 'running',
        availability_zone       => 'eu-west-1a',
        block_devices           => [{
                'delete_on_termination' => 'true',
                'device_name' => '/dev/sda1',
                'volume_size' => 30}],
        ebs_optimized           => 'true',
        #Centos7
        image_id                => 'ami-e4ff5c93',
        instance_type           => 'm4.large',
        key_name                => 'gergergreg',
        region                  => 'eu-west-1',
        security_groups         => ['sg_imageserver'],
        subnet                  => 'gergerg-subnet',
        associate_public_ip_address => 'true',
        user_data               => template('groupalia-imageservers/user-data.sh.erb'),
}
}

How can I access to ec2_instance resource attributes from user-data.sh.erb template?

Thank you very much in advance!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2015-09-23 14:11:55 -0500

GregLarkin gravatar image

updated 2015-09-23 18:35:04 -0500

You can do that by assigning the static values in your resource declaration to variables like so:

$region = 'eu-west-1'

# Now in the resource, replace the static value with the variable:
# e.g. region => $region

# Then use the following idiom in your ERB template when you want to insert
# the value of the attribute
# <%= @region -%>

Hope that helps!

UPDATE:

If you want to use two different resource declarations with the same template (and the same variables in that template), you should wrap your resource in a defined type, and then declare the defined type in your class. The defined type will have nearly the same parameter list as the actual resource it wraps, or at least every value that needs to be dynamic. Your code would then look like this:

# Filename: <modulename>/manifests/ec2_instance_wrapper.pp
define ec2_instance_wrapper(
  # Any of these variables plus $title can be referenced in the template ERB file
  $zone,
  $block_devices,
  $image_id,
  $instance_type,
  $key_name,
  $region,
  $security_groups,
  $subnet,
) {
  ec2_instance { $title:
    ensure                  => 'running',
    availability_zone       => $zone,
    block_devices           => $block_devices,
    ebs_optimized           => 'true',
    #Centos7
    image_id                => $image_id',
    instance_type           => $instance_type',
    key_name                => $key_name,
    region                  => $region',
    security_groups         => $security_groups,
    subnet                  => $subnet,
    associate_public_ip_address => 'true',
    user_data               => template('groupalia-imageservers/user-data.sh.erb'),
  }
}

# Filename: <modulename>/manifests/imageservers.pp
class imageservers {
  ec2_instance_wrapper { 'vbarba-imageserver-01':
    zone          => 'eu-west-1a',
    block_devices => [{
      'delete_on_termination' => 'true',
      'device_name' => '/dev/sda1',
      'volume_size' => 30}],
    image_id                => 'ami-e4ff5c93',
    instance_type           => 'm4.large',
    key_name                => 'gergergreg',
    region                  => 'eu-west-1',
    security_groups         => ['sg_imageserver'],
    subnet                  => 'gergerg-subnet',
  }

  ec2_instance_wrapper { 'vbarba-imageserver-02':
    zone          => 'eu-west-2a',
    block_devices => [{
      'delete_on_termination' => 'true',
      'device_name' => '/dev/sda1',
      'volume_size' => 30}],
    image_id                => 'ami-e4ff5c93',
    instance_type           => 'm4.large',
    key_name                => 'blahblah',
    region                  => 'eu-west-2',
    security_groups         => ['sg_imageserver'],
    subnet                  => 'blahblah-subnet',
  }
}

As an exercise for the reader, you could even move all of those parameters into Hiera and dynamically manage your ec2 instances by only modifying Hiera data files.

edit flag offensive delete link more

Comments

Thank you for your help, but you can't declare variables inside a resource, aren't you? what about if you have 2 resources, both with same template but different variables?

vbarba gravatar imagevbarba ( 2015-09-23 16:49:35 -0500 )edit

In that situation, you would wrap the resource in a defined type. I will update my answer to show that.

GregLarkin gravatar imageGregLarkin ( 2015-09-23 18:23:00 -0500 )edit

Thank you very much for your answer. In case of using hiera, is it still necessary to use the wrapper?

vbarba gravatar imagevbarba ( 2015-09-24 01:24:26 -0500 )edit

Yes, you'll still need the defined type in order for the variables referenced in your ERB template to be visible. Once you have that, you can either call hiera() in your class and declare ec2_instance_wrapper multiple times, or you could use the create_resources() function.

GregLarkin gravatar imageGregLarkin ( 2015-09-24 11:33:25 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-09-23 12:06:38 -0500

Seen: 288 times

Last updated: Sep 23 '15