本文收录在Linux运维企业架构实战系列html
puppet是什么,我们先不用专业的名词解释它,我们先描述一些工做场景,看明白这些工做场景,天然会知道puppet是什么。node
(1)场景一:python
管理员想要在100台服务器上同时建立一个名叫"along"的用户,怎么办,固然,咱们能够"手动解决",登陆每一台服务器,而后一台一台的建立用户,若是你真的这样干,那么我只能说,算你狠!!!可是,既然咱们这篇文章是介绍puppet的,咱们确定会推荐你使用puppet解决这个问题。linux
(2)场景二:nginx
公司新买了一堆云服务器,这些服务器最终可能要提供相同的服务,如今须要管理员在这一堆服务器上安装一些相同的应用,并且安装完成后,还须要这些服务器上的应用自动启动,怎么办,固然,手动解决算你狠,兄弟我服了,你说你写脚本解决,嗯,这是个办法,在我没有认识puppet以前我可能也会写个脚本,可是咱们是介绍puppet的,咱们会推荐你使用puppet解决这个问题。正则表达式
(3)场景三:redis
在一些服务器中,执行大批量的重复操做,咱们可使用puppet。shell
看完上述三个场景,你应该已经大概猜出puppet是干嘛的了,没错,说的糙一点,你能够把它理解成批量处理工具,可是你又不能彻底把它理解成批量处理工具,由于除了批量处理,他还有一些别的功能和特性,咱们暂且先这么认为,以便咱们入门。vim
(1)我本身对puppet的了解数组
像puppet这种工具备几个专业的名词能够用来称呼它们:"配置管理工具"、"自动化运维管理工具"。
Linux中,常见的配置管理工具备puppet、saltstack、ansible(博主以前讲解过)、chef等,它们的功能相似,puppet算是这个领域里面的老大哥,时间久(我说的是创始时间久,不要想歪了····),成熟,被普遍的应用,google、twitter、redhat,cisco等大公司都用到了它,功能强大,puppet是使用ruby研发的,saltstack和ansible都属于python系的, ansible和saltstack与puppet相比,属于后起之秀,咱们之后也可能会对它们进行总结,可是如今,咱们只聊puppet。
(2)专业术语介绍
本篇文章主要讲解资源;类、模块、站点清单都在下篇进行详解。
group | 组 |
user | 用户 |
packge | 程序包 |
service | 服务 |
file | 文件 |
exec | 执行自定义命令 |
cron | 周期性任务计划 |
notify | 通知 |
yumrepo | 源 |
host | /etc/hosts中主机解析 |
(1)查询资源类型和帮助信息
puppet describe [-h|--help] [-s|--short] [-p|--providers] [-l|--list] [-m|--meta] [type]
(2)执行资源清单命令
puppet apply [-h|--help] [-V|--version] [-d|--debug] [-v|--verbose] [-e|--execute] [--detailed-exitcodes] [-l|--logdest <file>] [--noop] [--catalog <catalog>] [--write-catalog-summary] <file>
这类垂直管理系统的使用及活跃,极大减轻了运维人员在重复性、批量化操做方面的负担,可以很是有效地在各自领域完成既定的运维子目标。但其缺陷在于只能针对某一垂直领域的特定问题进行高效处理,对于它们之间的关联性很难应对。由于运维的本质是保证服务的可用性,而自动化运维则是在彻底保证这一前提下,尽量将须要人干涉的部分处理掉,因此判断其优劣的标准则是——与人工处理比,对服务的保证有没有提升。若是仅是解决报警、部署这些单一动做,后续仍然须要人去处理、去关注、去判断的话,就离这个目标还有距离,谈不上真正的自动化,只能算是工具化。
puppet是一个开源的软件自动化配置和部署工具,它使用简单且功能强大,正获得了愈来愈多地关注,如今不少大型IT公司均在使用puppet对集群中的软件进行管理和部署,如google利用puppet管理超过6000台地mac桌面电脑(2007年数据)。
puppet设计架构是基于c/s架构的。服务器端保存着全部对客户端服务器的配置代码,在puppet里面叫作manifest. 客户端下载manifest以后,能够根据manifest对服务器进行配置,例如软件包管理,用户管理和文件管理等等。
puppet的使用模型分为 单机使用模型 和 master/agent模型;本文主要讲解单机使用模型,便于入门。
从上述专业的解释中,咱们能够得知puppet是C/S架构的,也就是说,它有服务端,也有客户端,管理员能够经过puppet服务端(master),管理每一台被管理的服务器,可是须要puppet客户端做为中介,也就是说,puppet客户端做为代理(agent),接收来自puppet服务端的配置信息,按照服务端(master)发送过来的配置信息,对被管理服务器进行配置,真正执行配置操做的是puppet客户端,puppet服务端只负责将配置信息准备好,发送给puppet客户端,以便客户端执行具体操做,puppet客户端还有另外一个做用,就是向puppet服务端发送报告,当客户端按照配置信息执行完成相关配置之后,会将执行信息发送到服务端,好比执行成功与否,执行结果等,默认状况下,每30分钟,puppet客户端会向puppet服务端发起一次请求,请求受管理服务器的配置信息, puppet服务端将配置信息发送给客户端,客户端根据反回的信息进行判断,判断被管理服务器是否符合管理员定义的配置,puppet的这种工做模式就是C/S架构的,也能够理解为master/agent的工做模式,用文字描述显得太无力,咱们画个图来看看。
那么,咱们把上图中的两台服务器拿出来,详细说说它们之间的具体工做流程,可是此处咱们须要说明,在master/agent模型下工做的puppet的工做流程比咱们总结的要复杂一点,可是为了入门方便,咱们只取出其中的一部分核心的流程进行总结,在后面的实际应用中,咱们会不断的丰富这些流程,此处先给出一个简化过的流程图,以下图所示。
过程分析:
① 客户端puppet agent请求catalog,咱们已经说过,catalog其实就是被管理服务器对应的配置文件(通过处理过的配置文件),服务端master收到agent的请求,而后找到对应被管理服务器的"站点清单",或者被称为"主机清单",由于一台"被管理服务器"可能同时担任多个"角色",每一个角色可能都会对应一个"manifest"(也就是清单),因此,咱们能够为每一台被管理服务器配置一个"站点清单",站点清单也能够理解成为一种"清单",只是它是针对一台服务器而存在的一种清单。
② 服务端master找到被管理服务器的站点清单后,根据站点清单,找到对应服务器须要哪些"清单",由于一台服务器可能会须要多张"清单",上图中为了示例,只画出了一个清单,可是这不表明一定只有一个。
③ master将找到的全部"清单"进行处理,处理为catalog。
④ master将处理过的catalog发送到agent端。
⑤ agent收到master发送过来的catalog,而后,agent会查询"被管理服务器的当前状态",看看服务器的当前状态是否符合catalog中定义的目标状态。
⑥ 若是"被管理服务器的当前状态"与"catalog中定义的目标状态"一致,那么资源对应的操做将不会执行,若是"被管理服务器的当前状态"与"catalog中定义的目标状态"不一致,那么资源对应的操做将会执行,以便让"被管理服务器"达到管理员指定的"目标状态"。
⑦ 通过上一步的"状态判断",执行对应操做,无论执行是否成功,都会生成对应的报告信息。
⑧ agent将生成的报告信息发送给master端。
上述过程,就是puppet在master/agent模式下的工做流程,咱们说过,默认状况下,客户端每隔30分钟向服务端请求一次配置信息,而后根据服务端返回的配置信息,判断当前服务器是否处于管理员定义的目标状态,若是被管理的服务器不处于管理员定义的目标状态,则须要执行对应的操做,使得被管理主机最终处于管理员定义的目标状态,咱们也能够没必要每次都等待30分钟,咱们能够从master端推送catalog到agent端,主动告诉agent端,配置已经发生了改变,请执行对应的操做。这是后话,咱们之后再聊。
固然,puppet也能够不在master/agent模式下工做,咱们能够在受管理服务器上只安装puppet客户端,使用客户端手动执行对应配置文件,至关于配置文件中的信息并非经过puppet服务端发送过来,而是经过本地的配置文件获取,也是能够的,咱们暂且称这种不须要puppet服务端的工做模式为单机模式,咱们在学习puppet时,可使用单机的模式进行练习,可是在生产环境中,通常都使用master/agent的方式使用puppet。
实现定义多个manifests --> complier --> catalog --> apply
首先,咱们仍是来安装一下puppet,puppet的安装可使用源码安装,也可使用rpm(官方提供)、epel源、官方提供的yum仓库来安装(经过下载官方提供的rpm包能够指定官方的yum仓库)。
在这里,咱们就是用 yum 安装的方式。
[root@along ~]# yum -y install puppet
(1)资源抽象
puppet 从如下三个维度来对资源完成抽象:
这三个也被称做puppet 的资源抽象层(RAL)
RAL 由type( 类型) 和provider( 提供者,即不一样OS 上的特定实现)组成。
(2)资源定义
① 资源定义经过向资源类型的属性赋值来实现,可称为资源类型实例化;
② 定义了资源实例的文件即清单,manifest;
③ 定义资源的语法以下:
type {'title': attribute1 => value1, atrribute2 => value2, …… }
注意:type必须使用小写字符;title是一个字符串,在同一类型中必须唯一;每个属性之间须要用“,”隔开,最后一个“,”可省略。
例如,能够同时有名为nginx 的“service”资源和“package”资源,但在“package” 类型的资源中只能有一个名为“nginx”的资源。
(3)资源属性中的三个特殊属性:
(1)属性 Parameters:
(2)puppet describe group -s -m 使用以前,能够查询这个类型的选项
① 编辑manifest( 清单)
[root@along manifest]# vim group.pp group{'mygrp': name => 'mygrp', ensure => present, gid => 2000, }
② 执行并验证 manifest
---先预执行一次,检查是否有语法错误 [root@along manifest]# puppet apply -v --noop group.pp Notice: Compiled catalog for along in environment production in 0.18 seconds Info: Applying configuration version '1550042227' Notice: /Stage[main]/Main/Group[mygrp]/ensure: current_value absent, should be present (noop) Notice: Class[Main]: Would have triggered 'refresh' from 1 events Notice: Stage[main]: Would have triggered 'refresh' from 1 events Info: Creating state file /var/lib/puppet/state/state.yaml Notice: Finished catalog run in 0.03 seconds ---真正执行,执行成功 [root@along manifest]# puppet apply -v group.pp Notice: Compiled catalog for along in environment production in 0.06 seconds Info: Applying configuration version '1550042298' Notice: /Stage[main]/Main/Group[mygrp]/ensure: created Notice: Finished catalog run in 0.09 seconds ---查询验证 [root@along manifest]# cat /etc/group |grep mygrp mygrp:x:2000:
(1)属性 Parameters:
(2)puppet describe user -s -m 使用以前,能够查询这个类型的选项
① 编辑manifest( 清单)
[root@along manifest]# vim user1.pp user{'along1': ensure => present, system => false, comment => 'Test user', shell => '/usr/sbin/nologin', home => '/app/along', managehome => true, groups => 'mygrp', uid => 3000, }
② 执行manifest
[root@along manifest]# puppet apply -v --noop user1.pp [root@along manifest]# puppet apply -v user1.pp Notice: Compiled catalog for along in environment production in 0.08 seconds Info: Applying configuration version '1550042932' Notice: /Stage[main]/Main/User[along1]/ensure: created Notice: Finished catalog run in 0.12 seconds [root@along manifest]# cat /etc/passwd |grep along1 along1:x:3000:3000:Test user:/mnt/along:/usr/sbin/nologin
① 属性:
② puppet describe package -s -m 能够查询这个类型的选项
① 建立manifest
[root@along manifest]# vim pkg.pp package{'redis': ensure => installed, provider => yum }
② 执行manifest
[root@along manifest]# puppet apply --noop -v pkg.pp [root@along manifest]# puppet apply -v pkg.pp Notice: Compiled catalog for along in environment production in 0.26 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') Info: Applying configuration version '1550044027' Notice: /Stage[main]/Main/Package[redis]/ensure: created Notice: Finished catalog run in 6.09 seconds [root@along manifest]# rpm -ql redis
① 属性:
② puppet describe service -s -m 能够查询这个类型的选项
① 编写manifest 资源清单
[root@along manifest]# vim srv1.pp service{'redis': ensure => running, enable => true, }
② 执行
[root@along manifest]# puppet apply -v --noop srv1.pp [root@along manifest]# puppet apply -v srv1.pp Notice: Compiled catalog for along in environment production in 0.08 seconds Info: Applying configuration version '1550044320' Notice: /Stage[main]/Main/Service[redis]/ensure: ensure changed 'stopped' to 'running' Info: /Stage[main]/Main/Service[redis]: Unscheduling refresh on Service[redis] Notice: Finished catalog run in 0.11 seconds [root@along manifest]# ss -nutl |grep 6379 tcp LISTEN 0 128 127.0.0.1:6379 *:*
① 编写manifest 资源清单
[root@along manifest]# vim srv2.pp package {'memcached': ensure => installed, } service{'memcached': ensure => running, enable => false, require => Package['memcached'] }
② 执行
[root@along manifest]# puppet apply -v --noop srv2.pp [root@along manifest]# puppet apply -v srv2.pp Notice: Compiled catalog for along in environment production in 0.35 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') Info: Applying configuration version '1550044434' Notice: /Stage[main]/Main/Package[memcached]/ensure: created Notice: /Stage[main]/Main/Service[memcached]/ensure: ensure changed 'stopped' to 'running' Info: /Stage[main]/Main/Service[memcached]: Unscheduling refresh on Service[memcached] Notice: Finished catalog run in 3.28 seconds [root@along manifest]# ss -nutlp |grep memcached udp UNCONN 0 0 *:11211 *:* users:(("memcached",pid=8573,fd=28))
(1)类型的属性、选项
① 属性
② puppet describe file -s -m |tail -20
建立test.txt,且直接写内容
① 编写manifest 清单
[root@along manifest]# vim file1.pp file{'/mnt/test.txt': ensure => file, content => "How are you?\nHow old are you?\n", owner => 'along1', group => 'mygrp', mode => '0400' }
② 执行
[root@along manifest]# puppet apply -v --noop file1.pp [root@along manifest]# puppet apply -v file1.pp Notice: Compiled catalog for along in environment production in 0.07 seconds Info: Applying configuration version '1550044597' Notice: /Stage[main]/Main/File[/mnt/test.txt]/ensure: defined content as '{md5}93c1208b855bf27a44bb2eb1b022b8cd' Notice: Finished catalog run in 0.04 seconds [root@along manifest]# ll /mnt/test.txt -r-------- 1 along1 mygrp 30 Feb 13 15:56 /mnt/test.txt [root@along manifest]# cat /mnt/test.txt How are you? How old are you?
① 编写manifest 清单
[root@along manifest]# vim file2.pp file{'redis.conf': path => '/mnt/redis.conf', ensure => file, source => '/root/manifest/files/redis.conf' } file{'symlink.conf': ensure => link, path => '/mnt/symlink.conf', target => '/mnt/redis.conf', require => File['redis.conf'] }
提示:若要指定source ,最好先把源文件复制到此目录中,由于在master-agent 模式中,不能确保agent 机器上的路径下,必定有要复制的文件;本次操做须要在/root/manifests/files/准备好redis.con文件。
[root@along manifest]# mkdir files [root@along manifest]# cp /etc/redis.conf /root/manifest/files/
② 执行
[root@along manifest]# puppet apply -v --noop file2.pp [root@along manifest]# puppet apply -v file2.pp Notice: Compiled catalog for along in environment production in 0.07 seconds Info: Applying configuration version '1550045858' Notice: /Stage[main]/Main/File[redis.conf]/ensure: defined content as '{md5}d98629fded012cd2a25b9db0599a9251' Notice: /Stage[main]/Main/File[symlink.conf]/ensure: created Notice: Finished catalog run in 0.05 seconds [root@along manifest]# ll /mnt/redis.conf /mnt/symlink.conf -rw-r----- 1 777 root 46729 Feb 13 16:17 /mnt/redis.conf lrwxrwxrwx 1 root root 15 Feb 13 16:17 /mnt/symlink.conf -> /mnt/redis.conf
建立目录,且递归复制另外一个目录
① 编写manifest 清单
[root@along manifest]# vim file3.pp file{'test.dir': ensure => directory, path => '/app/test.dir', source => '/etc/httpd', recurse => true #递归复制 }
注意:
目录复制目录,是将目录下的全部文件递归复制;
不要用path 为目录,source为文件,不会将此文件复制到目录中;只能是目录复制目录
② 执行
[root@along manifest]# puppet apply -v file3.pp Notice: Compiled catalog for along in environment production in 0.07 seconds Info: Applying configuration version '1550046089' Notice: /Stage[main]/Main/File[test.dir]/ensure: created Notice: /Stage[main]/Main/File[/mnt/test.dir/along]/ensure: created Notice: /Stage[main]/Main/File[/mnt/test.dir/along/.bash_logout]/ensure: defined content as '{md5}6a5bc1cc5f80a48b540bc09d082b5855' Notice: /Stage[main]/Main/File[/mnt/test.dir/symlink.conf]/ensure: created Notice: /Stage[main]/Main/File[/mnt/test.dir/along/.bash_profile]/ensure: defined content as '{md5}f939eb71a81a9da364410b799e817202' Notice: /Stage[main]/Main/File[/mnt/test.dir/redis.conf]/ensure: defined content as '{md5}d98629fded012cd2a25b9db0599a9251' Notice: /Stage[main]/Main/File[/mnt/test.dir/along/.bashrc]/ensure: defined content as '{md5}2f8222b4f275c4f18e69c34f66d2631b' Notice: /Stage[main]/Main/File[/mnt/test.dir/test.txt]/ensure: defined content as '{md5}93c1208b855bf27a44bb2eb1b022b8cd' Notice: Finished catalog run in 0.08 seconds [root@along manifest]# ll /mnt/test.dir/ total 52 drwx------ 2 along1 along1 62 Feb 13 16:21 along -rw-r----- 1 777 root 46729 Feb 13 16:21 redis.conf lrwxrwxrwx 1 root root 15 Feb 13 16:21 symlink.conf -> /mnt/redis.conf -r-------- 1 along1 mygrp 30 Feb 13 16:21 test.txt
下载redis 包,复制配置文件,启动redis 服务
① 编写manifest 清单
[root@along manifest]# vim srv3.pp package{'redis': ensure => installed, } file{'/etc/redis.conf': ensure => file, source => '/root/manifest/files/redis.conf', owner => 'redis', group => 'root', mode => '0640' } service{'redis': ensure => running, enable => false } Package['redis'] -> File['/etc/redis.conf'] -> Service['redis']
注意:我事先将/root/manifest/files/redis.conf配置文件的端口修改成了6300
② 执行
[root@along manifest]# puppet apply -v --noop srv3.pp [root@along manifest]# puppet apply -v srv3.pp Notice: Compiled catalog for along in environment production in 0.46 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') Info: Applying configuration version '1550046351' Notice: /Stage[main]/Main/Package[redis]/ensure: created Info: /Stage[main]/Main/File[/etc/redis.conf]: Filebucketed /etc/redis.conf to puppet with sum d98629fded012cd2a25b9db0599a9251 Notice: /Stage[main]/Main/File[/etc/redis.conf]/content: content changed '{md5}d98629fded012cd2a25b9db0599a9251' to '{md5}d3fc0c22e1a90f88a895242b2a251dad' Notice: /Stage[main]/Main/Service[redis]/ensure: ensure changed 'stopped' to 'running' Info: /Stage[main]/Main/Service[redis]: Unscheduling refresh on Service[redis] Notice: Finished catalog run in 3.65 seconds [root@along manifest]# ss -nutlp |grep 6300 tcp LISTEN 0 128 127.0.0.1:6300 *:* users:(("redis-server",pid=11628,fd=4))
注意:exec资源中的任何命令都必须可以屡次运行而不会形成损害——也就是说,它必须具备幂等性。
建立一个目录
① 编写manifest 清单
[root@along manifest]# vim cmd1.pp exec{'cmd': command => '[ -e /mnt/testdir ] || mkdir /mnt/testdir', path => ['/bin','sbin','/usr/bin','/usr/sbin'], # creates => '/mnt/testdir' 判断目录是否存在 }
分析:先判断目录是否存在,不存在再建立目录,使命令具备幂等性;就算是目录已存在,也不会有报错
② 执行
[root@along manifest]# puppet apply -v cmd1.pp Notice: Compiled catalog for along in environment production in 0.02 seconds Info: Applying configuration version '1550047003' Notice: /Stage[main]/Main/Exec[cmd]/returns: executed successfully Notice: Finished catalog run in 0.03 seconds [root@along manifest]# ll -d /mnt/testdir/ drwxr-xr-x 2 root root 6 Feb 13 16:36 /mnt/testdir/ [root@along manifest]# puppet apply -v cmd1.pp Notice: Compiled catalog for along in environment production in 0.02 seconds Info: Applying configuration version '1550047046' Notice: /Stage[main]/Main/Exec[cmd]/returns: executed successfully Notice: Finished catalog run in 0.03 seconds
---编写manifest 清单 [root@along manifest]# vim cmd2.pp exec{'user': command => 'useradd along', path => '/bin:sbin:/usr/bin:/usr/sbin', unless => 'id along' } ---执行 [root@along manifest]# puppet apply -v cmd2.pp Notice: Compiled catalog for along in environment production in 0.02 seconds Info: Applying configuration version '1550047187' Notice: Finished catalog run in 0.03 seconds [root@along manifest]# id along uid=1000(along) gid=1000(along) groups=1000(along)
① 编写manifest 清单
[root@along manifest]# vim cmd3.pp package{'redis': ensure => installed, } file{'/etc/redis.conf': ensure => file, source => '/root/manifest/files/redis.conf', owner => 'redis', group => 'root', mode => '0640' } exec{'redis': command => '/usr/bin/systemctl start redis', refresh => "/usr/bin/systemctl restart redis", user => 'redis', group => 'redis', } Package['redis'] -> File['/etc/redis.conf'] ~> Exec['redis']
注意:当配置文件修改,触发动做,会执行refresh 命令替代command
② 执行
[root@along manifest]# vim files/redis.conf 把redis 端口修改成7777,再执行 port 7777 [root@along manifest]# puppet apply -v cmd3.pp Notice: Compiled catalog for along in environment production in 0.38 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') Info: Applying configuration version '1550109397' Notice: /Stage[main]/Main/Exec[redis]/returns: executed successfully Notice: Finished catalog run in 0.12 seconds [root@along manifest]# ss -nutlp |grep redis tcp LISTEN 0 128 127.0.0.1:7777 *:* users:(("redis-server",pid=4988,fd=4))
由Puppet建立的每一个cron资源都须要一个命令和至少一个周期属性(小时、分钟、月、月、工做日、或特殊)
① 编写manifest 清单
下载ntpdate包,并制定计划任务,每隔5分钟同步一次时间
[root@along manifest]# vim cron.pp package{'ntpdate': ensure => installed, } cron{'mysync': command => '/usr/sbin/ntpdate 192.168.10.1 &> /dec/null', ensure => present, minute => "*/5", target => 'root' }
② 执行
[root@along manifest]# puppet apply -v cron.pp Notice: Compiled catalog for along in environment production in 0.29 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') Info: Applying configuration version '1550109974' Notice: /Stage[main]/Main/Cron[mysync]/ensure: created Notice: /Stage[main]/Main/Package[ntpdate]/ensure: created Notice: Finished catalog run in 3.20 seconds [root@along manifest]# crontab -l # HEADER: This file was autogenerated at 2019-02-14 10:06:14 +0800 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: mysync */5 * * * * /usr/sbin/ntpdate 192.168.10.1 &> /dec/null
---编写manifest 清单 [root@along manifest]# vim notify.pp notify{'syhi': message => "hi there." } ---执行 [root@along manifest]# puppet apply -v notify.pp Notice: Compiled catalog for along in environment production in 0.01 seconds Info: Applying configuration version '1550110275' Notice: hi there. Notice: /Stage[main]/Main/Notify[syhi]/message: defined 'message' as 'hi there.' Notice: Finished catalog run in 0.04 seconds
(1)yumrepo:yum源
(2)host:定义/etc/hosts 中名字解析
(1)格式:3中实现方法
① A before B: B依赖于A,定义在A资源中;
{ ... before => Type['B'], ... }
② B require A: B依赖于A,定义在B资源中;
{ ... require => Type['A'], ... }
③ A -> B,B依赖于A
(2)注意:
① before、 require 和 -> 三者用其一便可
② 资源引用:Type['title']
类型的首字母必须大写
(3)演示:建立一个用户,须要依赖一个组
① 建立manifest
[root@along manifest]# vim user2.pp [root@along manifest]# cat user2.pp group{'redhat': ensure => present, # before => User['ilinux'], #方案1 } user{'ilinux': ensure => present, comment => "ilinux.io", groups => 'redhat', require => Group['redhat'], #方案2 } #Group['redhat'] -> User['ilinux'] #方案3
② 执行,先建立了组redhat,再建立了用户ilinux
[root@along manifest]# puppet apply -v user2.pp Notice: Compiled catalog for along in environment production in 0.14 seconds Info: Applying configuration version '1550043297' Notice: /Stage[main]/Main/Group[redhat]/ensure: created Notice: /Stage[main]/Main/User[ilinux]/ensure: created Notice: Finished catalog run in 0.12 seconds
(1)格式:notify/subscribe
① notify:A notify B:B依赖于A,且A发生改变后会通知B;
{ ... notify => Type['B'], ... }
② subscribe:B subscribe A:B依赖于A,且B监控A资源的变化产生的事件;
{ ... subscribe => Type['A'], ... }
③ A ~ B ,B依赖于A缩写版
(2)实例:下载redis 包,复制配置文件,启动redis 服务
① 修改srv3.pp manifest 清单
[root@along manifest]# vim srv3.pp package{'redis': ensure => installed, } file{'/etc/redis.conf': ensure => file, source => '/root/manifest/files/redis.conf', owner => 'redis', group => 'root', mode => '0640', # notify => Service['redis'] } service{'redis': ensure => running, enable => false, hasrestart => true, # subscribe => File['/etc/redis.conf'] } Package['redis'] -> File['/etc/redis.conf'] ~> Service['redis']
② 执行
[root@along manifest]# puppet apply -v --noop srv3.pp [root@along manifest]# puppet apply -v srv3.pp Notice: Compiled catalog for along 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') Info: Applying configuration version '1550046675' Notice: /Stage[main]/Main/Package[redis]/ensure: created Info: FileBucket got a duplicate file {md5}d98629fded012cd2a25b9db0599a9251 Info: /Stage[main]/Main/File[/etc/redis.conf]: Filebucketed /etc/redis.conf to puppet with sum d98629fded012cd2a25b9db0599a9251 Notice: /Stage[main]/Main/File[/etc/redis.conf]/content: content changed '{md5}d98629fded012cd2a25b9db0599a9251' to '{md5}d3fc0c22e1a90f88a895242b2a251dad' Info: /Stage[main]/Main/File[/etc/redis.conf]: Scheduling refresh of Service[redis] Notice: /Stage[main]/Main/Service[redis]/ensure: ensure changed 'stopped' to 'running' Info: /Stage[main]/Main/Service[redis]: Unscheduling refresh on Service[redis] Notice: Finished catalog run in 5.25 seconds [root@along manifest]# ss -nutlp |grep redis tcp LISTEN 0 128 127.0.0.1:6300 *:* users:(("redis-server",pid=12492,fd=4))
③ 修改 /root/manifest/files/redis.conf 把端口改成6000
[root@along manifest]# vim files/redis.conf port 6000 [root@along manifest]# puppet apply -v srv3.pp 再执行,由于文件发生了修改,触发了重启服务,端口也确实改成了6000 Notice: Compiled catalog for along in environment production in 0.44 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') Info: Applying configuration version '1550046726' Info: /Stage[main]/Main/File[/etc/redis.conf]: Filebucketed /etc/redis.conf to puppet with sum d3fc0c22e1a90f88a895242b2a251dad Notice: /Stage[main]/Main/File[/etc/redis.conf]/content: content changed '{md5}d3fc0c22e1a90f88a895242b2a251dad' to '{md5}46e070a908eb1aca5c908e62dd4296b9' Info: /Stage[main]/Main/File[/etc/redis.conf]: Scheduling refresh of Service[redis] Notice: /Stage[main]/Main/Service[redis]: Triggered 'refresh' from 1 events Notice: Finished catalog run in 0.23 seconds [root@along manifest]# ss -nutlp |grep redis tcp LISTEN 0 128 127.0.0.1:6000 *:* users:(("redis-server",pid=12785,fd=4))
如同 anssible 同样,puppet 也能够定义“标签”——tag,打了标签之后,咱们在运行资源的时候就能够只运行某个打过标签的部分,而非所有。这样就更方便于咱们的操做。
一个资源中,能够有一个tag也能够有多个。
(1)格式:
资源定义:
type{'title': ... tag => 'TAG1', } type{'title': ... tag => ['TAG1','TAG2',...], }
手动调用:
puppet apply --tags TAG1,TAG2,... FILE.PP
(2)实例:
① 建立manifest
[root@along manifest]# vim srv3.pp package{'redis': ensure => installed, } file{'/etc/redis.conf': ensure => file, source => '/root/manifests/files/redis.conf', owner => 'redis', group => 'root', mode => '0640', tag => 'conf' } service{'redis': ensure => running, enable => false, hasrestart => true } Package['redis'] -> File['/etc/redis.conf'] ~> Service['redis']
② 执行
---先修改files配置文件端口为8888 [root@along manifest]# vim files/redis.conf port 8888 ---再执行 [root@along manifest]# puppet apply -v --tags conf srv3.pp Notice: Compiled catalog for along in environment production in 0.44 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') Info: Applying configuration version '1550113147' Info: /Stage[main]/Main/File[/etc/redis.conf]: Filebucketed /etc/redis.conf to puppet with sum fe60391ea96c958fe489f86002679ec3 Notice: /Stage[main]/Main/File[/etc/redis.conf]/content: content changed '{md5}fe60391ea96c958fe489f86002679ec3' to '{md5}cb214aa5ead0d70454d957ca9125ad01' Info: /Stage[main]/Main/File[/etc/redis.conf]: Scheduling refresh of Service[redis] Notice: /Stage[main]/Main/Service[redis]: Triggered 'refresh' from 1 events Notice: Finished catalog run in 0.17 seconds [root@along manifest]# grep "port 8888" /etc/redis.conf port 8888
puppet 变量以“$”开头,赋值操做符为“=”,语法为$variable_name=value。
(1)数据类型:
(2)正则表达式:
(?<ENABLED OPTION>:<PATTERN>) (?-<DISABLED OPTION>:<PATTERN>) OPTIONS: i:忽略字符大小写; m:把.当换行符; x:忽略<PATTERN>中的空白字符;
注意:不能赋值给变量,仅能用在接受=~或!~操做符的位置;
(3)puppet的变量种类
puppet 种类有三种,为facts,内建变量和用户自定义变量。
(4)变量的做用域
其优先级排序为:top scope < node scope < class scope