Linux自动化运维之puppet2.7&3.7安装和常用类型详解

摘要

puppet是一种Linux、Unix、windows平台的集中配置管理系统,使用自有的puppet描述语言,可管理配置文件、用户、cron任务、软件包、系统服务等。puppet把这些系统实体称之为资源,puppet的设计目标是简化对这些资源的管理以及妥善处理资源间的依赖关系。

1、puppet_2.7_安装

[[email protected] ~]# cd /tmp/
[[email protected] /tmp]# wget -q http://www.dwhd.org/wp-content/uploads/2015/06/facter-1.7.5-1.el6_.x86_64.rpm
[[email protected] /tmp]# wget -q http://www.dwhd.org/wp-content/uploads/2015/06/puppet-2.7.25-1.el6_.noarch.rpm
[[email protected] /tmp]# wget -q http://www.dwhd.org/wp-content/uploads/2015/06/puppet-dashboard-1.2.23-1.el6_.noarch.rpm
[[email protected] /tmp]# wget -q http://www.dwhd.org/wp-content/uploads/2015/06/puppet-server-2.7.25-1.el6_.noarch.rpm
[[email protected] /tmp]# ls -l | grep rpm
-rw-r--r--  1 root root   88800 6月   8 21:16 facter-1.7.5-1.el6_.x86_64.rpm
-rw-r--r--  1 root root 1127972 6月   8 21:16 puppet-2.7.25-1.el6_.noarch.rpm
-rw-r--r--  1 root root 4509032 6月   8 21:16 puppet-dashboard-1.2.23-1.el6_.noarch.rpm
-rw-r--r--  1 root root   25620 6月   8 21:16 puppet-server-2.7.25-1.el6_.noarch.rpm
[[email protected] /tmp]# yum install facter-1.7.5-1.el6_.x86_64.rpm puppet-2.7.25-1.el6_.noarch.rpm puppet-server-2.7.25-1.el6_.noarch.rpm -y
[[email protected] /tmp]# rpm -qc puppet
/etc/logrotate.d/puppet
/etc/puppet/auth.conf
/etc/puppet/puppet.conf
/etc/puppet/puppetca.conf
/etc/puppet/puppetd.conf
/etc/sysconfig/puppet
[[email protected] /tmp]# 

2、简单测试puppet单击模型上安装软件

[[email protected] /tmp]# cat > first.pp << EOF
package {'nginx':
        ensure  => present,
}
EOF
[[email protected] /tmp]# puppet apply first.pp
warning: Could not retrieve fact fqdn
notice: /Stage[main]//Package[nginx]/ensure: created
notice: Finished catalog run in 15.24 seconds
[[email protected] /tmp]# rpm -q nginx
nginx-1.0.15-11.el6.x86_64 #可以发现这时候我们的nginx就安装好了
[[email protected] /tmp]# 

Linux自动化运维之puppet2.7&3.7安装和常用类型详解
我们可以卸载然后重新安装试试

[[email protected] /tmp]# rpm -e nginx
[[email protected] /tmp]# rpm -q nginx
package nginx is not installed
[[email protected] /tmp]# puppet apply first.pp
warning: Could not retrieve fact fqdn
notice: /Stage[main]//Package[nginx]/ensure: created
notice: Finished catalog run in 4.09 seconds
[[email protected] /tmp]# rpm -q nginx
nginx-1.0.15-11.el6.x86_64
[[email protected] /tmp]# 

Linux自动化运维之puppet2.7&3.7安装和常用类型详解


 

3、puppet_3.7_安装

[[email protected] ~]# cd /tmp/
[[email protected] /tmp]# wget -q http://www.dwhd.org/wp-content/uploads/2015/06/facter-1.7.6-1.el6_.x86_64.rpm
[[email protected] /tmp]# wget -q http://www.dwhd.org/wp-content/uploads/2015/06/facter-2.2.0-1.el6_.x86_64.rpm
[[email protected] /tmp]# wget -q http://www.dwhd.org/wp-content/uploads/2015/06/hiera-1.3.4-1.el6_.noarch.rpm
[[email protected] /tmp]# wget -q http://www.dwhd.org/wp-content/uploads/2015/06/mcollective-2.6.0-1.el6_.noarch.rpm
[[email protected] /tmp]# wget -q http://www.dwhd.org/wp-content/uploads/2015/06/mcollective-client-2.6.0-1.el6_.noarch.rpm
[[email protected] /tmp]# wget -q http://www.dwhd.org/wp-content/uploads/2015/06/mcollective-common-2.6.0-1.el6_.noarch.rpm
[[email protected] /tmp]# wget -q http://www.dwhd.org/wp-content/uploads/2015/06/puppet-3.7.1-1.el6_.noarch.rpm
[[email protected] /tmp]# wget -q http://www.dwhd.org/wp-content/uploads/2015/06/puppet-dashboard-1.2.23-1.el6_.noarch1.rpm
[[email protected] /tmp]# wget -q http://www.dwhd.org/wp-content/uploads/2015/06/puppet-server-3.7.1-1.el6_.noarch.rpm
[[email protected] /tmp]# ls -l | grep .*.rpm
-rw-r--r--  1 root root   88860 6月   9 09:58 facter-1.7.6-1.el6_.x86_64.rpm
-rw-r--r--  1 root root   98384 6月   9 09:58 facter-2.2.0-1.el6_.x86_64.rpm
-rw-r--r--  1 root root   23856 6月   9 09:58 hiera-1.3.4-1.el6_.noarch.rpm
-rw-r--r--  1 root root   11204 6月   9 09:58 mcollective-2.6.0-1.el6_.noarch.rpm
-rw-r--r--  1 root root   27644 6月   9 09:58 mcollective-client-2.6.0-1.el6_.noarch.rpm
-rw-r--r--  1 root root  792568 6月   9 09:58 mcollective-common-2.6.0-1.el6_.noarch.rpm
-rw-r--r--  1 root root 1634996 6月   9 09:58 puppet-3.7.1-1.el6_.noarch.rpm
-rw-r--r--  1 root root 4509032 6月   9 09:58 puppet-dashboard-1.2.23-1.el6_.noarch1.rpm
-rw-r--r--  1 root root   24644 6月   9 09:58 puppet-server-3.7.1-1.el6_.noarch.rpm
[[email protected] /tmp]# 

Linux自动化运维之puppet2.7&3.7安装和常用类型详解

