转载:/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.ppclass host {host { '':#计算机名称ensure => present, #基础属性,应用此资源host_aliases => "puppet",#计算机别名,可以多个ip => "10.188.1.73", #计算机IP地址}host { '':ensure => present,host_aliases => "zabbix",ip => "10.188.1.103",}host { '':ensure => present,host_aliases => "centostest",ip => "10.188.1.85",}host { 'ywzhou-':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.ppnode "" {include ntp, yum, puppet, host}
(4)应用节点文件
[root@puppet ~]# vi /etc/puppet/manifests/site.ppimport "centostest.pp"
2、测试
(1)客户端执行前查看hosts文件
[root@centostest ~]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain610.188.1.103 10.188.1.73
说明:这是安装客户端时手动设置的。
(2)客户端执行puppet agent -t后查看日志
[root@centostest ~]# tailf /var/log/messages Nov 11 14:22:39 centostest puppet-agent[4]: (/Stage[main]/Host/Host[]/host_aliases) defined 'host_aliases' as 'zabbix'Nov 11 14:22:39 centostest puppet-agent[4]: (/Stage[main]/Host/Host[ywzhou-]/ensure) createdNov 11 14:22:39 centostest puppet-agent[4]: (/Stage[main]/Host/Host[]/ensure) createdNov 11 14:22:39 centostest puppet-agent[4]: (/Stage[main]/Host/Host[]/host_aliases) defined 'host_aliases' as 'puppet'Nov 11 14:22:40 centostest puppet-agent[4]: 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 # 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.localdomain610.188.1.103 zabbix10.188.1.73 puppet10.188.1.172 ywzhou- ywzhou-pc10.188.1.85 centostest
结论:可以看到对已有的条目添加了别名,没有的就新增了记录,这样就相当于建立了一个半自动的DNS;但是当计算机太多时最好还是搭建一个内网DNS服务器。有新的记录就直接写在class host里,通过puppet发布到客户端,所有调用了host类的客户端就自动更新了。
3、host资源
host { 'resource title':name => #主机名,可以写在标题中ensure => #基本参数,present, absentcomment=> #注释说明host_aliases => #别名ip => #IP地址target => #保住服务信息的文件,大部分系统默认是/etc/hosts}
如果觉得《puppet host模块》对你有帮助,请点赞、收藏,并留下你的观点哦!