简体   繁体   English

如何使用基于puppet的引导在EC2实例中设置主机名

[英]How to set hostname in EC2 instance with puppet based bootstrapping

I'm trying to build a puppet managed infrastructure (non-enterprise) in AWS with EC2 instances. 我正在尝试使用EC2实例在AWS中构建一个木偶管理基础架构(非企业)。 Using puppetlabs-aws module I'm able to create the machines by convenient means. 使用puppetlabs-aws模块我可以通过方便的方式创建机器。 Next up is to make local settings on each node, most importantly setting a unique hostname. 接下来是在每个节点上进行本地设置,最重要的是设置唯一的主机名。 How can I do this? 我怎样才能做到这一点?

One way I know of is to provide a script via the user_data parameter. 我知道的一种方法是通过user_data参数提供脚本。 That would be great, but to be usable I need to be able to parameterize that script in order to avoid duplicating the script once for each agent. 这将是很好的,但为了可用,我需要能够参数化该脚本,以避免为每个代理重复一次脚本。

Does it make sense? 是否有意义? I'd really appreciate a convenient way of achieving this, as I want to launch new instances programmatically. 我真的很感激一种方便的方法来实现这一点,因为我想以编程方式启动新实例。 Any suggestion will be considered. 任何建议都将被考虑。

Update 更新

To give an example of my problem, consider this snippet of my provisioning puppet manifest: 举一个我的问题的例子,考虑我的配置木偶清单的这个片段:

ec2_instance { 'backend':
  ensure => present,
  name => 'backend',
  region => 'us-west-2',
  image_id => 'ami-f0091d91',
  instance_type => 't2.micro',
  key_name => 'mykey',
  security_groups => ['provision-sg'],
  user_data => template('configure.erb'),
}

ec2_instance { 'webfront':
  ensure => present,
  name => 'webfront',
  region => 'us-west-2',
  image_id => 'ami-f0091d91',
  instance_type => 't2.micro',
  key_name => 'mykey',
  security_groups => ['provision-sg'],
  user_data => template('configure.erb'),
}

This will ensure the two instances are up and running. 这将确保两个实例启动并运行。 Please notice the user_data => template('configure.erb') referring to a template script which is executed on the instance once it is created. 请注意user_data => template('configure.erb')引用模板脚本,该脚本在创建后在实例上执行。 Here I would be able to set the hostname (or whatever I wanted to) if I only knew what data to base the decision on. 如果我只知道决定依据的数据,我可以在这里设置主机名(或我想要的任何东西)。 I can add tags to the instance descriptions, but that is not readable from the configure.erb script as far at I know. 我可以在实例描述中添加标签,但据我所知,这是从configure.erb脚本中无法读取的。

Anyway, setting the hostname is just my idea of solving the root problem. 无论如何,设置主机名只是我解决根本问题的想法。 There might be other more convenient methods. 可能还有其他更方便的方法。 What I want is simply a way of having these two instances representing different node types to the puppet master. 我想要的只是让这两个实例代表木偶大师的不同节点类型的方法。

The problem is how to set up a new instance with so that it will load it's config from a particular class 问题是如何设置一个新实例,以便从特定类加载它的配置

Let me try and explain the problem I think you are trying to address 让我试着解释一下我认为你想解决的问题

What I am trying to answer here 我想在这里回答什么

You have an existing script that sets up EC2 virtual hosts on AWS using the aws-puppet module. 您有一个现有脚本,使用aws-puppet模块在AWS上设置EC2虚拟主机。 This module calls AWS API to actually make EC2 virtual hosts. 此模块调用AWS API以实际生成EC2虚拟主机。 But they only contain configuration that is "built in" to the AMI file that is used in the API call. 但它们仅包含“内置”到API调用中使用的AMI文件的配置。 A typical AMI file might be a Centos base image. 典型的AMI文件可能是Centos基本映像。 Further configuration is possible at this phase via a "user data script". 在该阶段可以通过“用户数据脚本”进一步配置。 But let's assume this a shell script, difficult to test and maintain and so not containing complex setup 但是我们假设这是一个shell脚本,难以测试和维护,因此不包含复杂的设置

So further configuration, install of packages and setup is needed. 因此需要进一步配置,安装包和设置。 In order to make this setup happen, there is a second phase of activity from puppet, using entirely different manifests (that are not detailed in the question) 为了使这个设置发生,有一个来自木偶的第二阶段活动,使用完全不同的清单(问题中没有详细说明)

