hti 发表于 2018-8-2 11:40:27

puppet之模块详解

  显示当前系统装载的所有模块信息

  [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
  如果看到有需要的则进行安装就可以了,如下所示
  # puppet module install 模块名
  
  模块的查看
  # puppet module list
  这个命令上面已经提到了
  查看帮助信息

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

  [root@node3>  创建完其模块对应目录后,我们来查看模块列表
  # mkdir -p/etc/puppet/modules/nginx/{manifests,files,lib,templates,test,spec}
  # puppet module list
  /etc/puppet/modules
  └──nginx (???)
  /usr/share/puppet/modules (no modules installed)
  在/usr/share/puppet/modules/nginx/manifests这个目录下至少需要一个清单文件,叫init.pp 并且能有一个类,这个类还得与当前模块同名
  因此我们为其创建一个空类
  # cd /etc/puppet/modules/nginx/manifests/
  创建清单文件,文件名必须是init.pp
  拷贝配置文件置modules目录
  # cp /etc/nginx/nginx.conf /etc/puppet/modules/nginx/files/
  执行模块
  # puppet apply/etc/puppet/modules/nginx/manifests/init.pp
  # 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
  # yum install puppet-server
  # cd /etc/puppet/
  # ll
  total 100
  -rw-r--r-- 1 rootroot   175 Aug 21 13:54 1
  -rw-r--r-- 1 rootroot    4178 Jun 10 05:09auth.conf
  drwxr-xr-x 3 rootroot    4096 Aug 27 13:35environments
  -rw-r--r-- 1 rootroot    1462 Jun 10 05:08fileserver.conf
  drwxr-xr-x 2 rootroot    4096 Jun 10 05:09manifests
  drwxr-xr-x 3 rootroot    4096 Aug 27 11:22 modules
  -rw-r--r-- 1 rootroot   853 Jun 10 05:08puppet.conf
  -rw-r--r-- 1 rootroot   63344 Aug 22 15:52 puppet.conf.rpmsave
  drwxrwx--x 8 puppet puppet4096 Aug 21 10:21 ssl
  其中,/etc/puppet/manifests 为站点清单目录,是用来定义各节点的清单,哪个agent到底使用哪个类,就在这里定义
  在manifests目录下创建站点清单文件
  # cd manifests
  # ls
  site.pp
  要声明一个站点就要使用node加节点的fqdn ,确保其一定可以被解析,否则无效
  # hostname
  node3.test.com
  清单文件名必须要以site.pp来命名,否则模块是不会生效的
  node 'node3' {                        #明确指定哪个节点使用哪个模块
  include nginx         #说明文件使用哪个类
  }
  之后还需要定义模块
  # vim /etc/puppet/modules/nginx/manifests/init.pp
  将include nginx 去掉,因为模块只有被调用的时候才执行,这样才是真正意义上的模块
  应用模块
  # puppet apply/etc/puppet/manifests/site.pp
  使用继承类
  # pwd
  /etc/puppet/modules/nginx/manifests
  将其改名
  # mv init.pp nginxweb.pp
  声明一个空类
  # cat init.pp
  class nginx {
  }
  而后再编辑nginxweb.pp
  更改参数如下
  class nginx::web inherits nginx
  使其继承父类
  再创建一个类为tengine.pp
  # cp nginxweb.pp tengine.pp
  # 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
  在我们站点清单的里声明即可
  # 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/Nginx::Tengine/Package/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/Nginx::Tengine/File: DependencyPackage has failures: true
  Warning: /Stage/Nginx::Tengine/File: Skippingbecause of failed dependencies
  Notice: /Stage/Nginx::Tengine/Service: Dependency Packagehas failures: true
  Warning: /Stage/Nginx::Tengine/Service: Skipping because of faileddependencies
  Notice: Finished catalog run in 1.38 seconds
  很显然我们是定义的tengine类 而且yum源中没有这个rpm 所以是不会被安装的
  这样一来我们可以将各个类放在不同的文件中了
  假如node1运行的时候期望运行的是webserver 而另外一个节点则装载的是反向代理的配置
  很显然它们之间的配置是不一样的,但是它们有些东西是一样的,比如安装server 但是装载的文件不一样,因此我们可以更进一步分割类
  区分两者不同
  puppet类不支持多重继承,因此不能多次继承,也不能直接继承多个类
  但是我们可以使init.pp进行安装nginx ,其他模块继承init.pp
  # 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即可
  # 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,如下所示:
  # vim site.pp
  node 'node3' {
  include nginx::nginx_web
  }
  执行脚本并查看结果
  # puppet apply site.pp
  Notice: Compiled catalog for node3 in environment production in 1.25 seconds
  Notice: /Stage/Nginx/Package/ensure: created
  Notice: /Stage/Nginx::Nginx_web/Service/ensure: ensure changed'stopped' to 'running'
  Notice: Finished catalog run in 21.85 seconds
  定义模块需要注意的是
  site.pp 文件中的inculude 、模块的名称 、还有定义的class 的名称必须保持一致 否则会报错
页: [1]
查看完整版本: puppet之模块详解