设为首页 收藏本站
查看: 1048|回复: 0

[经验分享] Puppet条件语句,class,module的使用

[复制链接]
累计签到:2 天
连续签到:1 天
发表于 2018-8-2 12:00:40 | 显示全部楼层 |阅读模式
  一、Puppet条件语句和类
  Puppet2.7系列的版本支持使用3种条件判断语句,包括:if,case,selector,puppet也支持使用class是用于通用目标的一组资源,因此,它是命名的代码块,在某位置创建之后可在puppet全局使用,类似于其他编程语言中类的功能,puppet的class可以被继承,也可以包含子类。
  二、Puppet模块
  到目前为止,资源申报、定义类、声明类等所有功能都只能再一个manifest文件中实现,但这却非最有效的基于puppet管理IT基础架构的方式,实践中,一般需要把manifest文件分解成易于理解的结构,例如将类文件,配置文件、模块文件等分类存放,并且通过某种机制在必要时将它们整合起来,这种机制就是“模块”,它有助于以结构化、层次化的方式使用puppet,而puppet则基于“模块自动装载器”完成模块装载,模块实际就是一个按约定的、预定义的结构存放了多个文件或子目录的目录,目录里的这些文件或子目录必须遵循其命名规范,puppet会按此种规范在特定位置查找所需要的模块文件,不过,这些特定的目录也可以通过puppet的配置参数modulepath定义。
  首先来演示下if语句的使用,if又包括单分支,双分支,多分支语句
[root@node1 ~]# vi test6.pp  
if $operatingsystem =~ /(?i-mx:^(centos|fedora|redhat))/ {
  $webserver = 'httpd'
  
} elsif operatingsystem  =~ /(?i-mx:^(debian|ubuntu))/ {
  $webserver = 'apache2'
  
} else {
  $webserver = undef
  notice('Unkown OS')
  
}
  
package {"$webserver":
  ensure => installed,
  
}
  
[root@node1 ~]# puppet apply test6.pp
  
notice: /Stage[main]//Package[httpd]/ensure: created
  
notice: Finished catalog run in 2.91 seconds
  
[root@node1 ~]# rpm -q httpd
  
httpd-2.2.15-30.el6.centos.x86_64
  
# 使用多分支if判断语句,operatingsystem顶级变量,根据当前系统安装所要安装的程序包
  case语句,类似if语句,case语句会从多个代码块中选择一个分支执行,这跟其他编程语言中的case语句功能一致,case语句会接受一个控制表达式和一组case代码块,并执行第一个匹配到控制表达式的块
[root@node1 ~]# vi test7.pp  
case $operatingsystem {
  'Solaris':              {notice("Welcome to Solaris")}
  'RedHat','CentOS':      {notice("Welcome to RedHat OSFamily")}
  /^(Debian|Ubuntu)$/:    {notice("Welcome to $1 linux")}
  default:                {notice("Welcome,alien *_*")}
  
}
  
[root@node1 ~]# puppet apply test7.pp
  
notice: Scope(Class[main]): Welcome to RedHat OSFamily
  
notice: Finished catalog run in 0.01 seconds
  selector语句,selector只能用于期望出现直接值的地方,这包括变量赋值,资源属性,函数参数,资源标题,其他selector的值及表达式,selector不能用于一个已经嵌套于selector的case中,也不能用于一个已经嵌套于case的case语句中
[root@node1 ~]# vi test8.pp  
$webserver = $operatingsystem ? {
  /(?i-mx:ubuntu|debian)/        => 'apache2',
  /(?i-mx:centos|fedora|redhat)/ => 'httpd',
  
}
  
notify {"$webserver":
  message => "install $webserver",
  
}
  
[root@node1 ~]# puppet apply test8.pp
  
notice: install httpd
  
notice: /Stage[main]//Notify[httpd]/message: defined 'message' as 'install httpd'
  
notice: Finished catalog run in 0.02 seconds
  class的使用
[root@node1 ~]# vi test9.pp  
class nginx {
  package {'nginx':
  ensure => installed,
  before => File['/etc/nginx/nginx.conf'],
  }
  file {'/etc/nginx/nginx.conf':
  ensure => file,
  mode => 0644,
  owner => 'root',
  group => 'root',
  source => '/tmp/nginx.conf',
  notify => Service['nginx'],
  }
  service { 'nginx':
  ensure => running,
  }
  
}
  
