linux – Puppet 2.7.x – 全局变量 – 追加
在没有深入到杂草的情况下,Nginx正在逼迫我的手,以便用vhosts和map指令完成一些魔法.
是否有一个优雅的(相对)解决方案来跨多个定义调用共享变量,这允许每个定义调用将其数据附加到全局变量?在软件中,这将被称为单身人士. – 杂草 – Nginx有一个map指令,它指示应该将请求传递给哪个上游服务器池,如下所示: map $http_host $upstream_pool { hostnames; blog.example.com blog_example_com; repo.example.com repo_example_com; default example_com; } 如您所见,对blog.example.com的任何请求都将传递到blog_example_com上游服务器池(通过proxy_pass). 问题是map指令语法是它只能包含在主http块(nginx.conf)中,而vhost特定指令(如上游和位置)可以包含在vhost配置的服务器块中. 我的nodes.pp清单看起来像这样: service-a-1.example.com inherits project_dev { nginx::vhost { 'mothership': } nginx::vhost { 'mothership_blog': } nginx::vhost { 'repo': } } 正如您所看到的,在成功运行木偶之后,我应该在/etc/nginx/vhost.d/dir中使用3个不同的vhost配置文件. 我遇到的问题是,为了使map指令工作,我需要知道加载了哪些vhost,所以我可以将它们各自的上游id添加到map指令中,我在主配置中定义了:/ etc / http块中的nginx / nginx.conf(其中只能有一个). – 我尝试过的 – 我有一个global.pp文件,它做了一些“bootstrapping”,在这个文件中我添加了一个$singleton =”语法,然后在nginx :: vhost定义中,我添加了这个语法: $tpl_upstream_pool_labels = inline_template("<% tpl_upstream_pools.keys.sort.each do |role| %><%= role %>_<%= tpl_domain_primary %>_<%= tpl_domain_top_level %>|<% end %>") $singleton = "${singleton}${tpl_upstream_pool_labels}" notify { "n--------------------- Nginx::Conf::Vhost::Touch | Timestamp: ${timestamp} | Pool labels: ${singleton} -------------------------n": } 这应该导致上游ID的管道分隔列表.如前所述,在nodes.pp清单中,我对nginx :: vhost进行了三次调用,并期望为每次调用附加$singleton全局变量,但不是,它只包含最后一次调用的数据. 我也尝试通过写一个像这样的临时文件来解决这个问题: $temp_file_upstream_pool_labels_uri = "/tmp/puppet_known_upstreams_${timestamp}.txt" exec { "event_record_known_upstream_${name}" : command => "touch ${temp_file_upstream_pool_labels_uri} && echo ${tpl_upstream_pool_labels} >> ${temp_file_upstream_pool_labels_uri}",provider => 'shell' } 然后在nginx :: conf :: touch define中,主要配置nginx.conf将由puppet编写,我试过这个: $temp_file_upstream_pool_labels_uri = "/tmp/puppet_known_upstreams_${timestamp}.txt" $contents = file($temp_file_upstream_pool_labels_uri) 从理论上讲,这应该将文件的内容加载到$contents变量中.但是当我使用这种方法运行puppet时,我得到一个文件不存在的错误.我确保在考虑所有vhost之后才进行nginx :: conf :: touch调用,但仍无效. 解决方法
Puppet中全局变量的问题在于你无法实际附加到它们.允许使用=语法,但它会创建全局的本地副本,并附加右侧值.
我已经实现了一个可以满足您需求的模式,但我并不为此感到自豪,也不建议使用它.不过,既然你问,那么: class nginx::conf { $global = '' file { '/etc/nginx/nginx.conf': content => template(...) } } 可以附加类中的变量,至少在2.7.x中. define nginx::vhost(...) { include nginx::conf $nginx::conf::global += "new content here" nginx::override_conf_content { "for-vhost-$name": } } 魔法发生在神秘的最后一行,它实例化另一个定义. define nginx::override_conf_content() { include nginx::conf $global = $nginx::conf::global File<| title == '/etc/nginx/nginx.conf' |> { content => template(...) } } nginx.conf的content属性将被另一个模板评估结果覆盖,并带有新变量值. 完全披露,我不知道为什么这样做.它可能不应该,并且可能依赖于一个不起眼的bug.它可能会在将来的版本中停止工作. 请注意,我称之为$global而不是$singleton,因为它就是这样.单例可用于实现全局变量的语义,但它们不是同一个东西. 最后,即使我能感受到Puppet 3更新所面临的痛苦,你也应该花时间让它继续下去.我们都可能无法负担更长时间运行2.x. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |