实时警报通知:微信告警通知的重要性解析
819
2023-03-15
Puppet模块(三):puppet模块及file资源
作用:通过puppet模块自动控制客户端的puppet配置,当需要修改客户端的puppet配置时不用在客户端一一设置。
1、服务端配置puppet模块
(1)模块清单
1 2 3 4 5 6 7 8 9 10 11 | [root@puppet ~]# tree /etc/puppet/modules/puppet/ /etc/puppet/modules/puppet/ ├── files ├── manifests │ ├── config.pp │ ├── init.pp │ ├── install.pp │ ├── params.pp │ └── service.pp └── templates └── puppet.conf.erb |
1234567891011[root@puppet ~]# tree /etc/puppet/modules/puppet//etc/puppet/modules/puppet/├── files├── manifests│ ├── config.pp│ ├── init.pp│ ├── install.pp│ ├── params.pp│ └── service.pp└── templates └── puppet.conf.erb
(2)定义参数类
(3)定义安装类
1 2 3 4 5 6 7 8 9 | [root@puppet ~]# vi /etc/puppet/modules/puppet/manifests/install.pp class puppet::install { package { "puppet": ensure => $puppet::params::puppet_release, } package { "facter": ensure => $puppet::params::facter_release, } } |
123456789[root@puppet ~]# vi /etc/puppet/modules/puppet/manifests/install.ppclass puppet::install { package { "puppet": ensure => $puppet::params::puppet_release, } package { "facter": ensure => $puppet::params::facter_release, }}
说明:根据系统版本(centos5或centos6)来安装指定版本的puppet和facter
(4)定义配置类
1 2 3 4 5 6 7 8 9 10 11 12 | [root@puppet ~]# vi /etc/puppet/modules/puppet/manifests/config.pp class puppet::config { file { "/etc/puppet/puppet.conf": ensure => present, content => template("puppet/puppet.conf.erb"), #文件内容来源于模板 owner => "root", group => "root", mode => '0644', require => Class["puppet::install"], #要求先完成install.pp notify => Class["puppet::service"], #通知并触发service.pp } } |
123456789101112[root@puppet ~]# vi /etc/puppet/modules/puppet/manifests/config.ppclass puppet::config { file { "/etc/puppet/puppet.conf": ensure => present, content => template("puppet/puppet.conf.erb"), #文件内容来源于模板 owner => "root", group => "root", mode => '0644', require => Class["puppet::install"], #要求先完成install.pp notify => Class["puppet::service"], #通知并触发service.pp }}
说明:将配置模板传送到客户端的puppet.conf,设置用户、组、权限
(5)定义配置模板
1 2 3 4 5 6 7 8 9 10 11 12 13 | [root@puppet ~]# vi /etc/puppet/modules/puppet/template/puppet.conf.erb ### config by puppet ### [main] logdir = /var/log/puppet rundir = /var/run/puppet ssldir = $vardir/ssl [agent] classfile = $vardir/classes.txt localconfig = $vardir/localconfig server = <%= scope.lookupvar('puppet::params::puppetserver') %> #参数调用格式<%= 参数 %> report = true pluginsync = false runinterval = 10 #puppet 客户端默认连接到puppetmaster的时间间隔,默认30分钟,这里测试设为10秒,将会生成大量报告,建议测试完后改回1800 |
12345678910111213[root@puppet ~]# vi /etc/puppet/modules/puppet/template/puppet.conf.erb### config by puppet ###[main] logdir = /var/log/puppet rundir = /var/run/puppet ssldir = $vardir/ssl[agent] classfile = $vardir/classes.txt localconfig = $vardir/localconfig server = <%= scope.lookupvar('puppet::params::puppetserver') %> #参数调用格式<%= 参数 %> report = true pluginsync = false runinterval = 10 #puppet 客户端默认连接到puppetmaster的时间间隔,默认30分钟,这里测试设为10秒,将会生成大量报告,建议测试完后改回1800
说明:模板调用了params.pp中的参数$puppetserver
(6)定义服务类
1 2 3 4 5 6 7 8 9 10 | [root@puppet ~]# vi /etc/puppet/modules/puppet/manifests/service.pp class puppet::service { service { "puppet": ensure => running, #确保服务puppet处于运行状态 hasstatus => true, #是否支持service puppet status命令查看状态 hasrestart => true, #是否支持service puppet restart命令重启服务 enable => true, #是否开机启动服务 require => Class["puppet::install"], } } |
12345678910[root@puppet ~]# vi /etc/puppet/modules/puppet/manifests/service.ppclass puppet::service { service { "puppet": ensure => running, #确保服务puppet处于运行状态 hasstatus => true, #是否支持service puppet status命令查看状态 hasrestart => true, #是否支持service puppet restart命令重启服务 enable => true, #是否开机启动服务 require => Class["puppet::install"], }}
(7)定义puppet主类
1 2 3 4 | [root@puppet ~]# vi /etc/puppet/modules/puppet/manifests/init.pp class puppet { include puppet::params,puppet::install,puppet::config,puppet::service } |
1234[root@puppet ~]# vi /etc/puppet/modules/puppet/manifests/init.ppclass puppet { include puppet::params,puppet::install,puppet::config,puppet::service}
(8)定义节点文件,调用模块
[root@puppet ~]# vi /etc/puppet/manifests/centostest.pp
include ntp, yum, puppet
}
(9)应用节点文件
1 2 | [root@puppet ~]# vi /etc/puppet/manifests/site.pp import "centostest.pp" |
12[root@puppet ~]# vi /etc/puppet/manifests/site.ppimport "centostest.pp"
2、测试:软件安装版本、配置文件、服务启动
(1)查看已安装版本
1 2 3 4 5 6 | [root@centostest ~]# facter | grep operatingsystemmajrelease operatingsystemmajrelease => 6 [root@centostest ~]# rpm -aq|grep puppet puppet-3.7.3-1.el6.noarch [root@centostest ~]# rpm -aq|grep facter facter-2.3.0-1.el6.x86_64 |
123456[root@centostest ~]# facter | grep operatingsystemmajreleaseoperatingsystemmajrelease => 6[root@centostest ~]# rpm -aq|grep puppetpuppet-3.7.3-1.el6.noarch[root@centostest ~]# rpm -aq|grep facterfacter-2.3.0-1.el6.x86_64
(2)查看服务状态
1 2 3 4 5 6 | [root@centostest ~]# /etc/init.d/puppet stop Stopping puppet agent: [确定] [root@centostest ~]# /etc/init.d/puppet status puppet 已停 [root@centostest ~]# chkconfig --list | grep puppet puppet 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭 |
123456[root@centostest ~]# /etc/init.d/puppet stopStopping puppet agent: [确定][root@centostest ~]# /etc/init.d/puppet statuspuppet 已停[root@centostest ~]# chkconfig --list | grep puppetpuppet 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
(3)查看配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | [root@centostest ~]# cat /etc/puppet/puppet.conf [main] # The Puppet log directory. # The default value is '$vardir/log'. logdir = /var/log/puppet # Where Puppet PID files are kept. # The default value is '$vardir/run'. rundir = /var/run/puppet # Where SSL certificates are kept. # The default value is '$confdir/ssl'. ssldir = $vardir/ssl [agent] # The file in which puppetd stores a list of the classes # associated with the retrieved configuratiion. Can be loaded in # the separate ``puppet`` executable using the ``--loadclasses`` # option. # The default value is '$confdir/classes.txt'. classfile = $vardir/classes.txt # Where puppetd caches the local configuration. An # extension indicating the cache format is added automatically. # The default value is '$confdir/localconfig'. localconfig = $vardir/localconfig |
12345678910111213141516171819202122[root@centostest ~]# cat /etc/puppet/puppet.conf[main] # The Puppet log directory. # The default value is '$vardir/log'. logdir = /var/log/puppet # Where Puppet PID files are kept. # The default value is '$vardir/run'. rundir = /var/run/puppet # Where SSL certificates are kept. # The default value is '$confdir/ssl'. ssldir = $vardir/ssl[agent] # The file in which puppetd stores a list of the classes # associated with the retrieved configuratiion. Can be loaded in # the separate ``puppet`` executable using the ``--loadclasses`` # option. # The default value is '$confdir/classes.txt'. classfile = $vardir/classes.txt # Where puppetd caches the local configuration. An # extension indicating the cache format is added automatically. # The default value is '$confdir/localconfig'. localconfig = $vardir/localconfig
(4)客户端执行测试
(5)真正执行puppet agent(不带--noop参数)
报错:YUM安装失败,无法下载软件包,在客户端yum clean up再yum list恢复仓库后重试
(6)查看客户端日志
#以上日志是第一次执行puppet agent,安装facter成功,但下载puppet-3.7.1失败
#以上是重新获取YUM仓库后,第二次执行puppet agent的日志, 成功将puppet-3.7.3降为3.7.1版本,因此最好一开始指定好puppet版本安装。
(7)查看客户端测试结果
查看已安装版本:
1 2 3 4 | [root@centostest ~]# rpm -aq|grep facter facter-2.2.0-1.el6.x86_64 [root@centostest ~]# rpm -aq|grep puppet puppet-3.7.1-1.el6.noarch |
1234[root@centostest ~]# rpm -aq|grep facterfacter-2.2.0-1.el6.x86_64[root@centostest ~]# rpm -aq|grep puppetpuppet-3.7.1-1.el6.noarch
查看服务状态:
1 2 3 4 | [root@centostest ~]# /etc/init.d/puppet status puppet (pid 36125) 正在运行... [root@centostest ~]# chkconfig --list | grep puppet puppet 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭 |
1234[root@centostest ~]# /etc/init.d/puppet statuspuppet (pid 36125) 正在运行...[root@centostest ~]# chkconfig --list | grep puppetpuppet 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
查看配置文件:
结论:软件版本成功转变成指定版本;服务启动并添加到开机启动中;配备文件成功从模板获取,参数应用成功。
3、测试配置文件的变更影响
客户端修改配置文件导致puppet服务重启:
1 2 3 4 5 6 7 8 9 10 11 | [root@centostest ~]# echo "#add a line" >> /etc/puppet/puppet.conf [root@centostest ~]# tailf /var/log/message Nov 6 15:33:57 centostest puppet-agent[57545]: (/Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]/content) content changed '{md5}29acb66e2f297a5cf2ff6cbe731998f5' to '{md5}bb6d66a4b72890ef1bfa048c0cf179d8' Nov 6 15:33:57 centostest puppet-agent[56826]: Caught HUP; calling restart Nov 6 15:33:57 centostest puppet-agent[57545]: (/Stage[main]/Puppet::Service/Service[puppet]) Triggered 'refresh' from 1 events Nov 6 15:33:57 centostest puppet-agent[57545]: Finished catalog run in 1.10 seconds Nov 6 15:33:58 centostest puppet-agent[56826]: Caught HUP; calling restart Nov 6 15:33:58 centostest puppet-agent[56826]: Restarting with '/usr/bin/puppet agent' Nov 6 15:33:59 centostest puppet-agent[57782]: Reopening log files Nov 6 15:34:00 centostest puppet-agent[57782]: Starting Puppet client version 3.7.1 Nov 6 15:34:02 centostest puppet-agent[57784]: Finished catalog run in 0.63 seconds |
1234567891011[root@centostest ~]# echo "#add a line" >> /etc/puppet/puppet.conf[root@centostest ~]# tailf /var/log/messageNov 6 15:33:57 centostest puppet-agent[57545]: (/Stage[main]/Puppet::Config/File[/etc/puppet/puppet.conf]/content) content changed '{md5}29acb66e2f297a5cf2ff6cbe731998f5' to '{md5}bb6d66a4b72890ef1bfa048c0cf179d8'Nov 6 15:33:57 centostest puppet-agent[56826]: Caught HUP; calling restartNov 6 15:33:57 centostest puppet-agent[57545]: (/Stage[main]/Puppet::Service/Service[puppet]) Triggered 'refresh' from 1 eventsNov 6 15:33:57 centostest puppet-agent[57545]: Finished catalog run in 1.10 secondsNov 6 15:33:58 centostest puppet-agent[56826]: Caught HUP; calling restartNov 6 15:33:58 centostest puppet-agent[56826]: Restarting with '/usr/bin/puppet agent'Nov 6 15:33:59 centostest puppet-agent[57782]: Reopening log filesNov 6 15:34:00 centostest puppet-agent[57782]: Starting Puppet client version 3.7.1Nov 6 15:34:02 centostest puppet-agent[57784]: Finished catalog run in 0.63 seconds
结论:成功改变配置文件内容,触发puppet服务重启,接下来是agent启动的信息。
4、file资源
1 2 3 4 5 6 | file {'nginx.conf': ensure => file, #定义类型:文件file或目录directory mode => '0640', #权限 owner => root, #属于用户 group => root, #属于用户组 } |
123456file {'nginx.conf': ensure => file, #定义类型:文件file或目录directory mode => '0640', #权限 owner => root, #属于用户 group => root, #属于用户组}
其他参数
发表评论
暂时没有评论,来抢沙发吧~