include nginx
  
# 使用include来调用class
  
[root@node1 ~]# puppet apply test9.pp
  
notice: /Stage[main]/Nginx/Package[nginx]/ensure: created
  
notice: /Stage[main]/Nginx/File[/etc/nginx/nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}95f45f10386878664af2b7ccd1536ea4'
  
notice: /Stage[main]/Nginx/Service[nginx]/ensure: ensure changed 'stopped' to 'running'
  
notice: /Stage[main]/Nginx/Service[nginx]: Triggered 'refresh' from 1 events
  
notice: Finished catalog run in 2.16 seconds
  
# 也可以使用以下方式调用,这种方式可以传递参数
  
class nginx {
  package {'nginx':
  ensure => installed,
  before => File['/etc/nginx/nginx.conf'],
  }
  file {'/etc/nginx/nginx.conf':
  ensure => file,
  mode => 0644,
  owner => 'root',
  group => 'root',
  source => '/tmp/nginx.conf',
  notify => Service['nginx'],
  }
  service { 'nginx':
  ensure => running,
  }
  
}
  
class {'nginx':}
  
[root@node1 ~]# puppet apply test9.pp
  
notice: /Stage[main]/Nginx/Package[nginx]/ensure: created
  
notice: /Stage[main]/Nginx/File[/etc/nginx/nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}95f45f10386878664af2b7ccd1536ea4'
  
notice: /Stage[main]/Nginx/Service[nginx]/ensure: ensure changed 'stopped' to 'running'
  
notice: /Stage[main]/Nginx/Service[nginx]: Triggered 'refresh' from 1 events
  
notice: Finished catalog run in 2.06 seconds
  使用class方法调用类,还可以传递参数实现,根据参数来安装不同的服务,比如想安装tengine
[root@node1 ~]# vi test9.pp  
class webserver ($wbsvr='nginx') {
  package {"$wbsvr":
  ensure => installed,
  before => File['/etc/nginx/nginx.conf'],
  }
  file {'/etc/nginx/nginx.conf':
  ensure => file,
  mode => 0644,
  owner => 'root',
  group => 'root',
  source => '/tmp/nginx.conf',
  notify => Service['nginx'],
  }
  service { 'nginx':
  ensure => running,
  }
  
}
  
class {'webserver':
  wbsvr => 'nginx',
  
}
  
[root@node1 ~]# puppet apply test9.pp
  
notice: /Stage[main]/Webserver/Package[nginx]/ensure: created
  
notice: /Stage[main]/Webserver/File[/etc/nginx/nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}95f45f10386878664af2b7ccd1536ea4'
  
notice: /Stage[main]/Webserver/Service[nginx]/ensure: ensure changed 'stopped' to 'running'
  
notice: /Stage[main]/Webserver/Service[nginx]: Triggered 'refresh' from 1 events
  
notice: Finished catalog run in 2.09 seconds
  
[root@node1 ~]# vi test9.pp
  
class webserver ($wbsvr='nginx') {
  package {"$wbsvr":
  ensure => installed,
  before => File['/etc/nginx/nginx.conf'],
  }
  file {'/etc/nginx/nginx.conf':
  ensure => file,
  mode => 0644,
  owner => 'root',
  group => 'root',
  source => '/tmp/nginx.conf',
  notify => Service['nginx'],
  }
  service { 'nginx':
  ensure => running,
  }
  
}
  
class {'webserver':
  wbsvr => 'tenginx',
  
}
  
[root@node1 ~]# puppet apply test9.pp
  
err: /Stage[main]/Webserver/Package[tenginx]/ensure: change from absent to present failed: Execution of '/usr/bin/yum -d 0 -e 0 -y install tenginx' returned 1: Error: Nothing to do
  
notice: /Stage[main]/Webserver/File[/etc/nginx/nginx.conf]: Dependency Package[tenginx] has failures: true
  
warning: /Stage[main]/Webserver/File[/etc/nginx/nginx.conf]: Skipping because of failed dependencies
  
