首页 > 其他 > 详细

PUPPET基础

时间:2017-12-17 20:39:24      阅读:384      评论:0      收藏:0      [点我收藏+]
对于puppet,玩过ansible的人对puppet来说都不陌生。puppet是一种Linux、Unix、windows平台的集中配置管理系统,使用自有的puppet描述语言,可管理配置文件、用户、cron任务、软件包、系统服务等。puppet把这些系统实体称之为资源,puppet的设计目标是简化对这些资源的管理以及妥善处理资源间的依赖关系。

对于ansible来说puppet更为抽象了,且配置管理方面(playbook)更是puppet更为厉害(毕竟puppet时间这么长了)。但是自我感觉自动化运维工具还是用得顺手在第一,不顺手啥事都干不好,且puppet的学习要比ansible要难一点。所以子自行选择吧!

puppet有两种模式,单机和master/agent:单机就是自己跟自己玩了,在本机配置在本机实现操作。master/agent就是远控其他主机了。

现在呢来说说puppet的单机基本用法,单机模式下的简单的使用和配置。(先把单机模式下的操作玩熟练了,在去玩多台主机吧)

首先最简单的查看资源 : puppet describe -l  #describe 描述  -l list  查看资源列表

[root@node1 ~]# puppet describe -l                                                 
These are the types known to puppet:
augeas      - Apply a change or an array of changes to the  ...
compute     - Computer object management using DirectorySer ...
cron      - Installs and manages cron jobs
exec      - Executes external commands
file      - Manages files, including their content, owner ...
filebucket   - A repository for storing and retrieving file  ...
group     - Manage groups
host      - Installs and manages host entries
interface   - This represents a router or switch interface
k5login    - Manage the `.k5login` file for a user
macauthorization - Manage the Mac OS X authorization database
mailalias   - .. no documentation ..
maillist   - Manage email lists
mcx      - MCX object management using DirectoryService  ...
mount     - Manages mounted filesystems, including puttin ...

学会了如何查看资源列表,这么多资源怎么使用呢,今天学几个重要的常用的0.0

但是仅仅学会了查看列表还是太嫩了,若需要用到file这个资源,怎么知道他的用法呢。

[root@rootdns ~]# puppet describe file  #下面还有各种参数的用法,只截取一部分
.........................

Parameters
----------

.....................

- **checksum**
  The checksum type to use when determining whether to replace a file's
  contents.
  The default checksum type is md5.
  Valid values are `md5`, `md5lite`, `sha256`, `sha256lite`, `mtime`,
  `ctime`, `none`. 
......................

puppet describe [资源名] 可以查看某个资源的用法。知道用法了下面就就看看怎么使用格式是什么。

1.首先看第一个资源group。很简单但的一个资源: #这里开始使用本机执行命令了apply 后面跟上文件名(注意,要.pp结尾最好)

[root@rootdns pupp]# cat group.pp 
group{'meng':
    name => 'meng',
    ensure => present,
    gid => '2000',
}
[root@rootdns pupp]# 
[root@rootdns pupp]# puppet apply group.pp          
Notice: Compiled catalog for rootdns in environment production in 0.11 seconds
Notice: /Stage[main]/Main/Group[meng]/ensure: created
Notice: Finished catalog run in 0.13 seconds
[root@rootdns pupp]# cat /etc/group |grep 2000
meng:x:2000:
[root@rootdns pupp]#   #这里创建了一个gid为2000的组

关于组的命令这只是一个简单的创建,更多的用法可以用describe查看。

查看什么呢,这个文件内容:开头group为资源名其他什么符号啊括号啊都是固定格式,下面的name什么的为参数,就group而言需要定义一个组,组号为多少组名为什么这都需要参数定义,而开头group只是说明要定义组!

若只想查看某个资源有哪些参数,并不想查看具体用法可以加上 -m原参数(大部分用不到,但还是有地方能用到的) -s短格式查看

[root@rootdns pupp]# puppet describe group  -s -m

group
=====
Manage groups. On most platforms this can only create groups.
Group membership must be managed on individual users.
On some platforms such as OS X, group membership is managed as an
attribute of the group, not the user record. Providers must have
the feature 'manages_members' to manage the 'members' property of
a group record.

Parameters
----------
  allowdupe, attribute_membership, attributes, auth_membership, ensure,
  forcelocal, gid, ia_load_module, members, name, system

Meta Parameters
---------------
  alias, audit, before, loglevel, noop, notify, require, schedule, stage,
  subscribe, tag

Providers
---------
  aix, directoryservice, groupadd, ldap, pw, windows_adsi
[root@rootdns pupp]#

#Parameters下面都是可以定义的参数,先查看参数在查看用法,一般流程都是这样的。这里说明一点name一般若不定义就继承group{'meng':}    文件内这里的红色内容。(特殊参数)


2.user :用户