[[email protected] /tmp]# yum install facter-1.7.6-1.el6_.x86_64.rpm puppet-3.7.1-1.el6_.noarch.rpm puppet-server-3.7.1-1.el6_.noarch.rpm -y
[[email protected] /tmp]# mkdir puppet
[[email protected] /tmp]# cd puppet
[[email protected] /tmp/puppet]# cat >first.pp << EOF
package {'nginx':
        ensure => present,
}
EOF
[[email protected] /tmp/puppet]# puppet apply -d -v --test first.pp #-d debug -v详细信息 --test只是测试不真正应用(由于这里测试的是单机模型所以还是会安装的)
........
Notice: /Stage[main]/Main/Package[nginx]/ensure: created
Debug: /Stage[main]/Main/Package[nginx]: The container Class[Main] will propagate my refresh event
Debug: Class[Main]: The container Stage[main] will propagate my refresh event
Debug: Finishing transaction 70310119639420
Debug: Storing state
Info: Creating state file /var/lib/puppet/state/state.yaml
Debug: Stored state in 0.01 seconds
Notice: Finished catalog run in 15.10 seconds
Debug: Using settings: adding file resource 'rrddir': 'File[/var/lib/puppet/rrd]{:group=>"puppet", :loglevel=>:debug, :ensure=>:directory, :links=>:follow, :owner=>"puppet", :mode=>"750", :backup=>false, :path=>"/var/lib/puppet/rrd"}'
Debug: /File[/var/lib/puppet/rrd]/ensure: created
Debug: Finishing transaction 70310119049620
Debug: Received report to process from legion100
Debug: Processing report from legion100 with processor Puppet::Reports::Store
[[email protected] /tmp/puppet]# rpm -q nginx
nginx-1.0.15-11.el6.x86_64
[[email protected] /tmp/puppet]# puppet describe -l #列出puppet所有可用资源项
These are the types known to puppet:
augeas          - Apply a change or an array of changes to the  ...
computer        - 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 ...
nagios_command  - The Nagios type command
nagios_contact  - The Nagios type contact
nagios_contactgroup - The Nagios type contactgroup
nagios_host     - The Nagios type host
nagios_hostdependency - The Nagios type hostdependency
nagios_hostescalation - The Nagios type hostescalation
nagios_hostextinfo - The Nagios type hostextinfo
nagios_hostgroup - The Nagios type hostgroup
nagios_service  - The Nagios type service
nagios_servicedependency - The Nagios type servicedependency
nagios_serviceescalation - The Nagios type serviceescalation
nagios_serviceextinfo - The Nagios type serviceextinfo
nagios_servicegroup - The Nagios type servicegroup
nagios_timeperiod - The Nagios type timeperiod
notify          - .. no documentation ..
package         - Manage packages
resources       - This is a metatype that can manage other reso ...
router          - .. no documentation ..
schedule        - Define schedules for Puppet
scheduled_task  - Installs and manages Windows Scheduled Tasks
selboolean      - Manages SELinux booleans on systems with SELi ...
selmodule       - Manages loading and unloading of SELinux poli ...
service         - Manage running services
ssh_authorized_key - Manages SSH authorized keys
sshkey          - Installs and manages ssh host keys
stage           - A resource type for creating new run stages
tidy            - Remove unwanted files based on specific crite ...
user            - Manage users
vlan            - .. no documentation ..
whit            - Whits are internal artifacts of Puppet's curr ...
yumrepo         - The client-side description of a yum reposito ...
zfs             - Manage zfs
zone            - Manages Solaris zones
zpool           - Manage zpools
[[email protected] /tmp/puppet]# puppet describe notify

notify
======
Sends an arbitrary message to the agent run-time log.

Parameters
----------

- **message**
    The message to be sent to the log.

- **name**
    An arbitrary tag for your own reference; the name of the message.

- **withpath**
    Whether to show the full object path. Defaults to false.
    Valid values are `true`, `false`.
[[email protected] /tmp/puppet]# 
[[email protected] /tmp/puppet]# cat >notify.pp << EOF
notify {'message':
        message => "Hello world!!!",
}
EOF
[[email protected] /tmp/puppet]# puppet apply notify.pp
Warning: Could not retrieve fact fqdn
Notice: Compiled catalog for legion100 in environment production in 0.17 seconds
Notice: Hello world!!!
Notice: /Stage[main]/Main/Notify[message]/message: defined 'message' as 'Hello world!!!'
Notice: Finished catalog run in 0.05 seconds
[[email protected] /tmp/puppet]# 

Linux自动化运维之puppet2.7&3.7安装和常用类型详解


puppet常用类型:package、user、group、file、service、exec、cron、notify

notify {'title':

message => "something here",

}

package的常用属性:

ensure:present(安装)、installed(安装),absent(不能安装),latest(最新版本)

name:包名

source:包文件的路径(可以是本地文件系统路径,也可以是网络文件路径)

puppet://modules/MODULE_NAME/FILENAME

package {'mysql':

ensure => installed,#安装mysql

provider => msi,#Windows平台

source => "D:\mysql\mysql-5.5.45.msi",#安装源文件路径

install_options => {'INSTALLDIR' => 'C:\mysql'},#指明安装路径

}

file的常用属性

content:直接生成的文件内容;生成方式有三种content source target

ensure:确保文件存在性 present(文件必须存在),absent(不能存在),file,directory,link

source:通过指定的URL下载文件至目标位置:

target:符号链接文件的链接目标

owner:属主

group:属组

mode:权限

path:文件路径

force:强制删除,仅用于ensure为absent时

purge:清除指定目中存在的,但是在puppet中未定义的文件;

replace:是否替换已经存在的文件

