I'm writing an External Node Classifier for my Puppet infrastructure, and I have the need to manipulate the /etc/hosts
file on each node. The following (due to the duplicate key) is invalid 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
I see this related answer, which has the following code:
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';
}
However,
- It's not immediately obvious to me what's actually going on here.
- I can't find documentation for this anywhere.
- Because these (don't look like) class parameters - and because of #1 and #2 above - it's also not obvious to me how I could translate this into YAML.
A naive guess would be:
'host':
'www1':
'ip': 1.2.3.4
'www2':
'ip': 1.2.3.5
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.
SOLUTION: Posted here for reference is my final code:
class my_hosts(
$hosts = {
'localhost.localdomain' => {
'ensure' => 'present',
'name' => 'localhost.localdomain',
'host_aliases' => 'localhost',
'ip' => '127.0.0.1',
},
},
) {
create_resources host, $hosts
}
And then my ENC YAML looks like this:
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
ad 1 & 2:
is just a shortened notation for
ad 3:
When you have a YAML data entry like this:
you can load it into a puppet hash and then create resources from it
This yields the same result as:
So these lines should be written to /etc/hosts: