puppet 实战之新增标准化文件需求
需求:PUPPET(puppet v3.6.0)对线上所有机器(暂时对部分机器(系统版本:centos6)测试)新增标准化配置文件需求
最终配置文件样例:
global: # 公司标识:corp,hans等等 corp:corp # 环境标识:prod、stage、dev、local env:prod # 机房标识:m6、dx、yz、bx zone:m6 # 服务器类别:physical、docker machine:physical moa: # 注册中心 basekeeper: master:redis_basekeeper.base.com slave: - redis_basekeeper_s1.base.com - redis_basekeeper_s2.base.com - redis_basekeeper_s3.base.com # Store注册中心 basestore: zk: - mtp_zk_g3_1.base.com:2183 - mtp_zk_g3_2.base.com:2183 - mtp_zk_g3_3.base.com:2183
入口文件:site.pp (PUPPET/manifests/site.pp) (注:PUPPET为puppet配置主目录)
### 针对主机名以lab-base-0开头的机器做策略 node /^lab-base-0.*.dx/ {
### 包含 role模块下的lab-base.pp文件 (路径:PUPPET/modules/role/manifests/lab-base.pp)
### 这里需要注意 include 后的模块中不能带中横线,应改为下划线 lab-base -> lab_base ### include role::lab-base
include role::lab_base
}
规则文件: lab_base.pp (PUPPET/modules/role/manifests/lab_base.pp)
## role模块的lab-base.pp 继承 role模块也就是这个文件:(role/manifests/init.pp)
class role::lab_base inherits role {
## 包含了profile模块的lab_base.pp文件 (路径: PUPPET/modules/profile/manifests/lab_base.pp)
## include ::profile:lab_base 写法的"::"为从puppet主模块层级,每加一个::,都要往下找一个 "子模块/manifests/" include ::profile::lab_base }
主配置文件: lab_base.pp (PUPPET/modules/profile/manifests/lab_base.pp)
class profile::lab_base {
## 包含facters模块的lab_base.pp (路径: PUPPET/modules/facters/manifests/lab_base.pp)
## 这里包含这个文件的意思是,要根据所管理的机器配置成一个组,组名叫lab_base,且需要在这个组内定义一些变量 include ::facters::lab_base
## 这里的env 就是最关键的标准化配置模块
include ::env }
facters文件: lab_base.pp (PUPPET/modules/facters/manifests/lab_base.pp)
class facters::lab_base {
## 包含了facters的基本配置,下边会展示内容 include facters Class['facters'] -> Class['facters::lab_base']
## 定义一个组名 file { '/etc/facter/facts.d/group.txt': ensure => present,
## 这里定义组名为:lab-base,会从(PUPPET/hieradata/group/lab-base.json) 取值,下边会说到。(注意:include 里不能写中横线,group里是可以写的) content => "group=lab-base\n", } }
facters基本配置文件: init.pp (PUPPET/modules/facters/manifests/init.pp)
class facters { file { '/etc/facter': ensure => directory, } file { '/etc/facter/facts.d': ensure => present, mode => '0755', require => File['/etc/facter'], recurse => true, source => "puppet:///modules/${module_name}/", } }
接下来该说下env标准化模块和facter里group的变量了。
env标准化模块:(PUPPET/modules/env)
tree modules/env/ modules/env/ ├── manifests │ ├── init.pp │ └── lab_base.pp (忽略,暂时没用到) └── templates └── env.yaml.erb
标准化模块配置文件 init.pp (PUPPET/modules/env/manifests/init.pp)
class env (
## base_env_file、base_env_dir 是定义的变量,file资源里会用到 $base_env_file = '/etc/base/env.yaml', $base_env_dir = '/etc/base',
## 以下5个变量为标准化配置的变量,放在这里是作为默认值,假如facter里定义过自定义变量,会覆盖这个值。咱们就是要定义在facter里。 $corp = 'base', $env = 'prod', $moa_basekeeper_master = [], $moa_basekeeper_slave = [], $moa_basestore_zk = [], ) { file { $base_env_file: ensure => file, mode => '0644',
## 这个是标准化配置的模板,渲染进变量即可 content => template("${module_name}/env.yaml.erb"), require => File[$base_env_dir], } file { $base_env_dir: ensure => directory, mode => '0755', recurse => true, } }
标准化配置模板文件: env.yaml.erb (PUPPET/modules/env/templates/env.yaml.erb)
global:
## 此变量是人为定义 corp: <%= @corp %>
## 此变量是人为定义 env: <%= @env %>
## 此变量由服务器facter取
zone: <%= @location %>
## 此变量由服务器facter取
machine: <%= @virtual %> moa: basekeeper:
## 此变量由人为定义 master: <%= @moa_basekeeper_master %>
## 此变量由人为定义
slave: <%= @moa_basekeeper_slave %> basestore:
## 此变量由人为定义 zk: <%= @moa_basestore_zk %>
重点来了: facter变量文件: (PUPPET/hieradata/group/lab-base.json)
{
## 正常写法为:env::lab-base::env 解释为:env是puppet的一个模块目录 lab_base为 env/manifests/lab_base.pp文件 env为该文件里的env变量。
## 这里既然写成env::env 意思是取的是 env/manifests/init.pp 文件里的env 变量。可以省略init "env::corp": "lab", "env::env": "lab", "env::moa_basekeeper_master": [], "env::moa_basekeeper_slave": [], "env::moa_basestore_zk": [] }
hireadata主文件: hiera.yaml (PUPPET/hiera.yaml)
--- :backends: - json # if datadir is empty , hiera uses its defaults: # - /var/lib/hiera on *nix # - %CommonAppData%\PuppetLabs\hiera\var on Windows # When specifying a datadir, make sure the directory exists. :json: :datadir: /etc/puppet/hieradata :hierarchy:
## 优先级逐渐变低 - node/%{::fqdn} - location/%{::location}
## 我们用到的是group里的变量。 - group/%{::group} - os/%{::operatingsystem}%{::operatingsystemmajrelease} - common # options are native, deep, deeper :merge_behavior: deeper :logger: console