[[email protected] /tmp/puppet]# cat file.pp
file {'test.txt':
        ensure  => present,
        content => "test text",
        path    => '/tmp/test.txt',
        mode    => '0600',
        owner   => 'root',
        group   => 'root',
}
[[email protected] /tmp/puppet]# puppet apply file.pp
Warning: Could not retrieve fact fqdn
Notice: Compiled catalog for legion100 in environment production in 0.21 seconds
Notice: /Stage[main]/Main/File[test.txt]/ensure: created
Notice: Finished catalog run in 0.09 seconds
[[email protected] /tmp/puppet]# ls -l /tmp/test.txt
-rw------- 1 root root 9 6月   9 11:19 /tmp/test.txt
[[email protected] /tmp/puppet]# 

Linux自动化运维之puppet2.7&3.7安装和常用类型详解

[[email protected] /tmp/puppet]# cat file2.pp
file {'test2.txt':
        ensure  => present,
        source  => '/etc/fstab',
        path    => '/tmp/test2.txt',
        mode    => '0600',
        owner   => 'root',
        group   => 'root',
}
[[email protected] /tmp/puppet]# puppet apply file2.pp
Warning: Could not retrieve fact fqdn
Notice: Compiled catalog for legion100 in environment production in 0.21 seconds
Notice: /Stage[main]/Main/File[test2.txt]/ensure: defined content as '{md5}0917baf8c5960cce5d37ef9b2cc76ce4'
Notice: Finished catalog run in 0.06 seconds
[[email protected] /tmp/puppet]# cat /tmp/test2.txt 

#
# /etc/fstab
# Created by anaconda on Sun May 31 04:24:10 2015
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vgzhongH-root /                       ext4    defaults,acl        1 1
UUID=d990ab04-4b45-4037-a568-24a3d084c086 /boot                   ext4    defaults        1 2
/dev/mapper/vgzhongH-data /data                   ext4    defaults,acl        1 2
/dev/mapper/vgzhongH-swap swap                    swap    defaults,acl        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[[email protected] /tmp/puppet]# ls -l /tmp/test2.txt
-rw------- 1 root root 863 6月   9 11:39 /tmp/test2.txt
[[email protected] /tmp/puppet]# 

Linux自动化运维之puppet2.7&3.7安装和常用类型详解

[[email protected] /tmp/puppet]# cat file3.pp
file {'/tmp/dir1':
        ensure  => directory,
        mode    => '0750',
}
[[email protected] /tmp/puppet]# puppet apply file3.pp
Warning: Could not retrieve fact fqdn
Notice: Compiled catalog for legion100 in environment production in 0.21 seconds
Notice: /Stage[main]/Main/File[/tmp/dir1]/ensure: created
Notice: Finished catalog run in 0.07 seconds
[[email protected] /tmp/puppet]# ls -dl /tmp/dir1
drwxr-x--- 2 root root 4096 6月   9 12:01 /tmp/dir1
[[email protected] /tmp/puppet]# 

Linux自动化运维之puppet2.7&3.7安装和常用类型详解

[[email protected] /tmp/puppet]# cat file4.pp
file {'/tmp/file.link':
        ensure  => link,
        target  => '/tmp/test.txt',
}
[[email protected] /tmp/puppet]# puppet apply file4.pp
Warning: Could not retrieve fact fqdn
Notice: Compiled catalog for legion100 in environment production in 0.21 seconds
Notice: /Stage[main]/Main/File[/tmp/file.link]/ensure: created
Notice: Finished catalog run in 0.06 seconds
[[email protected] /tmp/puppet]# ls -l /tmp/file.link
lrwxrwxrwx 1 root root 13 6月   9 12:04 /tmp/file.link -> /tmp/test.txt
[[email protected] /tmp/puppet]# 

Linux自动化运维之puppet2.7&3.7安装和常用类型详解

service的常用属性:

enable:是否开机自动启动的

ensure:

ture/running

false/stopped

name:服务名称

path:服务脚本路径

restart:指明当我们打算restart时替代为一个别的命令譬如reload

start:

stop:

status:

[[email protected] /tmp/puppet]# rpm -q nginx
nginx-1.0.15-11.el6.x86_64
[[email protected] /tmp/puppet]# cat nginx.pp
package {'nginx':
        ensure => present,
}
service {'nginx':
        ensure  => true,
        path    => '/etc/rc.d/init.d/nginx',
        restart => 'reload',
        enable  => true,
}
[[email protected] /tmp/puppet]# puppet apply nginx.pp
Warning: Could not retrieve fact fqdn
Notice: Compiled catalog for legion100 in environment production in 0.60 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/lib/ruby/site_ruby/1.8/puppet/type/package.rb:430:in `default')
Notice: /Stage[main]/Main/Service[nginx]/ensure: ensure changed 'stopped' to 'running'
Notice: Finished catalog run in 0.57 seconds
[[email protected] /tmp/puppet]# ss -tnl
State       Recv-Q Send-Q                                             Local Address:Port                                               Peer Address:Port
LISTEN      0      100                                                          ::1:25                                                           :::*
LISTEN      0      100                                                    127.0.0.1:25                                                            *:*
LISTEN      0      128                                                    127.0.0.1:6010                                                          *:*
LISTEN      0      128                                                          ::1:6010                                                         :::*
LISTEN      0      128                                                           :::45981                                                        :::*
LISTEN      0      128                                                            *:14525                                                         *:*
LISTEN      0      128                                                           :::111                                                          :::*
LISTEN      0      128                                                            *:111                                                           *:*
LISTEN      0      511                                                            *:80                                                            *:*
LISTEN      0      128                                                           :::22                                                           :::*
LISTEN      0      128                                                            *:22                                                            *:*
LISTEN      0      128                                                    127.0.0.1:631                                                           *:*
LISTEN      0      128                                                          ::1:631                                                          :::*
[[email protected] /tmp/puppet]# service nginx status
nginx (pid  46839) 正在运行...
[[email protected] /tmp/puppet]# chkconfig --list nginx
nginx           0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭
[[email protected] /tmp/puppet]# 

Linux自动化运维之puppet2.7&3.7安装和常用类型详解

[[email protected] /tmp/puppet]# cat nginx2.pp
package {'nginx':
        ensure => present,
}
service {'nginx':
        ensure  => false,
        path    => '/etc/rc.d/init.d/nginx',
        restart => 'reload',
        enable  => false,
}
[[email protected] /tmp/puppet]# chkconfig --list nginx
nginx           0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭
[[email protected] /tmp/puppet]# service nginx status
nginx (pid  46839) 正在运行...
[[email protected] /tmp/puppet]# puppet apply nginx2.pp
Warning: Could not retrieve fact fqdn
Notice: Compiled catalog for legion100 in environment production in 0.55 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/lib/ruby/site_ruby/1.8/puppet/type/package.rb:430:in `default')
Notice: /Stage[main]/Main/Service[nginx]/ensure: ensure changed 'running' to 'stopped'
Notice: Finished catalog run in 0.52 seconds
[[email protected] /tmp/puppet]# service nginx status
nginx 已停
[[email protected] /tmp/puppet]# chkconfig --list nginx
nginx           0:关闭  1:关闭  2:关闭  3:关闭  4:关闭  5:关闭  6:关闭
[[email protected] /tmp/puppet]# 

Linux自动化运维之puppet2.7&3.7安装和常用类型详解

exec的常用属性:

用于执行命令:但通常仅应该用于必须执行命令才能完成的操作时;(puppet本身无法完成此功能才用)

command:要执行的命令(通常使用命令的绝对路径)

path:为非绝对路径的命令指定搜索路径

user:

group:

refresh:默认为收到依赖资源的事件时会重新执行此命令,但此属性可以修改此默认行为。

refreshonly:此资源不会自动被执行,除非被依赖的资源发生了 改变。

returens:指定返状态回值

tries:尝试最大次数

[[email protected] /tmp/puppet]# cat exec.pp
exec {'date':
        command => 'date > /tmp/date',
        path    => '/bin',
        user    => 'root',
        group   => 'root',
}
[[email protected] /tmp/puppet]# puppet apply exec.pp
Warning: Could not retrieve fact fqdn
Notice: Compiled catalog for legion100 in environment production in 0.23 seconds
Notice: /Stage[main]/Main/Exec[date]/returns: executed successfully
Notice: Finished catalog run in 0.18 seconds
[[email protected] /tmp/puppet]# ls -l /tmp/date
-rw-r--r-- 1 root root 43 6月   9 12:57 /tmp/date
[[email protected] /tmp/puppet]# cat /tmp/date
2015年 06月 09日 星期二 12:57:29 CST
[[email protected] /tmp/puppet]# 

