Puppet 3.0资源介绍

简介:

Puppet 3.0资源介绍

一,puppet资源:

Puppet将软件安装,文件配置,服务管理,用户添加,定时计划配置等抽象成资源。

二,资源类型:

1file管理文件,通过一定的配置也可以管理目录

2package管理软件包,会将软件自动安装

3service 管理服务,设置服务运行还是停止

4cron 设置定时任务

5user添加用户

6group添加组

7exec如果资源不能满足任务时,可以通过与shell 命令交互来完成

8notify资源之间通信

三,资源语法格式:

Type { title:

Attribute =>a,

Attribute =>b,

}

Type 就是上面的资源类型file……,“title”为了标识这个资源,attribute属性,如果权限,用户之类。

四,资源使用

1,应用于class中,完成某一个具体的应用

2,应用于site.pp中,作为全局配置

五,资源详解

1file

对文件:有用户,用户组,权限,链接文件,内容

对目录:用户,用户组,权限,清理目录及子目录。

2file资源属性

File { absolute path:

Ensure => present/absent/file/directory/link ,

#定义absolute path是否存在及它应该有哪些角色

#ensure定义资源处于什么状态,present表示如果client不存在则自动创建,absent表示如果client端存在这个文件,则删除,file表示文件,directory表示目录,link表示链接

User =>username,

Group => groupname,

Mode => 0777,

#四位八进制

Path=>absolute path,

#如果在title指定绝对路径了,在这可以省略,提高处理效率。

Soure => puppet:///url,

#指定到master上文件的路径或者client上本地文件路径

Content=>content,

#指定文件内容的字符串,也可以是模板或者输出内容的函数,模版常用

Target =>

#用于链接文件,创建链接文件时的目标文件

Recurse =>

#对应目录,递归

Purge =>

#对于没有定义的目录或文件,不让其存在这目录文件中。清理作用

}

}

其他的属性

Backup,checksum,force,igore,links,provider,recurselimit,replace,selrange,selrole,seltype,seltype,seluser,sourceselect,type

3flie资源的应用

