linux – Puppet 2.7.x – 全局变量 – 追加

前端之家收集整理的这篇文章主要介绍了linux – Puppet 2.7.x – 全局变量 – 追加前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在没有深入到杂草的情况下,Nginx正在逼迫我的手,以便用vhosts和map指令完成一些魔法.

是否有一个优雅的(相对)解决方案来跨多个定义调用共享变量,这允许每个定义调用将其数据附加到全局变量?在软件中,这将被称为单身人士.

– 杂草 –

Nginx有一个map指令,它指示应该将请求传递给哪个上游服务器池,如下所示:

  1. map $http_host $upstream_pool {
  2.  
  3. hostnames;
  4.  
  5. blog.example.com blog_example_com;
  6. repo.example.com repo_example_com;
  7.  
  8. default example_com;
  9. }

如您所见,对blog.example.com的任何请求都将传递到blog_example_com上游服务器池(通过proxy_pass).

问题是map指令语法是它只能包含在主http块(Nginx.conf)中,而vhost特定指令(如上游和位置)可以包含在vhost配置的服务器块中.

我的nodes.pp清单看起来像这样:

  1. service-a-1.example.com inherits project_dev {
  2.  
  3. Nginx::vhost { 'mothership': }
  4. Nginx::vhost { 'mothership_blog': }
  5. Nginx::vhost { 'repo': }
  6. }

正如您所看到的,在成功运行木偶之后,我应该在/etc/Nginx/vhost.d/dir中使用3个不同的vhost配置文件.

我遇到的问题是,为了使map指令工作,我需要知道加载了哪些vhost,所以我可以将它们各自的上游id添加到map指令中,我在主配置中定义了:/ etc / http块中的Nginx / Nginx.conf(其中只能有一个).

– 我尝试过的 –

我有一个global.pp文件,它做了一些“bootstrapping”,在这个文件中我添加了一个$singleton =”语法,然后在Nginx :: vhost定义中,我添加了这个语法:

  1. $tpl_upstream_pool_labels = inline_template("<% tpl_upstream_pools.keys.sort.each do |role| %><%= role %>_<%= tpl_domain_primary %>_<%= tpl_domain_top_level %>|<% end %>")
  2.  
  3. $singleton = "${singleton}${tpl_upstream_pool_labels}"
  4.  
  5. notify { "\n--------------------- Nginx::Conf::Vhost::Touch | Timestamp: ${timestamp} | Pool labels: ${singleton} -------------------------\n": }

这应该导致上游ID的管道分隔列表.如前所述,在nodes.pp清单中,我对Nginx :: vhost进行了三次调用,并期望为每次调用附加$singleton全局变量,但不是,它只包含最后一次调用的数据.

我也尝试通过写一个像这样的临时文件解决这个问题:

  1. $temp_file_upstream_pool_labels_uri = "/tmp/puppet_known_upstreams_${timestamp}.txt"
  2.  
  3. exec { "event_record_known_upstream_${name}" :
  4. command => "touch ${temp_file_upstream_pool_labels_uri} && echo ${tpl_upstream_pool_labels} >> ${temp_file_upstream_pool_labels_uri}",provider => 'shell'
  5. }

然后在Nginx :: conf :: touch define中,主要配置Nginx.conf将由puppet编写,我试过这个:

  1. $temp_file_upstream_pool_labels_uri = "/tmp/puppet_known_upstreams_${timestamp}.txt"
  2.  
  3. $contents = file($temp_file_upstream_pool_labels_uri)

从理论上讲,这应该将文件内容加载到$contents变量中.但是当我使用这种方法运行puppet时,我得到一个文件不存在的错误.我确保在考虑所有vhost之后才进行Nginx :: conf :: touch调用,但仍无效.

解决方法

Puppet中全局变量的问题在于你无法实际附加到它们.允许使用=语法,但它会创建全局的本地副本,并附加右侧值.

我已经实现了一个可以满足您需求的模式,但我并不为此感到自豪,也不建议使用它.不过,既然你问,那么:

  1. class Nginx::conf {
  2. $global = ''
  3. file { '/etc/Nginx/Nginx.conf':
  4. content => template(...)
  5. }
  6. }

可以附加类中的变量,至少在2.7.x中.

  1. define Nginx::vhost(...) {
  2. include Nginx::conf
  3.  
  4. $Nginx::conf::global += "new content here"
  5. Nginx::override_conf_content { "for-vhost-$name": }
  6. }

魔法发生在神秘的最后一行,它实例化另一个定义.

  1. define Nginx::override_conf_content() {
  2. include Nginx::conf
  3. $global = $Nginx::conf::global
  4. File<| title == '/etc/Nginx/Nginx.conf' |> {
  5. content => template(...)
  6. }
  7. }

Nginx.conf的content属性将被另一个模板评估结果覆盖,并带有新变量值.

完全披露,我不知道为什么这样做.它可能不应该,并且可能依赖于一个不起眼的bug.它可能会在将来的版本中停止工作.

请注意,我称之为$global而不是$singleton,因为它就是这样.单例可用于实现全局变量的语义,但它们不是同一个东西.

最后,即使我能感受到Puppet 3更新所面临的痛苦,你也应该花时间让它继续下去.我们都可能无法负担更长时间运行2.x.

猜你在找的Linux相关文章