Linux自动化运维之puppet2.7&3.7安装和常用类型详解

 

group的常用属性:

ensure:present、absent

name:组名

gid:GID

system:是否为系统组,true yes,false no

user的常用属性:

ensure:present absent

name:用户名

uid:UID

system:是否为系统用户

gid:基本组ID

groups:附加组

home:家目录

password:密码

shell:默认shell

[[email protected] /tmp/puppet]# cat user.pp
group {'legion':
        ensure  => 'present',
        gid             => '2000',
        system  => 'false',
}

user {'legion':
        ensure  => 'present',
        gid             => '2000',
        uid             => '2000',
        home    => '/tmp/legion',
        shell   => '/bin/tcsh',
}
[[email protected] /tmp/puppet]# puppet apply user.pp
Warning: Could not retrieve fact fqdn
Notice: Compiled catalog for legion100 in environment production in 0.28 seconds
Notice: /Stage[main]/Main/Group[legion]/ensure: created
Notice: /Stage[main]/Main/User[legion]/ensure: created
Notice: Finished catalog run in 0.48 seconds
[[email protected] /tmp/puppet]# id legion
uid=2000(legion) gid=2000(legion) 组=2000(legion)
[[email protected] /tmp/puppet]# finger legion
Login: legion                           Name:
Directory: /tmp/legion                  Shell: /bin/tcsh
Never logged in.
No mail.
No Plan.
[[email protected] /tmp/puppet]# 

Linux自动化运维之puppet2.7&3.7安装和常用类型详解

cron的常用属性:

ensure:present absent

command:周期性执行的命令

hour:小时

minute:分钟

month:日

monthdat:月

weekday:周

environment:定义PATH环境变量

[[email protected] /tmp/puppet]# crontab -l
no crontab for root
[[email protected] /tmp/puppet]# cat cron.pp
cron {'ntpdate':
        ensure  => 'present',
        command => '/usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1',
        minute  => '*/3',
}
[[email protected] /tmp/puppet]# puppet apply cron.pp
Warning: Could not retrieve fact fqdn
Notice: Compiled catalog for legion100 in environment production in 0.29 seconds
Notice: /Stage[main]/Main/Cron[ntpdate]/ensure: created
Notice: Finished catalog run in 0.08 seconds
[[email protected] /tmp/puppet]# crontab -l
# HEADER: This file was autogenerated at Tue Jun 09 13:52:40 +0800 2015 by puppet.
# HEADER: While it can still be managed manually, it is definitely not recommended.
# HEADER: Note particularly that the comments starting with 'Puppet Name' should
# HEADER: not be deleted, as doing so could cause duplicate cron jobs.
# Puppet Name: ntpdate
*/3 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1
[[email protected] /tmp/puppet]# 

Linux自动化运维之puppet2.7&3.7安装和常用类型详解

资源的特殊属性:

元参数:用于定义资源间的关系,常用的有4个

before:before    => Type['title']表示在某某之前执行

require:reqruire   => Type['title']表示在某某之后执行

notify:

subscribe:

notify和subscribe元素除了具有相应于before和require元素的依赖性定义功能之外,还具备额外的通知功能

"->"用于定义次序链,"~>"用于定义通知链,他们既可以用于资源引用间,也可以用于资源申报间

[[email protected] /tmp/puppet]# id legion
id: legion:无此用户
[[email protected] /tmp/puppet]# cat user.pp
user {'legion':
        ensure  => 'present',
        gid             => '2000',
        uid             => '2000',
        home    => '/tmp/legion',
        shell   => '/bin/tcsh',
}