notice: /Stage[main]/Webserver/Service[nginx]: Dependency Package[tenginx] has failures: true
  
warning: /Stage[main]/Webserver/Service[nginx]: Skipping because of failed dependencies
  
notice: Finished catalog run in 0.41 second
  类也可以被继承,实现根据不同情况调用不同的类
[root@node1 ~]# vi test10.pp  
class nginx {
  package {'nginx':
  ensure => installed,
  }
  
}
  
class nginx::websvr inherits nginx {
  file {'/etc/nginx/nginx.conf':
  ensure => file,
  mode => '0644',
  owner => 'root',
  group => 'root',
  require => Package['nginx'],
  source => '/tmp/nginx.conf',
  }
  service {'nginx':
  ensure => running,
  }
  
}
  
class nginx::rproxy inherits nginx {
  file {'/etc/nginx/nginx.conf':
  ensure => file,
  mode => '0644',
  owner => 'root',
  group => 'root',
  require => Package['nginx'],
  source => '/tmp/nginx-proxy.conf',
  }
  service {'nginx':
  ensure => running,
  }
  
}
  
include nginx::websvr
  
[root@node1 ~]# puppet apply test10.pp
  
notice: /Stage[main]/Nginx/Package[nginx]/ensure: created
  
notice: /Stage[main]/Nginx::Websvr/File[/etc/nginx/nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}95f45f10386878664af2b7ccd1536ea4'
  
notice: /Stage[main]/Nginx::Websvr/Service[nginx]/ensure: ensure changed 'stopped' to 'running'
  
notice: Finished catalog run in 1.81 seconds
  
# 调用nginx::rproxy类
  
[root@node1 ~]# vi test10.pp
  
class nginx {
  package {'nginx':
  ensure => installed,
  }
  
}
  
class nginx::websvr inherits nginx {
  file {'/etc/nginx/nginx.conf':
  ensure => file,
  mode => '0644',
  owner => 'root',
  group => 'root',
  require => Package['nginx'],
  source => '/tmp/nginx.conf',
  }
  service {'nginx':
  ensure => running,
  }
  
}
  
class nginx::rproxy inherits nginx {
  file {'/etc/nginx/nginx.conf':
  ensure => file,
  mode => '0644',
  owner => 'root',
  group => 'root',
  require => Package['nginx'],
  source => '/tmp/nginx-proxy.conf',
  }
  service {'nginx':
  ensure => running,
  }
  
}
  
include nginx::rproxy
  
[root@node1 ~]# puppet apply test10.pp
  
notice: Finished catalog run in 0.15 seconds
  puppet模块的创建和使用
# 安装puppet-server  
[root@node1 ~]# rpm -ivh puppet-server-2.7.23-1.el6.noarch.rpm
# 创建模块目录结构  
[root@node1 ~]# mkdir -pv /etc/puppet/modules/nginx/{manifests,files,lib,templates,tests,spec}
  
mkdir: created directory `/etc/puppet/modules/nginx'
  
mkdir: created directory `/etc/puppet/modules/nginx/manifests'
  
mkdir: created directory `/etc/puppet/modules/nginx/files'
  
mkdir: created directory `/etc/puppet/modules/nginx/lib'
  
mkdir: created directory `/etc/puppet/modules/nginx/templates'
  
mkdir: created directory `/etc/puppet/modules/nginx/tests'
  