[root@rootdns pupp]# vim user.pp           
user{'meng':
   name => 'meng',   
   uid => 2000,
   shell => '/bin/csh',
   managehome => true,
}                                                                                                                                                                                                                                                                                                       
"user.pp" 7L, 90C written                                                                                                                       
[root@rootdns pupp]# 
[root@rootdns pupp]# puppet apply  user.pp 
Notice: Compiled catalog for rootdns in environment production in 0.12 seconds
Notice: /Stage[main]/Main/User[meng]/ensure: created
Notice: Finished catalog run in 0.35 seconds
[root@rootdns pupp]# id meng
uid=2000(meng) gid=2000(meng) groups=2000(meng)
[root@rootdns pupp]#

#指定用户名 name   uid  指定shell类型  是否创建家目录managehome等操作可以查看帮助,比如截取一段关于user参数的使用说明:

#这里说明了system可选的为true等等这几个选项,默认小于五百为系统组,centos6和7不同。默认为false不为系统组。
- **system**
  Whether the user is a system user, according to the OS's criteria;
  on most platforms, a UID less than or equal to 500 indicates a system
  user. Defaults to `false`.
  Valid values are `true`, `false`, `yes`, `no`.


3.package   关于程序包  #在linux平台最多的就是操作管理服务了下面就介绍关于包和服务的命令。

[root@rootdns pupp]# vim pak.pp 
package{'nginx':
  ensure => latest,
  provider => yum,

}
~                                                                                                                                                                 
"pak.pp" 5L, 57C written
[root@rootdns pupp]# puppet apply pak.pp 
Notice: Compiled catalog for rootdns in environment production in 0.42 seconds
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/share/ruby/vendor_ruby/puppet/type.rb:816:in `set_default')

Notice: /Stage[main]/Main/Package[nginx]/ensure: created
Notice: Finished catalog run in 3.68 seconds
[root@rootdns pupp]# 
[root@rootdns pupp]#~         #上面有个warning并不影响

  上面ensure为是否安装latest为安装最新版  present为安装 absent为卸载

  provider定义安装方式:为yum,因为平台的不同可能命令不同。这里可以加上name => nginx  ,不加因为nginx包名从上面继承了。

  source:程序包来源

             

在说明这个资源之前想要先表示表示,也就是说明一下依赖问题。下面即将说明管理服务,安装过nginx后,怎么通过puppet来启动重启关闭呢。想要重启关闭但是还没装服务怎么办呢,这里就要执行两个资源动作,且还要定义先后。  大致是:安装 --> 启动

4.service  服务

[root@rootdns pupp]# puppet describe service  
#中间省略了.....................
- **ensure**
  Whether a service should be running.
  Valid values are `stopped` (also called `false`), `running` (also called
  `true`). 
  #这里表示,启动running也叫true  stoped也叫false
    
[root@rootdns pupp]# vim pak.pp          
package{'nginx':
    ensure => latest,
    provider => yum,
} ->

service{'nginx':
    ensure => running,
}
~

 继续定义service且参数ensure为running在继续puppet apply pak.pp执行一遍可以查看nginx端口已经开启。不知道你们观察看了没我在两个资源中间加了一个“->",这是什么意思呢,意思为后面的资源要依赖于前面的资源 ,前面一定要先于后面执行。

 三种依赖解决:

 1.简单的  ->

 技术分享图片

 #这只是简单的加个符号记住就行了,好使。

 2.Package[‘redis’] -> File []->Service[]

 技术分享图片

 #在所有资源下面做一个排序

 3.service[‘redis’:   #在内部定义   require => Package['nginx']

 技术分享图片

 在资源内定义两种方法,任意一个就行,上面一行我注释了。before表示在某资源之前执行,require表示依赖某资源也就是在什么自愿执行过后在执行。这表示三种解决以来的方法,还是比较容易理解的。

 好了继续说回service,service可以控制服务的启动重启等功能,配合上面的安装来批量管理服务,进行安装和启动,可以设置开启自动启动。

5.file  文件

 文件有很多属性比如创建文件,在linux中目录也是文件的一种,所以创建文件在linux中有三种情况,创建文件,目录,连接。

 这里ensure可以指定创建文件或者目录

技术分享图片

 #这里看到文件内容为nginx配置文件内容    file后面指定文件名   ensure为file(文件)类型,指定源文件这样形成一套复制的代码。

 也可以输入内容到文件,不指定源文件。

技术分享图片

 创建目录,且复制其他目录内的内容到创建的目录下。

  技术分享图片 

  #注意,若源是个文件则会失败,不能把文件复制成目录。

  技术分享图片

   #链接也需要指定源和目标名

  6.exec   命令的执行

  技术分享图片

  #这里使用命令创建要指定命令的路径还有判断该目录是否存在。

 技术分享图片

  unless可以根据判断是否为0,即输出为true来决定资源定义是否要执行。上面则判断id是否存在,存在不创建,不存在创建。

  其他还有许多的资源,这里就不一一细说了,这个东西就是这样使用的多了自然就会了。更多的资源还需要多自己尝试一下才知道怎么使用。

        



     

PUPPET基础

原文:http://blog.51cto.com/13154101/2051543

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!