我正在为我的 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
Run Code Online (Sandbox Code Playgroud)
我看到了这个相关的答案,它有以下代码:
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';
}
Run Code Online (Sandbox Code Playgroud)
然而,
A naive guess would be:
'host':
'www1':
'ip': 1.2.3.4
'www2':
'ip': 1.2.3.5
Run Code Online (Sandbox Code Playgroud)
But because of #2 above, I'm not comfortable moving forward with this in production. My question then:
Where can I find documentation on using the host class this way?
Or failing that
How else can I manage multiple /etc/hosts
entries with an ENC?
NB: These definitions are being used to quickly configure transient multi-node clusters on a cloud services API for development and testing purposes, so while I'm not wholly unwilling to explore a DNS-based (or other alternative) solution, managing /etc/hosts
in this way (if possible) is a much simpler solution with far fewer moving parts.
解决方案:在这里发布以供参考是我的最终代码:
class my_hosts(
$hosts = {
'localhost.localdomain' => {
'ensure' => 'present',
'name' => 'localhost.localdomain',
'host_aliases' => 'localhost',
'ip' => '127.0.0.1',
},
},
) {
create_resources host, $hosts
}
Run Code Online (Sandbox Code Playgroud)
然后我的 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
Run Code Online (Sandbox Code Playgroud)
广告 1 和 2:
host {
'front-01': ip => '192.168.1.103';
'front-02': ip => '192.168.1.106';
}
Run Code Online (Sandbox Code Playgroud)
只是缩短符号为
host { 'front-01':
ip => '192.168.1.103',
}
host { 'front-02':
ip => '192.168.1.106',
}
Run Code Online (Sandbox Code Playgroud)
广告3:
当您有这样的 YAML 数据条目时:
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
Run Code Online (Sandbox Code Playgroud)
您可以将其加载到 puppet 哈希中,然后从中创建资源
$custom_hosts = hiera('custom_hosts',{})
create_resources(host, $custom_hosts)
Run Code Online (Sandbox Code Playgroud)
这产生与以下相同的结果:
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',
}
Run Code Online (Sandbox Code Playgroud)
所以这些行应该写入/etc/hosts:
1.2.3.4 www1
1.2.3.5 www2 # other attributes work also
1.2.3.6 testserver
Run Code Online (Sandbox Code Playgroud)