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

[经验分享] puppet之模块详解

[复制链接]

尚未签到

发表于 2018-8-2 11:40:27 | 显示全部楼层 |阅读模式
  显示当前系统装载的所有模块信息

  [root@node3>  /etc/puppet/modules (no modules installed)
  /usr/share/puppet/modules (no modulesinstalled)
  no modules installed说明没有任何模块
  在puppet的官方站点已经有许多模块提供给我们使用
  https://forge.puppetlabs.com
  第三方模块未必使用,但是对其框架进行修改有的时候是必要的,所以精确理解模块是什么
  而且在master/agent模型下,几乎都是基于模块进行实现的
  如果我们想查看有哪些模块提供使用只要使用命令puppetmodule search [关键字]
  如下所示

  [root@node3>  Notice: Searching https://forgeapi.puppetlabs.com ...
  NAME                                DESCRIPTION                                           AUTHOR          KEYWORDS
  example42-apache                     Puppet module forapache                              @example42      example42,apache
  如果看到有需要的则进行安装就可以了,如下所示
  [root@node3 ~]# puppet module install 模块名
  
  模块的查看
  [root@node3 ~]# puppet module list
  这个命令上面已经提到了
  查看帮助信息

  [root@node3>  模块的创建
  假如我们想创建一个能够管理nginx的模块,那么这个模块名也叫nginx,由此:

  [root@node3>  创建完其模块对应目录后,我们来查看模块列表
  [root@node3 ~]# mkdir -p/etc/puppet/modules/nginx/{manifests,files,lib,templates,test,spec}
  [root@node3 ~]# puppet module list
  /etc/puppet/modules
  └──nginx (???)
  /usr/share/puppet/modules (no modules installed)
  在/usr/share/puppet/modules/nginx/manifests这个目录下至少需要一个清单文件,叫init.pp 并且能有一个类,这个类还得与当前模块同名
  因此我们为其创建一个空类
  [root@node3 ~]# cd /etc/puppet/modules/nginx/manifests/
  创建清单文件,文件名必须是init.pp
  拷贝配置文件置modules目录
  [root@node3 manifests]# cp /etc/nginx/nginx.conf /etc/puppet/modules/nginx/files/
  执行模块
  [root@node3 manifests]# puppet apply/etc/puppet/modules/nginx/manifests/init.pp
  [root@node3 manifests]# cat init.pp
  class nginx {
  package{'nginx':
  ensure =>installed,
  }
  }
  class nginx::web inherits nginx {
  file{'/etc/nginx/nginx.conf':
  ensure =>file,
  mode =>0644,
  owner =>root,
  group =>root,
  source =>'puppet:///modules/nginx/nginx.conf',
  require =>Package['nginx'],
  }
  service{'nginx':
  ensure =>true,
  subscribe=> File['/etc/nginx/nginx.conf'],
  }
  }
  include nginx::web
  配置站点清单
  每个节点在所谓的master/agent的模式下,angent需要联系master,通过master为自己生成catalog,所以在这种场景下为某个节点定义配置的时候,需要通过站点清单来实现的
  像刚才我们定义的manifests目录,是定义在模块中的
  而在puppet配置文件目录下还需有一个目录为master/angent模式提供清单
  安装puppet-server
  [root@node3 manifests]# yum install puppet-server
  [root@node3 manifests]# cd /etc/puppet/
  [root@node3 puppet]# ll
  total 100
  -rw-r--r-- 1 root  root     175 Aug 21 13:54 1
  -rw-r--r-- 1 root  root    4178 Jun 10 05:09auth.conf
  drwxr-xr-x 3 root  root    4096 Aug 27 13:35environments
  -rw-r--r-- 1 root  root    1462 Jun 10 05:08fileserver.conf
  drwxr-xr-x 2 root  root    4096 Jun 10 05:09manifests
  drwxr-xr-x 3 root  root    4096 Aug 27 11:22 modules
  -rw-r--r-- 1 root  root     853 Jun 10 05:08puppet.conf
  -rw-r--r-- 1 root  root   63344 Aug 22 15:52 puppet.conf.rpmsave
  drwxrwx--x 8 puppet puppet  4096 Aug 21 10:21 ssl
  其中,/etc/puppet/manifests 为站点清单目录,是用来定义各节点的清单,哪个agent到底使用哪个类,就在这里定义
  在manifests目录下创建站点清单文件
  [root@node3 puppet]# cd manifests
  [root@node3 manifests]# ls
  site.pp
  要声明一个站点就要使用node加节点的fqdn ,确保其一定可以被解析,否则无效
  [root@node3 manifests]# hostname
  node3.test.com
  清单文件名必须要以site.pp来命名,否则模块是不会生效的
  node 'node3' {                        #明确指定哪个节点使用哪个模块
  include nginx         #说明文件使用哪个类
  }
  之后还需要定义模块
  [root@node3 manifests]# vim /etc/puppet/modules/nginx/manifests/init.pp
  将include nginx 去掉,因为模块只有被调用的时候才执行,这样才是真正意义上的模块
  应用模块
  [root@node3 manifests]# puppet apply/etc/puppet/manifests/site.pp
  使用继承类
  [root@node3 manifests]# pwd
  /etc/puppet/modules/nginx/manifests
  将其改名
  [root@node3 manifests]# mv init.pp nginxweb.pp
  声明一个空类
  [root@node3 manifests]# cat init.pp
  class nginx {
  }
  而后再编辑nginxweb.pp
  更改参数如下
  class nginx::web inherits nginx
  使其继承父类
  再创建一个类为tengine.pp
  [root@node3 manifests]# cp nginxweb.pp tengine.pp
  [root@node3 manifests]# vim tengine.pp
  更改参数:
  class nginx {
  package{'nginx':
  ensure=> installed,
  }
  }
  class nginx::tengine inherits nginx {
  file{'/etc/nginx/nginx.conf':
  ensure => file,
  mode =>0644,
  owner =>root,
  group =>root,
  source=> 'puppet:///modules/nginx/tengine.conf',
  require=> Package['nginx'],
  }
  service{'nginx':
  ensure=> true,
  subscribe =>File['/etc/nginx/nginx.conf'],
  }
  }
  include nginx::tengine
  这样我们想将站点1安装nginx,而不是安装tengine
  在我们站点清单的里声明即可
  [root@node3 manifests]# vim site.pp
  node 'node3.test.com' {
  includenginx::tengine
  }
  应用测试
  puppet apply site.pp
  Warning: Could not retrieve fact fqdn
  Warning: Host is missing hostname and/or domain: node3
  Notice: Compiled catalog for node3 in environment production in 0.92 seconds
  Error: Execution of '/usr/bin/yum -d 0 -e 0 -y list tengine' returned 1: Error:No matching Packages to list
  Error: /Stage[main]/Nginx::Tengine/Package[tengine]/ensure: change from absentto present failed: Execution of '/usr/bin/yum -d 0 -e 0 -y list tengine'returned 1: Error: No matching Packages to list
  Notice: /Stage[main]/Nginx::Tengine/File[/etc/nginx/nginx.conf]: DependencyPackage[tengine] has failures: true
  Warning: /Stage[main]/Nginx::Tengine/File[/etc/nginx/nginx.conf]: Skippingbecause of failed dependencies
  Notice: /Stage[main]/Nginx::Tengine/Service[nginx]: Dependency Package[tengine]has failures: true
  Warning: /Stage[main]/Nginx::Tengine/Service[nginx]: Skipping because of faileddependencies
  Notice: Finished catalog run in 1.38 seconds
  很显然我们是定义的tengine类 而且yum源中没有这个rpm 所以是不会被安装的
  这样一来我们可以将各个类放在不同的文件中了
  假如node1运行的时候期望运行的是webserver 而另外一个节点则装载的是反向代理的配置
  很显然它们之间的配置是不一样的,但是它们有些东西是一样的,比如安装server 但是装载的文件不一样,因此我们可以更进一步分割类
  区分两者不同
  puppet类不支持多重继承,因此不能多次继承,也不能直接继承多个类
  但是我们可以使init.pp进行安装nginx ,其他模块继承init.pp
  [root@node3 manifests]# pwd
  /etc/puppet/modules/nginx/manifests
  使init.pp只负责安装
  class nginx {
  package {'nginx':
  allow_virtual => false,
  ensure=> installed,
  }
  }
  修改nginxweb.pp使其只配置nginx web server 其父类是nginx
  #只需要将之前的package类剪切至init.pp即可
  [root@node3 manifests]# cat nginx_web.pp
  class nginx::web inherits nginx {
  file{'/etc/nginx/nginx.conf':
  ensure =>file,
  mode =>0644,
  owner =>root,
  group =>root,
  source =>'puppet:///modules/nginx/nginx.conf',
  require =>Package['nginx'],
  }
  service{'nginx':
  ensure =>true,
  subscribe=> File['/etc/nginx/nginx.conf'],
  }
  }
  创建nginx_proxy.pp 文件,使其只配置proxy功能
  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,
  require=> Package['nginx'],
  notify=> Service['nginx'],
  }
  service {'nginx':
  ensure => running,
  }
  }
  回到/etc/puppet/manifests目录
  我们让其安装nginx_web,如下所示:
  [root@node3 manifests]# vim site.pp
  node 'node3' {
  include nginx::nginx_web
  }
  执行脚本并查看结果
  [root@node3 manifests]# puppet apply site.pp
  Notice: Compiled catalog for node3 in environment production in 1.25 seconds
  Notice: /Stage[main]/Nginx/Package[nginx]/ensure: created
  Notice: /Stage[main]/Nginx::Nginx_web/Service[nginx]/ensure: ensure changed'stopped' to 'running'
  Notice: Finished catalog run in 21.85 seconds
  定义模块需要注意的是
  site.pp 文件中的inculude 、模块的名称 、还有定义的class 的名称必须保持一致 否则会报错

运维网声明 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-545320-1-1.html 上篇帖子: puppet之判断和class 下篇帖子: 运维自动化之锋芒初露puppet 基础详解。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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