本文参考了刘宇的《puppet实战》,并得到他的指导。在此,感谢刘宇。
前一篇文章提到,利用Puppet来给客户端自动生成在服务端定义好的文件。
我们这里继续引申,来从服务端定义客户端需要安装的软件。
这里我们以安装httpd举例。
在服务端,需要定义模块,我们定义一个httpd模块,名字就叫httpd。
目录结构如下:
puppet/
|-- auth.conf
|-- environments
| `-- example_env
| |-- README.environment
| |-- manifests
| `-- modules
|-- fileserver.conf
|-- manifests
| |-- nodes
| | `-- cahou.linux.net.pp
| `-- site.pp
|-- modules
| |-- httpd
| | |-- files
| | |-- mainfests
| | | `-- init.pp
| | `-- templates
| `-- test
| |-- files
| |-- manifests
| | `-- init.pp
| `-- templates
| `-- test.erb
`-- puppet.conf
15 directories, 9 files
我们这样来建立目录和所需的配置文件:
来看看puppet客户端和服务端的交互过程:
puppet客户端将节点名称和facts信息发送给master
puppet服务端master通过分类判断请求的客户端是谁,它要做什么?这个判断依赖于site.pp中包含的node.pp配置文件完成
puppet服务端将客户端所学的class信息编译到catalog里面去,并把它发送给客户端
客户端对catalog进行验证并执行
客户端通过api把执行结果回报给master
site.pp这个文件是指引去哪里寻找并加载所有主机相关的配置。
这个文件默认放在 /etc/puppet/mainfests中,在这个文件里面我们会定义一些全局变量。
而在site.pp里面可以引用其他模块定义的*.pp文件,以让文件结构显得清晰。
需要提供给客户端的配置信息可以任意定义,只要在site.pp里面import对应的配置文件即可。
我们采用的目录结构配置是:
/etc/puppet/manifests/nodes
/etc/puppet/modules
#下面我们建立了一个叫做test的模块和叫做cahou.linux.net的节点
mkdir -p /etc/puppet/manifests/nodes
mkdir -p /etc/puppet/modules/test/{mainfests,templates,files}
vi /etc/puppet/modules/test/manifests/init.pp
class test
{
file { "/tmp/$hostname.txt": content => "hello,world 1234567890!\n"; }
}
#在上面的模块中配置了一个变量,叫做$hostname,这个变量需要传给ERB文件,这个文件就放在test类这个目录下的templates目录中,文件名与类名一致。
cat /etc/puppet/modules/test/templatest/test.erb
hostname <% fqdn %>
在上面的步骤里,我们已经创建了模块test,现在我们需要创建测试节点
cat /etc/puppet/manifests/nodes/cahou.linux.net.pp
node 'cahou.linux.net' {
include test
}
到此,节点信息、模块信息均已创建。
前面讲过,puppet会首先读取site.pp,我们需要让site.pp能导入节点信息和模块信息
vi /etc/puppet/manifests/site.pp
cat site.pp
Package {provider => "yum"}
node default { file { "/tmp/puppettest1.txt": content => "hello,first puppet test1234567890!\n";}}
import "/etc/puppet/modules/httpd/mainfests/init.pp"
import "/etc/puppet/manifests/nodes/*.pp"
至此,客户端可以来向服务端取信息了,我们可以采用模拟执行的方式,这样,可以看到是否能执行(但并不真正执行)
puppet agent --server cahoa.linux.net --test --verbose --no-daemonize --noop
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for cahou.linux.net
Warning: The package type's allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false.
(at /usr/lib/ruby/site_ruby/1.8/puppet/type/package.rb:430:in `default')
Info: Applying configuration version '1413640549'
Notice: Finished catalog run in 0.38 seconds
上面的warning信息,我们暂且不理会,可以看到,已经取到配置信息了。
下面我们给它加个功能,让服务端给客户端安装httpd。
为此,我们要添加一个模块,
mkdir -p /etc/puppet/modules/httpd/{mainfests,templates,files}
编辑httpd模块文件,在里面指定yum源
cat /etc/puppet/modules/httpd/mainfests/init.pp
class httpd {
yumrepo
{ "reposohu":
descr => "sohu repo",
baseurl => "http://mirrors.sohu.com/centos/5.11/os/i386/",
gpgcheck => "0",
enabled => "1";
}
package
{
"httpd":
ensure => installed,
require => Yumrepo["reposohu"];
}
}
修改节点信息,添加引用httpd模块:
cat /etc/puppet/manifests/nodes/cahou.linux.net.pp
node 'cahou.linux.net' {
include test
include httpd
}
至此,客户端可以通过向服务端同步信息来安装httpd了。
[root@cahou ~]# puppet agent --server cahoa.linux.net --test --no-daemonize
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for cahou.linux.net
Warning: The package type's allow_virtual parameter will be changing its default value from false to true in a future release. If you do not want to allow virtual packages, please explicitly set allow_virtual to false.
(at /usr/lib/ruby/site_ruby/1.8/puppet/type/package.rb:430:in `default')
Info: Applying configuration version '1413640549'
Notice: /Stage[main]/Httpd/Package[httpd]/ensure: created
Notice: Finished catalog run in 61.91 seconds
[root@cahou ~]# rpm -q httpd
[root@cahou ~]# ll -c /etc/httpd/conf/httpd.conf
-rw-r--r-- 1 root root 33726 Oct 18 23:54 /etc/httpd/conf/httpd.conf
#从上面的httpd.conf的ctime信息,我们可以看到,这正是刚刚安装的httpd。
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com