group {'legion':
        ensure  => 'present',
        gid             => '2000',
        system  => 'false',
        before  => User['legion'],
}
[[email protected] /tmp/puppet]# puppet apply user.pp
Warning: Could not retrieve fact fqdn
Notice: Compiled catalog for legion100 in environment production in 0.28 seconds
Notice: /Stage[main]/Main/Group[legion]/ensure: created
Notice: /Stage[main]/Main/User[legion]/ensure: created
Notice: Finished catalog run in 0.27 seconds
[[email protected] /tmp/puppet]# id legion
uid=2000(legion) gid=2000(legion) 组=2000(legion)
[[email protected] /tmp/puppet]# finger legion
Login: legion                           Name:
Directory: /tmp/legion                  Shell: /bin/tcsh
Never logged in.
No mail.
No Plan.
[[email protected] /tmp/puppet]# 

Linux自动化运维之puppet2.7&3.7安装和常用类型详解

[[email protected] /tmp/puppet]# id legion
id: legion:无此用户
[[email protected] /tmp/puppet]# cat user.pp
user {'legion':
        ensure  => 'present',
        gid             => '2000',
        uid             => '2000',
        home    => '/tmp/legion',
        shell   => '/bin/tcsh',
        require => Group['legion'],
}

group {'legion':
        ensure  => 'present',
        gid             => '2000',
        system  => 'false',
}
[[email protected] /tmp/puppet]# puppet apply user.pp
Warning: Could not retrieve fact fqdn
Notice: Compiled catalog for legion100 in environment production in 0.30 seconds
Notice: /Stage[main]/Main/Group[legion]/ensure: created
Notice: /Stage[main]/Main/User[legion]/ensure: created
Notice: Finished catalog run in 0.23 seconds
[[email protected] /tmp/puppet]# id legion
uid=2000(legion) gid=2000(legion) 组=2000(legion)
[[email protected] /tmp/puppet]# finger legion
Login: legion                           Name:
Directory: /tmp/legion                  Shell: /bin/tcsh
Never logged in.
No mail.
No Plan.
[[email protected] /tmp/puppet]# tail -1 /etc/passwd
legion:x:2000:2000::/tmp/legion:/bin/tcsh
[[email protected] /tmp/puppet]# 

Linux自动化运维之puppet2.7&3.7安装和常用类型详解

[[email protected] /tmp/puppet]# cat nginx3.pp
package {'nginx':
        ensure => present,
}

file {'/etc/nginx/nginx.conf':
        ensure  => file,
        source  => '/configfile/nginx/nginx.conf',
}

service {'nginx':
        ensure  => false,
        path    => '/etc/rc.d/init.d/nginx',
        restart => 'reload',
        enable  => false,
        subscribe => File['/etc/nginx/nginx.conf'],
}
[[email protected] /tmp/puppet]# mkdir -p /configfile/nginx/
[[email protected] /tmp/puppet]# echo 1111> /configfile/nginx/nginx.conf

