本文共 6924 字,大约阅读时间需要 23 分钟。
案例1,通过puppet远程在centos3的/tmp目录下自动创建一个文件test.txt,并写入内容:Welconme
方法一:
1)centos2服务端配置:
[root@centos2 puppet-2.7.21]# vim /etc/puppet/manifests/site.pp
[root@centos2 puppet-2.7.21]# cat /etc/puppet/manifests/site.pp
node default{
file { "/tmp/test.txt":
content=> "this is a test file";
}
}
[root@centos2 puppet-2.7.21]# /etc/init.d/puppetmaster restart
2)测试客户端能否自动创建文件
方法1 通过重启puppet客户端服务,来执行脚本
[root@centos3 puppet-2.7.21]# /etc/init.d/puppet restart
[root@centos3 puppet-2.7.21]# cat /tmp/test.txt
this is a test file
方法2:
1)同步临时生效
[root@centos3 puppet-2.7.21]# puppetd --test --server centos2.cn
info: Caching catalog for centos3.cn
info: Applying configuration version '1429357828'
notice: Finished catalog run in 0.10 seconds
[root@centos3 puppet-2.7.21]# cat /tmp/test.txt
this is a test file
方法3:等30分钟后自己同步。
案例2: 文件分发。
要把server服务器上/opt目录下的a.txt(apache-4.1.12.rpm)发布至client服务器的/opt目录下,文件名不变。 注意:分发文件时,不要用/root ,会有权限问题。
1)centos2配置:
[root@centos2 ~]# cp /etc/hosts /opt/a.txt
[root@centos2 ~]# vim /etc/puppet/fileserver.conf #在文件最后,追加以下内容
[files]
path /opt/
allow 192.168.1.0/24
注: [files] #名字,可以随便写
2)指定客户端要执行的操作:
[root@centos2 ~]# vim /etc/puppet/manifests/site.pp
[root@centos2 ~]# cat /etc/puppet/manifests/site.pp #添加红色内容
node default{
file { "/tmp/test.txt":
content=> "this is a test file";
}
file { "/opt/a.txt":
source => "puppet://$puppetserver/files/a.txt",
#//centos2.cn//opt/a.txt
}
}
注:
file { "/opt/a.txt": #指分发到客户端的路径
source => "puppet://$puppetserver/files/a.txt", 中的files要和/etc/puppet/fileserver.conf中定义的名字一样。
注:修改site.pp 文件后,服务端不需要重新启动服务。
3)同步临时生效
[root@centos3 puppet-2.7.21]# puppetd --test --server centos2.cn
info: Caching catalog for centos3.cn
info: Applying configuration version '1418651351'
notice: /Stage[main]//Node[default]/File[/opt/a.txt]/ensure: defined content as '{md5}e7d6d91a44650a85573b0cd47f2c1647'
notice: Finished catalog run in 0.46 seconds
[root@centos3 puppet-2.7.21]# ls /opt/ #查看
a.txt
[root@centos3 puppet-2.7.21]# cat /opt/a.txt
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.63 centos2.cn
192.168.1.64 centos3.cn
案例3: 自动修改文件属性
1)centos2编写脚本配置
[root@centos3 ~]# ll /opt/a.txt
-rw-r--r-- 1 root root 210 10月 22 16:31 /opt/a.txt
[root@centos2 ~]# cat /etc/puppet/manifests/site.pp
node default{
file { "/tmp/test":
content=> "this is a test file";
}
file { "/opt/a.txt":
source => "puppet://$puppetserver/files/a.txt",
owner => "puppet",
group => "puppet",
mode => 777,
}
}
注:需要将上一个实验的代码删掉
2)同步临时生效测试:
[root@centos3 puppet-2.7.21]# puppetd --test --server centos2.cn
info: Caching catalog for centos3.cn
info: Applying configuration version '1418651544'
notice: /Stage[main]//Node[default]/File[/opt/a.txt]/owner: owner changed 'root' to 'puppet'
notice: /Stage[main]//Node[default]/File[/opt/a.txt]/group: group changed 'root' to 'puppet'
notice: /Stage[main]//Node[default]/File[/opt/a.txt]/mode: mode changed '0644' to '0777'
notice: Finished catalog run in 0.30 seconds
[root@centos3 puppet-2.7.21]# ll /opt/a.txt
-rwxrwxrwx 1 puppet puppet 213 Dec 15 21:49 /opt/a.txt
案例4: server端让client端自动执行shell脚本或命令
通过puppet分发执行shell脚本,然后在客户端的/opt目录下执行test.sh脚本,该脚本执行后,会在/tmp目录创建一个testfile文件。
1)编辑测试脚本
[root@centos2 ~]# vim /opt/test.sh
#!/bin/bash
/bin/touch /tmp/testfile
2)编辑脚本
[root@centos2 opt]#vim /etc/puppet/manifests/site.pp #写入以下红色标记内容
node default{
file { "/tmp/test":
content=> "this is a test file";
}
file { "/opt/a.txt":
source => "puppet://$puppetserver/files/a.txt",
owner => "puppet",
group => "puppet",
mode => 777,
}
file { "/opt/test.sh":
source => "puppet://$puppetserver/files/test.sh",
owner => "puppet",
group => "puppet",
mode => 755,
}
exec { "exec-mkdir":
cwd => "/opt",
command => "sh /opt/test.sh",
user => "puppet",
path => "/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin",
}
}
注: 不用重启服务器的
3)同步测试:
[root@centos3 puppet-2.7.21]#puppetd --test --server centos2.cn
info: Caching catalog for centos3.cn
info: Applying configuration version '1418651975'
notice: /Stage[main]//Node[default]/File[/opt/test.sh]/ensure: defined content as '{md5}d68e2194d349dcc3f1990a0ce37dcf1a'
notice: /Stage[main]//Node[default]/Exec[exec-mkdir]/returns: executed successfully
notice: Finished catalog run in 1.85 seconds
[root@centos3 puppet-2.7.21]# ls /tmp/testfile
/tmp/testfile
[root@centos3 puppet-2.7.21]# ll /opt/
total 8
-rwxrwxrwx 1 puppet puppet 213 Dec 15 21:49 a.txt
-rwxr-xr-x 1 puppet puppet 37 Dec 15 21:59 test.sh
案例5:客户端自动执行服务的启动及关闭:
可以通过puppet对一些服务进行重启,状态等操作。puppet是通过service命令操作的。所以,只能针对在/etc/init.d/目录下的服务 实例:把客户端centos3的vsftpd服务关闭,并把nfs服务启动
1)准备环境:
[root@centos3 ~]# rpm -ivh /mnt/Packages/vsftpd-2.2.2-6.el6_0.1.x86_64.rpm
[root@centos3 ~]# service vsftpd start #为了实现远程关闭服务,所以这里先开着
[root@centos3 ~]#service nfs stop
[root@centos3 ~]# service vsftpd status
[root@centos3 ~]#service nfs status
2)配图manifests菜单,配置客户端如何执行:
[root@centos2 opt]# cat /etc/puppet/manifests/site.pp #添加红色标记内容
node default{
file { "/tmp/test":
content=> "this is a test file";
}
file { "/opt/a.txt":
source => "puppet://$puppetserver/files/a.txt",
owner => "puppet",
group => "puppet",
mode => 777,
}
file { "/opt/test.sh":
source => "puppet://$puppetserver/files/test.sh",
owner => "puppet",
group => "puppet",
mode => 755,
}
exec { "exec-mkdir":
cwd => "/opt",
command => "sh /opt/test.sh",
user => "puppet",
path => "/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin",
}
service { vsftpd":
ensure => stopped;
"nfs":
ensure => running;
}
}
3)centos3 测试:
[root@centos3 ~]#puppetd --test --server centos2.cn
[root@centos3 ~]# service vsftpd status
vsftpd is stopped
[root@centos3 ~]# service nfs status
rpc.svcgssd is stopped
rpc.mountd (pid 8896) is running...
nfsd (pid 8893 8892 8891 8890 8889 8888 8887 8886) is running...
rpc.rquotad (pid 8880) is running...
案例6:修改同步时间
修改puppet 客户端默认连接puppetmaster的时间 默认puppet 客户端每隔30分钟连接到puppetmaster, 同步一次配置文件 现在修改为1分钟.同步一次。
1)修改配置文件
[root@centos3 ~]# 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
server = centos2.cn
certname = centos3.cn
runinterval = 60 #同步时间间隔默认为妙
# Where puppetd caches the local configuration. An
[root@centos3 ~]# /etc/init.d/puppet restart
[root@centos3 ~]# rm -rf /opt/*
[root@centos3 ~]# ls /opt/
2)测试:等待1分钟后再查看。
[root@centos3 ~]# ls /opt/ #发现执行了site.pp中内容
a.txt test.sh
转载地址:http://gwxvb.baihongyu.com/