/etc/puppetmanifestsclient的入口,当client来请求资源来,从manifests请求。在manifests下的site.pp是必须存在的。所有的客户端需要的资源都在这里定义。如果服务器过多,肯定会造成这个文件庞大无比,解决办法是给客户端单独新建一个文件,将资源属性定义到这,然后在site.pp import将其文件包括进来就行了。如果一台服务器有许多服务时,可以为这台服务器在manifests下新建一个目录,然后通过import  node/*node目录的文件都包含进来

[root@master manifests]# touch site.pp  需要手工创建

[root@master manifests]# ls

Site.pp

[root@master manifests]# touch node.pp

[root@master manifests]# echo "import node.pp" >> site.pp

4,实例

1)文件

[root@master manifests]# vim node.pp

file { "/etc/ssh/sshd_config":

       ensure=> present,

       owner => root,

       group => root,

       mode => 0600,

}

[root@client ~]# mv /etc/ssh/sshd_config /root

[root@client puppet]# puppet agent --verbose --no-daemonize 下面表示成功

Notice: Starting Puppet client version 3.2.3

Info: Retrieving plugin

Info: Caching catalog for client

Info: Applying configuration version '1375107271'

Notice: /Stage[main]//File[/etc/ssh/sshd_config]/ensure: created

Notice: Finished catalog run in 0.04 seconds

2)目录

[root@master manifests]# vim node.pp

file { "/etc/ssh/sshd_config":

       ensure=> directory,

       owner => root,

       group => root,

       mode => 0600,

recurse =>true,

purge => true,

#如果在/test下目录不在资源管理的范围,则会将其删除,必须加下面的force才有效,删除目录需要配合force,删除文件则不需要

force =>true,

igore => test.txt #表示test.txt将忽略,不会被删除。

}

[root@master manifests]# vim node.pp /etc/puppet/puppet.conf创建链接文件,链接名为/test/test

file { "/test":

       ensure=> directory,

       owner => root,

       group => root,

       mode => 0600,

       recurse => true,

       purge => true,

       force => true,

}

file { "/test/test":

       ensure => link,

       target => "/etc/puppet/puppet.conf",

}

~  

[root@client puppet]# puppet agent --verbose --no-daemonize  查看有没有创建

[root@master manifests]# vim site.pp 定义不同节点的资源,也可以这样写

node "client1" {

       file { "/test_client1":

               ensure => present,

       }

node "client2" {

       file { "/test_client2":

               ensure => present,

       }

[root@client puppet]# puppet agent --test也可以实现上面--verbose

六,Package资源属性

Package { package name:

Ensure =>present/absent/latest/{version}/purged,

#present 只要存在即可

#absent 无依赖可删除,如果有依赖会报错,并不删除,只能删除没有依赖的软件包

#lastest 会升级到最新版本

#version 指定安装具体某个版本

#purged 删除该包包括有依赖性的包,如果有依赖,则依赖包也会被删除,使用有风险,少使用

Name => package name,

#titile里指定了这里可以省略

}

实例

vim site.pp

node "client" {

       file { "/test_client":

               ensure => present,

       }

       package { "httpd":

               ensure => present,

       }

[root@client ~]# puppet agent --test

Info: Retrieving plugin

Info: Caching catalog for client

Info: Applying configuration version '1375177502'

Notice: /Stage[main]//Node[client]/File[/test_client]/ensure: created

Notice: /Stage[main]//Node[client]/Package[httpd]/ensure: created

Notice: Finished catalog run in 42.28 seconds

vim site.pp

node "client" {

       file { "/test_client":

               ensure => present,

       }

       package { "openldap":

               ensure => absent,

       }

[root@client ~]# puppet agent --test

Info: Retrieving plugin

Info: Caching catalog for client

Info: Applying configuration version '1375177769'

Error: Execution of '/bin/rpm -e openldap-2.4.23-26.el6.x86_64' returned 1: error: Failed dependencies:

liblber-2.4.so.2()(64bit) is needed by (installed) cyrus-sasl-2.1.23-13.el6.x86_64

liblber-2.4.so.2()(64bit) is needed by (installed) postfix-2:2.6.6-2.2.el6_1.x86_64

liblber-2.4.so.2()(64bit) is needed by (installed) sudo-1.7.4p5-11.el6.x86_64

liblber-2.4.so.2()(64bit) is needed by (installed) apr-util-ldap-1.3.9-3.el6_0.1.x86_64

七,Service资源

Service  {package name:

Ensure =>running(true)/stopped(false),

Enable =>true/false,

#enable确保资源开机启动还是不启动

Status,start,stop,restart=>command,

#如果不支持标准的命令时,如service xxx start,表示标准的,一些编译的软件,不支付标准的,而要查询资源的属性时,需要执行命令,command表示命令行

Hasrestart => true/false,

#true表示直接执行restart命令重启服务,false表示先对服务stop,start

Hasstatus =>true/false,

#表示在进程查看服务的状态

}

实例

Vim site.pp

node "client" {

       file { "/test_client":

               ensure => present,

       }

       package { "openldap":

               ensure => present,

       }

       service { "httpd":

       ensure => running,

       }

}

#node 是一个关键字,指定哪台客户端client是客户端主机名,{}写上需要在client执行的作业

#由于httpd支持标准启动,所以可以不需要定义start =>command

[root@client ~]# puppet agent --test

Info: Retrieving plugin

Info: Caching catalog for client

Info: Applying configuration version '1375179204'

Notice: /Stage[main]//Node[client]/Service[httpd]/ensure: ensure changed 'stopped' to 'running'

Info: /Stage[main]//Node[client]/Service[httpd]: Unscheduling refresh on Service[httpd]

Notice: Finished catalog run in 0.42 seconds

httpd移到到/root目录下,此时不再支持service来启动,需要指定路径/root/httpd start,那么此时就需要定义start =>command

[root@client ~]# service  httpd stop

Stopping httpd:                                            [  OK  ]

[root@client ~]# mv /etc/init.d/httpd  .

Vim site.pp

node "client" {

       file { "/test_client":

               ensure => present,

       }

       package { "openldap":

               ensure => present,

       }

       service { "httpd":

       ensure => running,

Start => /root/httpd start,

       }

}

[root@client ~]# puppet agent --test

Info: Retrieving plugin

Info: Caching catalog for client

Info: Applying configuration version '1375179610'

Notice: /Stage[main]//Node[client]/Service[httpd]/ensure: ensure changed 'stopped' to 'running'

Info: /Stage[main]//Node[client]/Service[httpd]: Unscheduling refresh on Service[httpd]

Notice: Finished catalog run in 0.32 seconds

Vim site.pp

node "client" {

       file { "/test_client":

               ensure => present,

       }

       package { "openldap":

               ensure => present,

       }

       service { "httpd":

       ensure => running,

#start => /root/httpd start,

restart => /root/httpd restart,

hasrestart =>true,

#hasrestart 要服务支持标准的启动,如果不能则需要将上面的start改成restart

       }

}

八,管理ssh服务

1ssh相关软件包的安装

2,配置文件的管理

3,服务运行管理

[root@master puppet]# puppet master --configprint modulepath

/etc/puppet/modules:/usr/share/puppet/modules

查看模块配置文件的路径,这么两个路径的模块都可以用puppet实现

[root@master modules]# mkdir  -p  ssh/{files,manifests,templates}

创建ssh模块,需要在modules建立ssh,并在下面建立上面三个文件夹。模块都需要这三个目录。

Files放一些静态文件,如果配置文件之类

Manifests存放一些代码,必须有init.pp这个文件,就像site.pp文件一样,必须存在。

Templates files存放的文件不能完成要求时,可以在这个目录下定义一些模板,让其引用。

[root@master modules]# vim ssh/manifests/init.pp

class ssh {

       package { "openssh-server":

               ensure => present,

               }

       file { "/etc/ssh/sshd_config":

               ensure => present,

               owner => root,

               group => root,

               mode =>0600,

               source =>"puppet:///modules/ssh/sshd_config",

#class关键字,表示定义ssh模块

#source的作用是当客户端的sshd_configsource定义的sshd_config内容不一样时,将其下载下来,并覆盖自己的配置文件。

#puppet://表示采用puppet协议,modules表示连接到modulepath 下的路径中找,而modulespath,有两个路径,如果在/etc/puppet/modules中找不到则到/usr/share/puppet/modules下找。如果两个目录都找不到,则会报错。

[root@master puppet]# puppet master --configprint modulepath

/etc/puppet/modules:/usr/share/puppet/modules

#ssh表示模块名字,不需要再指定files,因为模块下面这三个目录是固定的。

#sshd_config表示到files下找这个文件。

               notify => service ['sshd'],

#如果files资源有什么变动则通知下面的service资源。

               }

       service { "sshd":

               ensure =>running,

               enables => true,

               hasrestart =>true,

               hasstatus =>true,

               }

}

[root@master modules]# cp /etc/ssh/sshd_config  ssh/files/

[root@master puppet]# echo > manifests/site.pp

[root@client ~]# puppet agent --test










本文转自 deng304749970 51CTO博客,原文链接:http://blog.51cto.com/damondeng/1261807,如需转载请自行联系原作者
目录
相关文章
|
关系型数据库 MySQL Shell
puppet连载24:虚拟资源
class account::virtual { @user {"mysql": ensure => present, uid => 27, gid => 27, home => "/var/lib/mysql", shell => "/bin/bas...
750 0
puppet连载17:file资源详解
file {'/tmp/filetest': content => "aaa", ensure => file, } ensure => present file {'/tmp/filetest': content => "aaa", } file {...
851 0
|
Web App开发 Ubuntu 应用服务中间件
|
应用服务中间件 nginx Linux