5.4 Puppet的简单文件应用

这里还是以上面两台机器为例进行说明。

在服务端/etc/puppet/manifests/下建立文件site.pp,此文件可以将/tmp/andrew.txt的内容和权限都推送过去,如果客户端存在此文件,则会采用此文件定义的文件内容和权限;如果不存在,则在推送过去之后,由所推送的文件来定义相应的内容和权限,文件内容如下:

广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元

node default{

file {"/tmp/andrewy.txt":

content =>"hello, My Name is Andrew.Yu !\n",

ensure => present,

mode => 644,

owner => root,

group => root,

}

}

Puppet的基础结构是这样的:

类型{标题:

属性

=>值

,

}

在上面的代码中,资源的类型是file。Puppet默认提供了很多资源类型,可以用来管理文件、服务、软件包及cron定时任务等。

文件中的其他内容都很好理解,只是ensure=>present表示什么意思呢?

ensure后面可以接许多参数,如果后面接的是present,则会检查该文件是否存在,如果不存在就新建该文件。

服务端先启动puppetmaster进程,命令如下:

service puppetmaster start

然后客户机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 '1446519028'

Notice: /Stage[main]/Main/Node[default]/File[/tmp/yhc.txt]/ensure: created

Notice: Finished catalog run in 0.03 seconds

由上述结果可以看出client.cn7788.com 中不存在/tmp/yhc.txt文件。

还可以再设定这样一种情况:client.cn7788.com 的节点机器存在/tmp/yhc.txt文件。演示步骤如下所示:

1)删除此文件后,在/tmp目录下再建立yhc.txt文件,文件内容如下:

Hi,

This is a test file!

2)然后在client.cn7788.com 机器上再次执行连接server.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 '1446519028'

Notice: /Stage[main]/Main/Node[default]/File[/tmp/yhc.txt]/content:

--- /tmp/yhc.txt 2015-11-03 03:08:03.677046037 +0000

+++ /tmp/puppet-file20151103-5329-179s1ch-0 2015-11-03 03:08:18.765044005 +0000

@@ -1 +1 @@

-Hi,This is a test file!

+hello, My Name is yuhongchun !

Info: Computing checksum on file /tmp/yhc.txt

Info: /Stage[main]/Main/Node[default]/File[/tmp/yhc.txt]: Filebucketed /tmp/yhc.txt to puppet with sum e38fd170e47129b097f377fd9bae116a

Notice: /Stage[main]/Main/Node[default]/File[/tmp/yhc.txt]/content: content changed '{md5}e38fd170e47129b097f377fd9bae116a' to '{md5}448db1ed0c41f6dac48d218e169463ae'

Notice: Finished catalog run in 0.08 seconds

从日志结果中就可以分析出来,yhc.txt文件已经发生了变化。

上面的文件推送应用比较简单,我们应如何根据自己的实际需求,从Puppet-Server服务器端向Puppet-Client客户端分发指定的文件呢?比如说,要将服务器端的/usr/local/src/softlist文件集中分发到Puppet-Client的/tmp下面,应该如何操作呢?步骤如下。

1)先修改/etc/puppet/fileserver.conf文件,命令如下:

[files]

path /usr/local/src

allow *

阅读 ‧ 电子书库

注意

files在这里是一个虚拟目录,它实际对应的是服务器端的/usr/local/src目录,这里的语法跟samba服务器的语法是一样的,即定义的虚拟目录。

allow后面连接的是允许连接到服务端主机地址的设置,这里设置为允许所有。

2)再修改/etc/puppet/manifests/下面的site.pp文件,内容如下:

file

{ "/tmp/softlist":

source => "puppet://server.cn7788.com/files/softlist",

group => root,

owner => root,

mode => "644"

}

3)在client.cn7788.com 机器上执行如下命令(执行命令之前可以先修改/etc/hosts文件,使之与Puppet-Master不一致):

puppet agent --test --server server.cn7788.com

命令显示结果如下:

Info: Retrieving pluginfacts

Info: Retrieving plugin

Info: Caching catalog for client.cn7788.com

Info: Applying configuration version '1446535632'

Notice: /Stage[main]/Main/File[/usr/local/src/softlist]/content:

