通过配置ssh深刻理解puppet的语法及工作机制
需求分析
1)、要求openssh-server处于被安装状态
2)、要求在配置文件/etc/ssh/sshd_config正确的情况下,sshd服务处于运行状态
2)、要求/etc/ssh/sshd_config文件属性不被串改(权限、属主、属组等)
3)、要求/etc/ssh/sshd_config文件被修改或者删除后会被自动修复
4)、要求通过puppetserver端修改/etc/ssh/sshd_config之后,sshd服务能够自动重启。
定义全局配置信息
定义全局配置文件site.pp
[root@puppetserver ~]# vim /etc/puppet/manifests/site.pp
import 'nodes/*'
$puppetserver = 'puppetserver.rsyslog.org'
创建并配置nodes.pp节点
[root@puppetserver ~]# mkdir /etc/puppet/manifests/nodes –p
[root@puppetserver ~]# vim /etc/puppet/manifests/nodes/nodes.pp
> node /^agent\d+\.rsyslog.org$/{
> include ssh
> }
> endf
设置模块搜索路径
vim /etc/puppet/puppet.conf
[main]
modulepath = /etc/puppet/modules:/var/lib/puppet/modules:/usr/local/lib/puppet/modules
创建模块目录结构
[root@puppetserver ~]# mkdir -vp
/etc/puppet/modules/ssh/{files,templates,manifests}
创建配置文件
创建配置文件(/etc/puppet/modules/ssh/manifests目录下)
1)、创建site.pp文件
class ssh{
include ssh::params,ssh::config,ssh::service,ssh::install
}
2)、创建install.pp文件
class ssh::install{
package { $ssh::params::ssh_package_name:
ensure => installed,
}
}
3)、创建config.pp文件
class ssh::config{
file { $ssh::params::ssh_service_config:
ensure => present,
owner => 'root',
group => 'root',
mode => 0440,
source => "puppet:///modules/ssh/etc/ssh/sshd_config",
require => Class["ssh::install"],
notify => Class["ssh::service"],
}
}
4)、创建service.pp文件
class ssh::service{
service { $ssh::params::ssh_service_name:
ensure => running,
hasstatus => true,
hasrestart => true,
enable => true,
require => Class["ssh::config"],
}
}
5)、创建params.pp文件
class ssh::params {
case $::operatingsystem {
Slaris: {
$ssh_package_name = 'openssh'
$ssh_service_config = '/etc/ssh/sshd_config'
$ssh_service_name = 'sshd'
}
/^(Ubuntu|Debian)$/: {
$ssh_package_name = 'openssh-server'
$ssh_service_config = '/etc/ssh/sshd_config'
$ssh_service_name = 'sshd'
}
/^(RedHat|CentOS|Fedora)$/: {
$ssh_package_name = 'openssh-server'
$ssh_service_config = '/etc/ssh/sshd_config'
$ssh_service_name = 'sshd'
}
default: {
$ssh_package_name = 'openssh-server'
$ssh_service_config = '/etc/ssh/sshd_config'
$ssh_service_name = 'sshd'
}
}
}
创建测试文件
[root@puppetserver manifests]# mkdir /etc/puppet/modules/ssh/files/etc/ssh/ -p [root@puppetserver manifests]# scp agent1.rsyslog.org:/etc/ssh/sshd_config /etc/puppet/modules/ssh/files/etc/ssh/
[root@puppetserver ~]# service puppetmaster reload
测试(puppet kick的方式)
Puppet server端开启调试模式测试
[root@puppetserver ~]# puppet master --no-daemonize --verbose
Puppet agent端开启调试模式测试
[root@puppetserver manifests]# puppetrun -p 10 --host agent1.rsyslog.org
Triggering agent1.rsyslog.org
Getting status
status is success
agent2.rsyslog.org finished with exit code 0
Finished
本文转自凌激冰51CTO博客,原文链接:http://blog.51cto.com/dreamfire/1257719,如需转载请自行联系原作者