转载:http://blog.51cto.com/ywzhou/1577432
作用:自动配置客户端的hosts文件,解决网络内的计算机之间计算机名称解析问题,适用于没有部署DNS服务器的内网环境。
1、服务端配置puppet模块
(1)模块清单
[root@puppet ~]# tree /etc/puppet/modules/host/
/etc/puppet/modules/host/
├── files
├── manifests
│ └── init.pp
└── templates
(2)定义host主类
[root@puppet ~]# vi /etc/puppet/modules/host/manifests/init.pp
class host {
host { 'puppet.ewin.com': #计算机名称
ensure => present, #基础属性,应用此资源
host_aliases => "puppet", #计算机别名,可以多个
ip => "10.188.1.73", #计算机IP地址
}
host { 'zabbix.ewin.com':
ensure => present,
host_aliases => "zabbix",
ip => "10.188.1.103",
}
host { 'centostest.ewin.com':
ensure => present,
host_aliases => "centostest",
ip => "10.188.1.85",
}
host { 'ywzhou-pc.ewin.com':
ensure => present,
host_aliases => "ywzhou-pc",
ip => "10.188.1.172",
}
}
说明:有多少条解析记录就写多少个host资源,需要注意的是计算机的IP必须固定,使用了DHCP服务的最好在DHCP服务器或路由器上进行保留或绑定;
或者手动设定IP,还可以通过puppet来管理客户端的主机名和IP。
(3)定义节点文件,调用模块
[root@puppet ~]# vi /etc/puppet/manifests/centostest.pp
node "centostest.ewin.com" {
include ntp, yum, puppet, host
}
(4)应用节点文件
[root@puppet ~]# vi /etc/puppet/manifests/site.pp
import "centostest.pp"
2、测试
(1)客户端执行前查看hosts文件
[root@centostest ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.188.1.103 zabbix.ewin.com
10.188.1.73 puppet.ewin.com
说明:这是安装客户端时手动设置的。
(2)客户端执行puppet agent -t后查看日志
[root@centostest ~]# tailf /var/log/messages
Nov 11 14:22:39 centostest puppet-agent[20214]: (/Stage[main]/Host/Host[zabbix.ewin.com]/host_aliases) defined 'host_aliases' as 'zabbix'
Nov 11 14:22:39 centostest puppet-agent[20214]: (/Stage[main]/Host/Host[ywzhou-pc.ewin.com]/ensure) created
Nov 11 14:22:39 centostest puppet-agent[20214]: (/Stage[main]/Host/Host[centostest.ewin.com]/ensure) created
Nov 11 14:22:39 centostest puppet-agent[20214]: (/Stage[main]/Host/Host[puppet.ewin.com]/host_aliases) defined 'host_aliases' as 'puppet'
Nov 11 14:22:40 centostest puppet-agent[20214]: Finished catalog run in 1.13 seconds
说明:对原有的记录进行修改,增加了别名记录,没有的就创建。
(3)客户端再次查看hosts文件
[root@centostest ~]# cat /etc/hosts
# HEADER: This file was autogenerated at Tue Nov 11 14:22:39 +0800 2014
# HEADER: by puppet. While it can still be managed manually, it
# HEADER: is definitely not recommended.
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.188.1.103 zabbix.ewin.com zabbix
10.188.1.73 puppet.ewin.com puppet
10.188.1.172 ywzhou-pc.ewin.com ywzhou-pc
10.188.1.85 centostest.ewin.com centostest
结论:可以看到对已有的条目添加了别名,没有的就新增了记录,这样就相当于建立了一个半自动的DNS;但是当计算机太多时最好还是搭建一个内网DNS服务器。
有新的记录就直接写在class host里,通过puppet发布到客户端,所有调用了host类的客户端就自动更新了。
3、host资源
host { 'resource title':
name => #主机名,可以写在标题中
ensure => #基本参数,present, absent
comment => #注释说明
host_aliases => #别名
ip => #IP地址
target => #保住服务信息的文件,大部分系统默认是/etc/hosts
}