--- /usr/local/src/softlist 2015-11-03 06:46:28.994043464 +0000

+++ /tmp/puppet-file20151103-6644-mbchft-0 2015-11-03 07:27:15.504044004 +0000

@@ -1,4 +1,6 @@

-apache

-memcache

+nginx

redis

-

+hadoop

+spark

+php-fpm

+pdns

Info: Computing checksum on file /usr/local/src/softlist

Info: /Stage[main]/Main/File[/usr/local/src/softlist]: Filebucketed /usr/local/src/softlist to puppet with sum 7f43de0bcc4ac5f984f17775aba0ba96

Notice: /Stage[main]/Main/File[/usr/local/src/softlist]/content: content changed '{md5}7f43de0bcc4ac5f984f17775aba0ba96' to '{md5}36fe51e7e690fe7d65046e9868ed2fa4'

Notice: /File[/usr/local/src/softlist]/seluser: seluser changed 'unconfined_u' to 'system_u'

Notice: Finished catalog run in 0.39 seconds

观察日志可以得知,Puppet-Client的/etc/hosts文件已经更新了,耗时0.36秒。

同理,如果要推送Puppet-Server端的/etc/crontab文件,只需要关注Puppet-Server端的这两个相关文件即可。

fileserver.conf的文件内容如下:

[files]

path /etc/

allow *

/etc/puppet/manifests/site.pp的文件内容如下:

file

{ "/etc/crontab":

source => "puppet://server.cn7788.com/files/crontab",

group => root,

owner => root,

mode => "644"

}

接着,让client.cn7788.com 节点机器执行同步命令,命令如下:

puppetd --test --server server.cn7788.com

结果如下所示:

Info: Retrieving pluginfacts

Info: Retrieving plugin

Info: Caching catalog for client.cn7788.com

Info: Applying configuration version '1446536063'

Notice: /Stage[main]/Main/File[/etc/crontab]/content:

--- /etc/crontab 2011-09-27 01:33:08.000000000 +0000

+++ /tmp/puppet-file20151103-6923-1twgwy5-0 2015-11-03 07:34:26.168044006 +0000

@@ -14,3 +14,10 @@

# | | | | |

# * * * * * user-name command to be executed

+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

+

+#*/5 * * * * root /etc/init.d/iptables stop

Info: Computing checksum on file /etc/crontab

Info: /Stage[main]/Main/File[/etc/crontab]: Filebucketed /etc/crontab to puppet with sum 4f2aaa54c48dda350f75da151f79ae57

Notice: /Stage[main]/Main/File[/etc/crontab]/content: content changed '{md5}4f2aaa54c48dda350f75da151f79ae57' to '{md5}7e76ef490e02dde0dd8e82a5cf7c0c69'

Notice: Finished catalog run in 0.59 seconds

从日志可以得知,/etc/crontab文件已经很顺利地推送过去了,client.cn7788.com 端的/etc/crontab文件已经跟server.cn7788.com 端的/etc/crontab文件保持一致了,整个过程总共耗时0.59秒。

上面的例子都是分发文件,如果要强制推送文件夹呢?继续进行测试,编辑/etc/puppet/manifests/site.pp文件,文件内容如下:

file

{

"/usr/local/src/test":

source => "puppet://server.puppet.com/files/test",

recurse => true,

ensure => directory,

force => true

}

其中的recurse=>true是递归复制,ensure=>directory是确保客户端存在/usr/local/src/test目录,forece=>true会强制删除或覆盖已存在的目录。

然后,在客户端执行同步命令,命令显示结果如下所示:

Info: Retrieving pluginfacts

Info: Retrieving plugin

Info: Caching catalog for client.cn7788.com

Info: Applying configuration version '1446537314'

Notice: /Stage[main]/Main/File[/usr/local/src/test]/ensure: created

Notice: /Stage[main]/Main/File[/usr/local/src/test/yhc]/ensure: defined content as '{md5}d41d8cd98f00b204e9800998ecf8427e'

Notice: /Stage[main]/Main/File[/usr/local/src/test/cc]/ensure: defined content as '{md5}d41d8cd98f00b204e9800998ecf8427e'