mkdir: created directory `/etc/puppet/modules/nginx/spec'
  
[root@node1 ~]# puppet module list
  
/etc/puppet/modules
  
└── nginx (???)
  
/usr/share/puppet/modules (no modules installed)
  MODULE NAME:模块名称,只能以小写字母开头,也可以包含小写字母、数字和下划线,但不能使用“main”或者“settings”作为模块名;
  manifests目录:包含当前模块的所有manifest文件;每个mainfest文件必须包含一个类或一个定义的类,此文件访问路径格式为"ModuleName::[SubDircetoryName::]ManifestFileName",注意manifest文件不需要其后缀.pp;
  init.pp:只能包含一个单独的类定义,且类的名称必须与模块名称相同;
  files目录:包含了一组静态文件,这些文件可被节点下载使用,每个文件的访问路径遵循
  puppet:///modules/MODULE_NAME/filename路径格式;
  lib目录:插件目录,常用于自定义fact及自定义资源类型等;
  templates目录:存储了manifest用到的模板文件,其访问路径遵循template('ModulName/TemplateName')格式;
  tests目录:当前模块的使用帮助或使用范例文件,类似如何声明当前模块中的类定义的类型等;
  spec目录:类似于tests目录的功能,只不过,其是为lib目录中定义的各插件提供使用的范例的;
  在nginx模块中定义init.pp
[root@node1 ~]# vi /etc/puppet/modules/nginx/manifests/init.pp  
class nginx {
  package {'nginx':
  ensure => installed,
  }
  
}
  定义nginx_web.pp文件
[root@node1 ~]# vi /etc/puppet/modules/nginx/manifests/nginx_web.pp  
class nginx::nginx_web inherits nginx {
  file {'/etc/nginx/nginx.conf':
  ensure => file,
  source => 'puppet:///modules/nginx/nginx-web.conf',
  mode => '0644',
  owner => 'root',
  group => 'root',
  notify => Service['nginx'],
  require => Package['nginx'],
  }
  service {'nginx':
  ensure => running,
  
}
  
}
  
# 准备source文件
  
[root@node1 ~]# cp /tmp/nginx.conf /etc/puppet/modules/nginx/files/nginx-web.conf
  定义nginx_proxy.pp文件
[root@node1 ~]# vi /etc/puppet/modules/nginx/manifests/nginx_proxy.pp  
class nginx::nginx_proxy inherits nginx {
  file {'/etc/nginx/nginx.conf':
  ensure => file,
  source => 'puppet:///modules/nginx/nginx-proxy.conf',
  mode => '0644',
  owner => 'root',
  group => 'root',
  notify => Service['nginx'],
  require => Package['nginx'],
  }
  service {'nginx':
  ensure => running,
  
}
  
}
  
# 准备source文件
  
[root@node1 ~]# cp /tmp/nginx.conf /etc/puppet/modules/nginx/files/nginx-proxy.conf
  创建site.pp文件调用前面定义的class
[root@node1 ~]# vi /etc/puppet/manifests/site.pp  
node 'node1.luojianlong' {
  include nginx::nginx_web
  
}
  执行操作
[root@node1 ~]# puppet apply /etc/puppet/manifests/site.pp  
notice: /Stage[main]/Nginx/Package[nginx]/ensure: created
  
notice: /Stage[main]/Nginx::Nginx_web/File[/etc/nginx/nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}95f45f10386878664af2b7ccd1536ea4'
  
notice: /Stage[main]/Nginx::Nginx_web/Service[nginx]/ensure: ensure changed 'stopped' to 'running'
  
notice: /Stage[main]/Nginx::Nginx_web/Service[nginx]: Triggered 'refresh' from 1 events
  
notice: Finished catalog run in 2.05 seconds
  这样就是使用nginx-web.conf配置文件启动的nginx
  也可以使用nginx-proxy.conf,启动
[root@node1 ~]# vi /etc/puppet/manifests/site.pp  
node 'node1.luojianlong' {
  include nginx::nginx_proxy
  
}
  
[root@node1 ~]# yum -y remove nginx
  
[root@node1 ~]# puppet apply /etc/puppet/manifests/site.pp
  
notice: /Stage[main]/Nginx/Package[nginx]/ensure: created
  
notice: /Stage[main]/Nginx::Nginx_proxy/File[/etc/nginx/nginx.conf]/content: content changed '{md5}d9dfc198c249bb4ac341198a752b9458' to '{md5}95f45f10386878664af2b7ccd1536ea4'
  
notice: /Stage[main]/Nginx::Nginx_proxy/Service[nginx]/ensure: ensure changed 'stopped' to 'running'
  
notice: /Stage[main]/Nginx::Nginx_proxy/Service[nginx]: Triggered 'refresh' from 1 events
  
notice: Finished catalog run in 2.11 seconds
  
# 执行成功
  到此,Puppet条件语句,class,module的使用就介绍这么多了。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-545340-1-1.html 上篇帖子: puppet案例解析 下篇帖子: 【基础】yum本地源 - yum puppet-server为例
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表