我正在为Puppet基础结构编写一个外部节点分类器,并且需要在每个节点上操作/etc/hosts文件。 以下(由于重复键)是无效的YAML:
--- host: name: www1.example.com host_aliases: www1 ip: 1.2.3.4 host: name: www2.example.com host_aliases: www2 ip: 1.2.3.5
我看到这个相关的答案 ,它有以下代码:
host { # Public IPs - eth0 'front-01': ip => '192.168.1.103'; 'front-02': ip => '192.168.1.106'; # Private IPs - eth1 'priv0-0': ip => '192.169.40.201'; 'priv0-1': ip => '192.169.40.202'; 'priv1-0': ip => '192.169.40.207'; 'priv1-1': ip => '192.169.40.208'; # Virtual IPs - eth0:1 'vip-01': ip => '192.169.50.202'; 'vip-02': ip => '192.169.50.205'; }
然而,
一个天真的猜测是:
'host': 'www1': 'ip': 1.2.3.4 'www2': 'ip': 1.2.3.5
但是因为上面的#2,我不适应这个在生产。 我的问题是:
我在哪里可以find关于如何使用主机类的文档?
或者失败了
我还可以用ENCpipe理多个/etc/hosts条目吗?
注意:这些定义正在被用来快速configuration云服务API上的瞬态多节点集群以用于开发和testing目的,所以虽然我并不是完全不愿意探索基于DNS(或其他替代)解决scheme,但是pipe理/etc/hosts以这种方式(如果可能的话)是一个简单得多的解决scheme,移动部件less得多。
解决scheme:这里供参考是我的最终代码:
class my_hosts( $hosts = { 'localhost.localdomain' => { 'ensure' => 'present', 'name' => 'localhost.localdomain', 'host_aliases' => 'localhost', 'ip' => '127.0.0.1', }, }, ) { create_resources host, $hosts }
然后我的ENC YAML看起来像这样:
classes: my_hosts: hosts: www1.example.com: ensure: present name: www1.example.com host_aliases: www1 ip: 10.0.0.101 www2.example.com: ensure: present name: www2.example.com host_aliases: www2 ip: 10.0.0.102
广告1和2:
host { 'front-01': ip => '192.168.1.103'; 'front-02': ip => '192.168.1.106'; }
只是一个缩写符号
host { 'front-01': ip => '192.168.1.103', } host { 'front-02': ip => '192.168.1.106', }
广告3:
当你有这样的YAML数据input:
custom_hosts: www1: ip: 1.2.3.4 www2: ip: 1.2.3.5 comment: other attributes work also www3: name: testserver ip: 1.2.3.6
你可以把它加载到一个木偶散列,然后从中创build资源
$custom_hosts = hiera('custom_hosts',{}) create_resources(host, $custom_hosts)
这产生了与以下相同的结果:
host { 'www1': ip => '1.2.3.4', } host { 'www2': ip => '1.2.3.5', comment => 'other attributes work also', } host { 'www3': name => 'testserver', ip => '1.2.3.6', }
所以这些行应该写入/ etc / hosts:
1.2.3.4 www1 1.2.3.5 www2 # other attributes work also 1.2.3.6 testserver