Notice: /Stage[main]/Main/File[/usr/local/src/test/dd]/ensure: defined content as '{md5}d41d8cd98f00b204e9800998ecf8427e'

Notice: Finished catalog run in 0.77 seconds

使用上面的配置继续测试会发现,当服务端源文件夹内增加、更新文件时,客户端会自动增加、更新相应的文件,但在服务端源文件夹内删除文件时,客户端不会自动删除,如果工作中有同步删除需求,可以考虑采用Puppet结合rsync的方式来实现。

file资源是我们在使用Puppet时最常用的资源之一,其配置方法也是多元化的,而且是影响Puppet执行效率的关键。那么,可以利用file资源做哪些工作呢?

·管理文件内容、属性和权限等。

·管理文件、目录、符号链接等。

·通过属性来指定文件来源,也可以通过source属性从远程服务器中下载。

·设置recurse属性为true同步传输整个目录。

file目前可使用的参数如下。

·ensure参数:这个参数指定是否创建、删除文件或目录,有present、absent、file、directory等值。其中present会检查文件是否存在,若不存在则会创建一个空文件;absent会删除文件或目录,如果是目录则需要通过recurse参数指定是否允许递归;如果recurse参数指定的是其他的参数,则会创建连接文件,为了方便管理,建议在创建的时候使用ensure=>link,并通过target参数指定文件。

·force参数:该参数会强制执行删除文件、软链接和目录等相关操作,进行清空子目录、修改文件或链接的目录、删除目录等操作时必须指定force参数,并确保ensure=absent。

·group参数:指定文件或目录的属组,可以是组名或组id,如果是Windows,属组和属主不能相同。

·ignore参数:指定在递归期间对符合指定模式的文件所进行的操作将被忽略。

·links参数:指定处理文件期间如何处理链接文件,可以设置为follow和manage。在复制文件的时候,follow将会复制目标文件来代替链接文件,manage将只会复制链接文件。

·mode参数:用来指定文件或目录的权限,Puppet使用的是传统的Unix权限方案。

·owner参数:指定文件的属主,可以是用户名或用户id,如果是Windows,属组和属主不能相同。

·path参数:指定文件管理的路径。

·purge参数:用于删除在Puppet-Server上不存在的文件,这个参数只有在管理目录时指定了recurse=>true参数才有意义。

·recurse参数:指定是否进行递归调用,其值有true、false、inf和remote。

·source参数:指定将会被复制到指定位置的资源文件。

·target参数:指定创建链接文件的目标文件或目录。

了解完file的参数,再来看看file资源的缺点。

file资源不适合做大量文件的分发处理工作,如果文件数量增多了,效率就会下降,尤其是文件的属性比较多的时候。可以通过客户端的反馈结果发现,大量文件的分发处理工作是耗时最多的一个。在后面的内容中会提到,这个缺点可以结合rsync服务来弥补。

如果局域网内的机器非常多,每次从Puppet-Client向Puppet-Server端发送证书请求时都必须手动输入命令给客户端签名,那么在服务器上应该如何配置以确保自动安全地给客户端签名呢?步骤如下。

1)编辑/etc/puppet/puppet.conf,在[main]的最后添加如下内容:

autosign = true

2)重启puppetmaster服务,命令如下:

service puppetmaster restart

3)另外配置一台名为fabric.cn7788.com 的Puppet-Client机器,安装过程此处略过,然后使用如下命令连接Puppet-Server机器:

puppet agent --test --server server.cn7788.com

此命令返回结果显示如下所示:

Info: Creating a new SSL key for fabric.cn7788.com

Info: Caching certificate for ca

Info: csr_attributes file loading from /etc/puppet/csr_attributes.yaml

Info: Creating a new SSL certificate request for fabric.cn7788.com

Info: Certificate Request fingerprint (SHA256): C4:91:56:7A:46:78:89:5E:DC:A1:B9:93:23:DD:6D:31:82:AD:71:86:EC:86:D8:71:34:96:EE:4E:16:6A:33:80

Info: Caching certificate for fabric.cn7788.com

Info: Caching certificate_revocation_list for ca