This second phase is controlled by the new EC2 virtual hosts attaching to the puppet master in their own right. 第二阶段由连接到木偶主人的新EC2虚拟主机控制。 So what I am assuming you are doing is: 所以我假设你正在做的是:

  • phase 1, making EC2 hosts 第1阶段,制作EC2主机
  • phase 2, when they are up config themselves from puppet 阶段2,当他们从木偶配置自己

Basic Answer using roles 使用角色的基本答案

Here some ideas of how to make this scenario with two phase configuration of the EC2 hosts work 这里有一些关于如何使用EC2主机的两阶段配置来实现这种情况的想法

At create time make a custom fact "role". 在创建时创建自定义事实“角色”。 Make a file in /etc/facter/facts.d/role.yaml like this 像这样在/etc/facter/facts.d/role.yaml中创建一个文件

role: webserver

This can be setup as the instance is made by adding a command like this to a User Data script 这可以通过向用户数据脚本添加这样的命令来设置实例

echo 'role: webserver' > /etc/facter/facts.d/role.yaml

As long as this "role" is setup before puppet starts up it will work fine. 只要在木偶启动之前设置这个“角色”,它就能正常工作。

I am assuming that you have a set of modules with manifests and maybe files subdirectories in the module path with the same name as the role 我假设您有一组模块,其中包含清单,可能是模块路径中的文件子目录,其名称与角色相同

Next, alter your site.pp to say something like 接下来,改变你的site.pp来说出类似的话

include "$role"

And the init.pp from the module will kick in and do the right thing, install packages, configure files etc! 并且模块中的init.pp将启动并执行正确的操作,安装包,配置文件等!

This idea is explained in more detail here https://puppetlabs.com/presentations/designing-puppet-rolesprofiles-pattern 这个想法在这里有更详细的解释https://puppetlabs.com/presentations/designing-puppet-rolesprofiles-pattern


Another Approach 另一种方法

The above is a really crude way of doing it which I haven't tested! 以上是一种非常粗暴的做法,我还没有测试过! Our setup has roles but loads them via hiera configuration. 我们的设置有角色,但通过hiera配置加载它们。 The heira configuration looks somewhat like this heira配置看起来有点像这样

---
:backends:
  - yaml
:hierarchy:
    - role/%{::role}
    - global
:yaml:
  :datadir: /etc/puppet/environments/production/hiera

Then I might have a /etc/puppet/environments/production/hiera/role/webserver.yaml file which says 然后我可能有/etc/puppet/environments/production/hiera/role/webserver.yaml文件说

classes:
  - webserver
  - yum_repos
  - logstash
  - java8

And the end of the site.pp says 而site.pp的结尾说

hiera_include('classes')

Which loads all the relevant "classes" definitions from the modules_include files 它从modules_include文件加载所有相关的“类”定义

This has the advantage that multiple classes can be loaded by each role with much less duplication of code 这样做的优点是每个角色可以加载多个类,而代码重复更少

The "global" part of the yaml configuration is intended for classes that are loaded by everything in your environment, for example admin user ssh keys yaml配置的“全局”部分适用于环境中所有内容加载的类,例如admin用户ssh密钥


defined type example 定义的类型示例

Here is an example of how you might use a defined type as a wrapper around ec2_instance to pass the "myrole" into the template. 下面是一个示例,说明如何使用已定义的类型作为ec2_instance的包装器将“myrole”传递到模板中。 I have not tested this, I don't have the aws puppet stuff installed 我没有测试过这个,我没有安装aws木偶的东西

define my_instance( 
  $ensure = present,
  $region = 'us-west-2',
  $image_id = 'ami-f0091d91',
  $instance_type = 't2.micro',
  $key_name= 'mykey',
  $security_groups = ['provision-sg'],
  $myrole = 'webserver'
  )
{
ec2_instance { $title :
  ensure => $ensure,
  name => $title,
  region => $region,
  image_id => $image_id,
  instance_type => $instance_type,
  key_name => $key,
  security_groups => $security_groups,
  user_data => template('configure.erb'),
}
}

$instance_data={
  'backend' =>
  {
  ensure => present,
  name => 'backend',
  region => 'us-west-2',
  image_id => 'ami-f0091d91',
  instance_type => 't2.micro',
  key_name => 'mykey',
  security_groups => ['provision-sg'],
  myrole => 'voodooswamp'
},
  'webfront'=>
  {
  ensure => present,
  region => 'us-west-2',
  image_id => 'ami-f0091d91',
  instance_type => 't2.micro',
  key_name => 'mykey',
  security_groups => ['provision-sg'],
  myrole => 'humanfly'
  }
}


create_resources(my_instance, $instance_data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM