使用hiera设置类参数? [英] Using hiera to set class parameters?
问题描述
我试图弄清楚如何使用hiera设置类参数的值. 我正在用两个简单的类来测试事物:testhiera和testhiera2
I am trying to figure out how to use hiera for setting the values for class parameters. I am testing things out with two simple classes: testhiera and testhiera2
以下是这些类别:
[root@puppet-el7-001 modules]# cat testhiera/manifests/init.pp
class testhiera (
$haproxy_cert_content = 'unknown' ,
) {
notify {"cert is $haproxy_cert_content":}
}
[root@-puppet-el7-001 modules]# cat testhiera2/manifests/init.pp
class testhiera2 (
$haproxy_cert_content = 'unknown' ,
) {
notify {"number two cert is $haproxy_cert_content":}
}
这是我的/etc/puppletlabs/puppet/hiera.yaml文件
here is my /etc/puppletlabs/puppet/hiera.yaml file
---
:backends:
- yaml
:hierarchy:
- defaults
- "%{clientcert}"
- "%{environment}"
- global
:yaml:
# datadir is empty here, so hiera uses its defaults:
# - /var/lib/hiera on *nix
# - %CommonAppData%\PuppetLabs\hiera\var on Windows
# When specifying a datadir, make sure the directory exists.
:datadir: /root/puppetmaster/hiera/ost-el7
我有这个文件/root/puppetmaster/hiera/ost-el7/defaults.yaml
and I have this file /root/puppetmaster/hiera/ost-el7/defaults.yaml
另一个问题:我是否必须将文件命名为"defaults.yaml"?我该如何使用其他文件名?
---
testhiera::haproxy_cert_content: "\n
-----BEGIN CERTIFICATE-----\n
blah blah blha\n
-----END RSA PRIVATE KEY-----\n
blah blah blha\n
-----BEGIN CERTIFICATE-----\n
blah blah blha\n
-----END CERTIFICATE-----\n
"
当我像这样执行我的人偶类时...
When I execute my puppet classes like so ...
# puppet apply -e 'include testhiera'
...我得到了预期的输出:
... I get the expected output:
Notice: Compiled catalog for puppet-el7-001.cisco.com in environment production in 0.08 seconds
Notice: cert is
-----BEGIN CERTIFICATE-----
blah blah blha
-----END RSA PRIVATE KEY-----
blah blah blha
-----BEGIN CERTIFICATE-----
blah blah blha
-----END CERTIFICATE-----
Notice: /Stage[main]/Testhiera/Notify[cert is
-----BEGIN CERTIFICATE-----
blah blah blha
-----END RSA PRIVATE KEY-----
blah blah blha
-----BEGIN CERTIFICATE-----
blah blah blha
-----END CERTIFICATE-----
]/message: defined 'message' as 'cert is
-----BEGIN CERTIFICATE-----
blah blah blha
-----END RSA PRIVATE KEY-----
blah blah blha
-----BEGIN CERTIFICATE-----
blah blah blha
-----END CERTIFICATE-----
'
Notice: Finished catalog run in 0.17 seconds
我的问题是将haproxy_cert_content数据获取到的最佳方法是什么 testhiera2类?我是否需要将defaults.yaml文件更改为如下所示...
My question is what is the best way to get thehaproxy_cert_content data to the testhiera2 class? Do I have to change my defaults.yaml file to look like this ...
---
testhiera::haproxy_cert_content: "\n
-----BEGIN CERTIFICATE-----\n
blah blah blha\n
-----END RSA PRIVATE KEY-----\n
blah blah blha\n
-----BEGIN CERTIFICATE-----\n
blah blah blha\n
-----END CERTIFICATE-----\n
"
testhiera2::haproxy_cert_content: "\n
-----BEGIN CERTIFICATE-----\n
blah blah blha\n
-----END RSA PRIVATE KEY-----\n
blah blah blha\n
-----BEGIN CERTIFICATE-----\n
blah blah blha\n
-----END CERTIFICATE-----\n
"
...基本上只是剪切-粘贴线并将testhiera更改为testhiera2?
... basically just cut--n-pasting the lines and changing the testhiera to testhiera2?
我希望有人可以向我展示一种更好的方法.
I hope someone can show me a better way to do this.
谢谢
推荐答案
In Hiera you can reuse already defined variables by using hiera lookup funcion. In your example it will be:
testhiera2::haproxy_cert_content:"%{hiera('testhiera::haproxy_cert_content')}"
另一个问题:Hiera将按照与hiera.yaml
中提供的顺序相同的顺序查找文件中的变量. (了解hiera 层次结构)因此,在您的配置中,它将首先检查文件defaults.yaml
.如果找不到定义的变量,它将尝试求值变量clientcert
并在文件value_of_clientcert.yaml
中查找,然后在文件value_of_environment.yaml
中查找,最后在global.yaml
中查找.
For side question: Hiera will look up for variables in files in the same order as provided in hiera.yaml
. (reading about hiera hierarchy) So in your configuration, first it will check file defaults.yaml
. If it will not find defined variable there it will try to evaluate variable clientcert
and look in file value_of_clientcert.yaml
, next in file value_of_environment.yaml
and at the end in global.yaml
.
您不必将文件命名为defaults.yaml
.根据上面的描述,所有与hiera配置匹配的东西都会很好.
You don't have to name the file defaults.yaml
. Everything that will match hiera configuration, according to description above, will be good.
*顺便说一句,如果您在层次结构层次中使用事实事实,请将::
放在变量名之前,所以%{::facter_fact}
.
*BTW, if you are using facter facts in hiera hierarchy put ::
before the name of variable, so %{::facter_fact}
.
这篇关于使用hiera设置类参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!