赤色烙印 发表于 2018-8-1 13:09:33

Puppet node节点的特性(十二)

  前言:
  生产机器很多通常会新建nodes.pp文件和site.pp文件平级,存放于/etc/puppet/manifests/nodes.pp文件,这种方法比较常用.当然也有其他办法直接写入site.pp文件.
  nodes.pp文件主机匹配,支持正则表达式和继承.
  //:正则匹配
  "":精确匹配
  inherits:继承
  实例:
  先正则匹配然后在精确匹配.
node /sh-(proxy|web)\d+/ {  
case $::hostname {
  
    "sh-proxy2": {
  
         include apache
  
         user {"test1":
  
            ensure => present,
  
            }
  
      }
  
   "sh-web1": {
  
      include nginx::nginxconf
  
         }
  
    }
  
}
  比如sh-proxy不止sh-proxy2一台,有50台也可以匹配操作:
node /sh-(proxy|web)\d+/ {  
case $::hostname {
  
    /sh-proxy\d+/: {      #正则匹配
  
         include apache
  
         user {"test1":
  
            ensure => present,
  
            }
  
      }
  
   "sh-web1": {
  
            include nginx::nginxconf
  
         }
  
    }
  
}
  测试agent端更新:
# puppet agent -t  
Info: Retrieving pluginfacts
  
Info: Retrieving plugin
  
Info: Caching catalog for sh-proxy2.localdomain
  
Info: Applying configuration version '1505706252'
  
Notice: /Stage/Apache/Package/ensure: created
  
Notice: Finished catalog run in 8.69 seconds
  node节点继承(inherits)
  1、新建一个admin模块,做selinux替换关闭.
  /modules/admin/manifests/init.pp文件内容:
class admin {  
    exec {"selinux":
  
    command => "sed -i '/^SELINUX=/s/=.*/=disabled/g' /etc/sysconfig/selinux",
  
    path => ["/bin/","/sbin/","/usr/bin/","/usr/sbin/"],
  
    user => root,
  
    group => root,
  
    }
  
}

  注意:尝试定义类名'base'报错,现象:svn无法提交,但却可以更新,update后获取到master主机ip地址目录等信息,不确实是否和系统内置变量冲突.
manifests/nodes.pp文件内容.  
    node base {
  
      include admin
  
}
node /sh-(proxy|web)\d+/inherits base {  
case $::hostname {
  
    /sh-proxy\d+/: {
  
         include apache
  
         user {"test1":
  
            ensure => present,
  
            }
  
      }
  
   "sh-web1": {
  
      include nginx::nginxconf
  
         }
  
    }
  
}

  注意:node节点名不能和模块名冲突,尝试定义为node admin,提交无报错,但是agent端无更新无修改无提示,修改为base以后agent端内容更新.
页: [1]
查看完整版本: Puppet node节点的特性(十二)