Info: Caching certificate for ca

Info: Retrieving pluginfacts

Info: Retrieving plugin

Info: Caching catalog for fabric.cn7788.com

Info: Applying configuration version '1446606289'

Notice: /Stage[main]/Main/File[/usr/local/src/test]/ensure: created

Notice: /Stage[main]/Main/File[/usr/local/src/test/cc]/ensure: defined content as '{md5}d41d8cd98f00b204e9800998ecf8427e'

Notice: /Stage[main]/Main/File[/usr/local/src/test/dd]/ensure: defined content as '{md5}d41d8cd98f00b204e9800998ecf8427e'

Info: Creating state file /var/lib/puppet/state/state.yaml

Notice: Finished catalog run in 0.43 seconds

通过日志分析,Puppet-Server机器已经替名为fabric.cn7788.com 的节点机器自动颁发了签证,表示上述配置是生效的。

阅读 ‧ 电子书库

注意

自动颁发证书是一种比较危险的操作,除非有足够安全的保证,否则一般情况下不要这样操作。

另外,还有个问题:Puppet客户端应该如何自动连接Puppet-Master,并且如何修改默认连接时间间隔呢?

此时间间隔默认为1800秒(30分钟),有时候在实际工作中需要更改此时间间隔,比如将其更改为10分钟一次,下面以client.cn7788.com 的客户机为例说明具体的实现步骤。

首先,要修改它的/etc/puppet/puppet.conf文件,在

server=server.cn7788.com

runinterval=300

然后以服务的形式启动此Puppet Agent,命令如下:

service puppet start

在上面的配置文件里,server选项设置的是Puppet-Server的域名地址,在Puppet-Client端设置好此域名地址后,就可以以默认的30分钟为频率自动连接Puppet-Server了,在这里域名地址为server.cn7788.com 。其实配置了参数以后,Puppet-Client客户端就可以自动向Puppet-Master发起连接了,而不再需要用puppetd--test--serverserver.cn7788.com 命令进行手动连接。如果不指定此项参数,Puppet客户机将只是单纯地启动Puppetd进程,而不会主动找server.cn7788.com 发起请求。在实际应用中通常需要配置此选项,让客户端自动进行连接工作。runinterval选项可用于设置每隔多长的时间进行一次自动更新,时间单位为秒,这里选择的是300秒。

阅读 ‧ 电子书库

注意

一般来说,不要随意修改系统中默认配置的“30分钟”这个值,特别是不能更改成一个过小的数值,这会导致在客户端数量比较大的工作场景中,Puppet-Server因为响应不了频繁的连接而发生“timeout”的报错。

下面还是以client.cn7788.com 节点机器为例来验证下,大家可以观察下messages系统日志,命令如下所示:

tail –

f /var/log/messages

命令显示结果如下所示:

Nov 3 02:56:58 client puppet-agent[7882]: Applying configuration version '1446537314'

Nov 3 02:56:58 client puppet-agent[7882]: Finished catalog run in 0.38 seconds

Nov 3 22:11:28 client puppet-agent[8340]: Reopening log files

Nov 3 22:11:29 client puppet-agent[8340]: Starting Puppet client version 3.8.3

Nov 3 22:11:37 client puppet-agent[8343]: Finished catalog run in 0.32 seconds

Nov 3 22:12:57 client puppet-agent[8340]: Caught TERM; storing stop

Nov 3 22:12:58 client puppet-agent[8340]: Processing stop

Nov 3 22:13:00 client puppet-agent[8497]: Reopening log files

Nov 3 22:13:01 client puppet-agent[8497]: Starting Puppet client version 3.8.3

Nov 3 22:13:07 client puppet-agent[8500]: Finished catalog run in 0.10 seconds

Nov 3 22:18:07 client puppet-agent[8635]: Finished catalog run in 0.40 seconds

Nov 3 22:23:05 client puppet-agent[8766]: Finished catalog run in 0.10 seconds

Nov 3 22:28:06 client puppet-agent[8897]: Finished catalog run in 0.35 seconds

可以清楚地看到,Puppet-Client每次自动连接的时间间隔都是5分钟,即300秒,从而验证了上面的配置是正确的。