木偶variables不总是工作

我想通过分组机器(例如RH5和RH6机器)在nodes.pp中应用“DRY”(不要重复自己)原则,而不是为所有RH5和RH6服务器添加多行包含。

木偶在tst-01使用时工作正常:

node "tst-01" inherits basenode { 

但是当我试图用这种configuration将服务器组织成组时,它会中断:

 node "tst-01" inherits redhat6server { 

“inheritanceredhat6server”的错误是:

 err: Could not retrieve catalog; skipping run [root@tst-01 ~]# puppet agent --test err: Could not retrieve catalog from remote server: Error 400 on SERVER: Failed to parse template ldap/access.conf: Could not find value for 'netgroup' at 124:/etc/puppet/modules/ldap/templates/access.conf at /etc/puppet/modules/ldap/manifests/init.pp:82 on node tst-01.tst.it.test.com warning: Not using cache on failed catalog err: Could not retrieve catalog; skipping run 

这是access.conf文件,如果inheritance设置为“inherits basenode”,则工作正常。

 [root@puppet]# grep -v "#" /etc/puppet/modules/ldap/templates/access.conf + : root : LOCAL + : @<%= netgroup %> : ALL - : ALL : ALL [root@puppet]# 

这是/etc/puppet/manifests/nodes.pp中的configuration。

 # Basenode configuration node "basenode" { include resolv_conf include sshd include ntpd include motd } # Groups node "redhat6server" inherits basenode { include ldap_auth } # Testservers node "tst-01" inherits redhat6server { $netgroup = tst-01 } 

它可能会失败,因为在$netgroupvariables评估之前发生inheritance,因此目录编译失败。 但是,使用这种方法从数据中分离代码有一定的局限性, 应该避免 。

我已经重构了我的木偶代码,使用分层数据进行数据绑定,并实现了对类似节点进行分组的相同效果。 一个简单的例子,假设你只需要对RHEL服务器进行分组,那将是:

  • 分层数据将使用以下定义来检索:

     hiera.yaml --- :backends: - yaml :hierarchy: - %{::operatingsystem} - %{::hostname} - common :yaml: :datadir: /etc/puppet/hieradata/ 

    operatingsystemhostname都是facts 。 Hiera将按照以下顺序尝试数据parsing:主机名为some-rhel-hostname的Red Hat系统:

    • RedHat.yaml
    • 一些-RHEL-hostname.yaml
    • common.yaml
  • 这期待以下目录结构和文件:

     /etc/puppet/hieradata/ ├── common.yaml ├── RedHat.yaml ├── some-rhel-hostname.yaml └── tst-01.yaml 
  • 示例yaml文件的内容:

     common.yaml --- classes: - resolv_conf - sshd - ntpd - motp RedHat.yaml --- classes: - ladp_auth some-rhel6-hostname.yaml --- classes: - this_host_only ldap_auth::netgroup: foo-bar tst-01.yaml --- ldap_auth::netgroup: tst-01 nodes.pp default { hiera_include(classes) } 

    注意hiera_include 函数的使用,它将返回每个节点的所有值(来自common.yaml和来自层次结构parsing的那些值的数组),即some-rhel-hostname将包括来自common.yaml所有类来自RedHat.yaml和来自它自己的yaml文件的this_host_only )。

    如何在你的模块中使用数据绑定取决于你正在使用的puppet版本。 检查这里的指针。