预计阅读本页时间:-
5.5.4 如何根据不同名字的节点机器推送不同的文件
如果需要让Puppet-Server端分别向名为client.cn7788.com 的客户机推送/etc/crontab文件,向名为fabric.cn7788.com 的客户机推送/etc/hosts文件,向名为nginx.cn7788.com 的客户机推送/etc/resolv.conf文件,应该如何实现呢?
需求比较复杂,可以通过Puppet模块来实现需求,模块文件一般放置在服务器端的/etc/puppet/modules/下。在下面首先会定义一个名为pushfile的模块。模块是Puppet生态系统的核心部分,我们一般通过资源的定义告诉Puppet应该去做什么,通常会针对一个应用编写一个模块,例如即将定义的pushfile模块。
广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元
首先注释掉fileserver.conf文件里面的相关内容,清空site.pp文件后输入新的内容,然后在/etc/puppet/modules下面建立名为pushfile的模块,操作如下:
mkdir -p /etc/puppet/modules/pushfile/{manifests,files,templates}
操作完成后,site.pp文件的内容如下:
import "node.pp"
这里扩展了site.pp文件的内容,它会载入node.pp文件,这样Puppet-Master在启动的时候,就会自动载入并处理node.pp文件。
这时,服务器端的/etc/puppet/manifests/node.pp的文件内容如下:
node 'client.cn7788.com'{
file
{"/etc/crontab":
source => "puppet://server.cn7788.com/modules/pushfile/crontab",
group => root,
owner => root,
mode => 644,
}
}
node 'fabric.cn7788.com'{
file
{"/etc/hosts":
source => "puppet://server.cn7788.com/modules/pushfile/hosts",
group => root,
owner => root,
mode => 644,
}
}
node 'nginx.cn7788.com'{
file
{"/etc/resolv.conf":
source => "puppet://server.cn7788.com/modules/pushfile/resolv.conf",
group => root,
owner => root,
mode => 644,
}
}
node.pp的配置文件比较长并且也很复杂,究竟应该使用什么方法来检查它的语法错误呢?可以输入如下命令:
puppet parser validate node.pp
如果配置文件是正确的,则什么也不显示;如果检测到错误了,则会以红色醒目字体来提示。
Puppet是利用node(节点)来区分不同的客户端的,它会给不同的客户端分配不同的资源。观察下node.pp文件,在这个文件里,source的值会告诉Puppet去哪里寻找文件,这里将文件全部都置于Puppet-Server的/etc/puppet/modules/site/files目录下面了,它相当于是根目录,然后将服务器端的文件依次复制到此目录的/etc下,操作命令如下:
cp /etc/{crontab,hosts,resolv.conf}
/etc/puppet/modules/pushfile/files
现在用tree命令来查看下pushfile模块的目录树结构,如下:
tree /etc/puppet/modules/pushfile
命令显示结果如下所示:
/etc/puppet/modules/pushfile├──
files│
├──
crontab│
├──
hosts│
└──
resolv.conf├──
manifests└──
templates
3 directories,3 files
下面依次在3台Puppet-Client机器上执行puppet agent命令,以client.cn7788.com 机器为例进行说明,输入的命令如下:
puppet agent --test --server server.cn7788.com
命令显示结果如下所示:
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for client.cn7788.com
Info: Applying configuration version '1446623887'
Notice: /Stage[main]/Main/Node[client.cn7788.com]/File[/etc/crontab]/content:
--- /etc/crontab 2015-11-03 07:34:26.372044003 +0000
+++ /tmp/puppet-file20151104-17989-alqkh6-0 2015-11-04 08:02:50.127072946 +0000
@@ -14,10 +14,9 @@
# | | | | |
# * * * * * user-name command to be executed
-00 01 * * * root /bin/bash /usr/local/nginx/sbin/cut_nginx_log.sh >> /dev/null 2>&1
+#00 01 * * * root /bin/bash /usr/local/nginx/sbin/cut_nginx_log.sh >> /dev/null 2>&1
01 02 * * * root /bin/bash /root/backup.sh >> /dev/null 2>&1
-03 03 * * * root /bin/bash /root/sshdeny.sh >> /dev/null 2>&1
-#01 04 * * * root /bin/bash /root/rsync_dir.sh >>/dev/null 2>&1
+#03 03 * * * root /bin/bash /root/sshdeny.sh >> /dev/null 2>&1
-#*/5 * * * * root /etc/init.d/iptables stop
+*/5 * * * * root /etc/init.d/iptables stop
Info: Computing checksum on file /etc/crontab
Info: /Stage[main]/Main/Node[client.cn7788.com]/File[/etc/crontab]: Filebucketed /etc/crontab to puppet with sum 7e76ef490e02dde0dd8e82a5cf7c0c69
Notice: /Stage[main]/Main/Node[client.cn7788.com]/File[/etc/crontab]/content: content changed '{md5}7e76ef490e02dde0dd8e82a5cf7c0c69' to '{md5}2022061d798f3933e0caafb614272212'
Notice: Finished catalog run in 0.47 seconds
可以看到配置是成功的,/etc/crontab被成功地推送过来了,而且内容也进行了置换,其他节点机器的结果在这里就不打印了。
注意
在/etc/puppet/modules下定义的模块都是自动载入的,所以不需要用import来加载。