[[email protected] /tmp/puppet]# cat /configfile/nginx/nginx.conf
[[email protected] /tmp/puppet]# echo "1111" > /configfile/nginx/nginx.conf
[[email protected] /tmp/puppet]# cat /configfile/nginx/nginx.conf
1111
[[email protected] /tmp/puppet]# puppet apply nginx3.pp
Warning: Could not retrieve fact fqdn
Notice: Compiled catalog for legion100 in environment production in 0.58 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/lib/ruby/site_ruby/1.8/puppet/type/package.rb:430:in `default')
Notice: /Stage[main]/Main/File[/etc/nginx/nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}1f18348f32c9a4694f16426798937ae2'
Notice: /Stage[main]/Main/Service[nginx]: Triggered 'refresh' from 1 events
Notice: Finished catalog run in 0.37 seconds
[[email protected] /tmp/puppet]# cat /etc/nginx/nginx.conf
1111
[[email protected] /tmp/puppet]# 

Linux自动化运维之puppet2.7&3.7安装和常用类型详解

Linux自动化运维之puppet2.7&3.7安装和常用类型详解

Linux自动化运维之puppet2.7&3.7安装和常用类型详解

[[email protected] /tmp/puppet]# head -6 /etc/nginx/nginx.conf | tail -1
worker_processes  1;
[[email protected] /tmp/puppet]# head -6 /configfile/nginx/nginx.conf | tail -1
worker_processes  2;
[[email protected] /tmp/puppet]# rpm -e nginx
[[email protected] /tmp/puppet]# chkconfig --list | grep nginx
[[email protected] /tmp/puppet]# cat nginx3.pp
package {'nginx':
        ensure => present,
}

file {'/etc/nginx/nginx.conf':
        ensure  => file,
        source  => '/configfile/nginx/nginx.conf',
}

service {'nginx':
        ensure  => true,
        path    => '/etc/rc.d/init.d/nginx',
        restart => 'reload',
        enable  => true,
}

Package['nginx'] -> File['/etc/nginx/nginx.conf'] ~> Service['nginx']
[[email protected] /tmp/puppet]# puppet apply nginx3.pp
Warning: Could not retrieve fact fqdn
Notice: Compiled catalog for legion100 in environment production in 0.80 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/lib/ruby/site_ruby/1.8/puppet/type/package.rb:430:in `default')
Notice: /Stage[main]/Main/Package[nginx]/ensure: created
Notice: /Stage[main]/Main/File[/etc/nginx/nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}33d2119b71f717ef4b981e9364530a39'
Notice: /Stage[main]/Main/Service[nginx]/ensure: ensure changed 'stopped' to 'running'
Notice: Finished catalog run in 4.72 seconds
[[email protected] /tmp/puppet]# head -6 /configfile/nginx/nginx.conf | tail -1
worker_processes  2;
[[email protected] /tmp/puppet]# head -6 /etc/nginx/nginx.conf | tail -1
worker_processes  2;
[[email protected] /tmp/puppet]# chkconfig --list | grep nginx
nginx           0:关闭  1:关闭  2:启用  3:启用  4:启用  5:启用  6:关闭
[[email protected] /tmp/puppet]# service nginx status
nginx (pid  54889) 正在运行...
[[email protected] /tmp/puppet]# 

Linux自动化运维之puppet2.7&3.7安装和常用类型详解

[[email protected] /tmp/puppet]# ps aux | pgrep nginx
54889
54890
54891
[[email protected] /tmp/puppet]# sed -i 's/worker_processes.*/worker_processes  4;/' /configfile/nginx/nginx.conf
[[email protected] /tmp/puppet]# grep worker_processes /etc/nginx/nginx.conf
worker_processes  2;
[[email protected] /tmp/puppet]# grep worker_processes /configfile/nginx/nginx.conf
worker_processes  4;
[[email protected] /tmp/puppet]# puppet apply nginx3.pp
Warning: Could not retrieve fact fqdn
Notice: Compiled catalog for legion100 in environment production in 0.58 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/lib/ruby/site_ruby/1.8/puppet/type/package.rb:430:in `default')
Notice: /Stage[main]/Main/File[/etc/nginx/nginx.conf]/content: content changed '{md5}33d2119b71f717ef4b981e9364530a39' to '{md5}95f45f10386878664af2b7ccd1536ea4'
Notice: /Stage[main]/Main/Service[nginx]: Triggered 'refresh' from 1 events
Notice: Finished catalog run in 0.67 seconds
[[email protected] /tmp/puppet]# ps aux | pgrep nginx
55495
55496
55497
55498
55499
[[email protected] /tmp/puppet]# grep worker_processes /etc/nginx/nginx.conf
worker_processes  4;
[[email protected] /tmp/puppet]# service nginx status
nginx (pid  55495) 正在运行...
[[email protected] /tmp/puppet]# cat nginx3.pp
package {'nginx':
        ensure => present,
}

file {'/etc/nginx/nginx.conf':
        ensure  => file,
        source  => '/configfile/nginx/nginx.conf',
}

service {'nginx':
        ensure  => true,
        path    => '/etc/rc.d/init.d/nginx',
        enable  => true,
}

Package['nginx'] -> File['/etc/nginx/nginx.conf'] ~> Service['nginx']
[[email protected] /tmp/puppet]# 

Linux自动化运维之puppet2.7&3.7安装和常用类型详解

  • 本文由 发表于 2015年6月9日07:18:49
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: