puppet 配置 5 常见例子

变量复用

说明:

变量定义后可以直接调用变量名称使用

范例:

$etcd_controller1='10.100.84.22'
$etcd_controller2='10.100.84.23'
$etcd_controller3='10.100.84.24'

$etcd_host1='gx-yun-084022.vclound.com'
$etcd_host2='gx-yun-084023.vclound.com'
$etcd_host3='gx-yun-084024.vclound.com'

$etcd_connect="$etcd_host1=http://$etcd_host1:2380,$etcd_host2=http://$etcd_host2:2380,$etcd_host3=http://$etcd_host3:2380"

$etcdcluster="$etcd_controller1:2379,$etcd_controller2:2379,$etcd_controller3:2379"

变量的判断

说明:

1. 对主机地址进行匹配,  (可以利用 case , if 的方式进行判断, 效果一样)
2. 针对不同的主机, 定义不同的变量使用

范例

$myipaddress=$ipaddress_vlanbr0

if $myipaddress =~ /^10\.201\.\\*/ {
    $ntpserver = '10.201.100.21'
} elsif  $myipaddress =~ /^10\.200\.\\*/  {
    $ntpserver = '10.200.100.21'
} elsif $myipaddress =~ /^10\.205\.\\*/  {
    $ntpserver = '10.205.100.25'
} elsif $myipaddress =~ /^192\.168\.\\*/ {
    $ntpserver = [ '10.199.129.21', '10.199.129.22' ]
} elsif $myipaddress =~ /^10\.100\.\\*/ {
    $ntpserver = [ '10.199.129.21', '10.199.129.22' ]
}

case 语法

作用:

1.  对主机地址进行匹配, 
2.  利用匹配规则,  令不同的主机执行不同的命令

范例:

case $myipaddress {
  $etcd_controller1, $etcd_controller2, $etcd_controller3 : {
    exec { 'exit':
      user => root, group => root,
      path => '/bin:/sbin:/usr/bin:/usr/sbin',
      unless => [ "/usr/bin/nmap $fqdn -p 2380 | /usr/bin/grep -o open" ],
      require => [ Package['nmap'], Class['initial'], Class['etcd::service']  ],
    }
  }

  default : {
    exec { 'exit':
      user => root, group => root,
      path => '/bin:/sbin:/usr/bin:/usr/sbin',
      unless => [ "/usr/bin/nmap $etcd_controller1 -p 2380 | /usr/bin/grep -o open &&  /usr/bin/nmap $etcd_controller2 -p 2380 | /usr/bin/grep -o open &&  /usr/bin/nmap $etcd_controller3 -p 2380 | /usr/bin/grep -o open"  ],
      require => Package['nmap'],
    }
  }
}

配置文件特殊语法

参考下面语法 (config.pp)

convoy_config {
    'Service/LimitMEMLOCK':  value => $convoyLimitMEMLOCK;
    'Service/LimitSTACK':  value => $convoyLimitSTACK;
    'Service/LimitNPROC':  value => $convoyLimitNPROC;
    'Service/LimitNOFILE':  value => $convoyLimitNOFILE;
    'Service/LimitCORE':  value => $convoyLimitCORE;
}

当执行 puppet 后, 可以得到下面的常见的配置文件配置结果 (实际会得到 等号后的变量值 )

[Service]
LimitMEMLOCK = $convoyLimitMEMLOCK
LimitSTACK = $convoyLimitSTACK
LimitNPROC = $convoyLimitNPROC
LimitNOFILE = $convoyLimitNOFILE
LimitCORE = $convoyLimitCORE

要使用这样的配置方法, 必须添加下面两个模块库文件

/etc/puppet/modules/convoy
├── lib
│   └── puppet
│       ├── provider
│       │   └── convoy_config
│       │       └── ini_setting.rb    <- 添加
│       └── type
│           └── convoy_config.rb       <- 添加
├── manifests
│   ├── config.pp                   <- 添加后, 配置才生效
│   ├── directlvm.pp
│   ├── init.pp

/etc/puppet/modules/convoy/lib/puppet/provider/convoy_config/ini_setting.rb

Puppet::Type.type(:convoy_config).provide(
  :ini_setting,
  :parent => Puppet::Type.type(:ini_setting).provider(:ruby)
) do

  def section
    resource[:name].split('/', 2).first
  end

  def setting
    resource[:name].split('/', 2).last
  end

  def separator
    '='
  end

  def self.file_path
    '/usr/lib/systemd/system/convoy.service'
  end

  def file_path
     self.class.file_path
  end
end

/etc/puppet/modules/convoy/lib/puppet/type/convoy_config.rb

Puppet::Type.newtype(:convoy_config) do

  ensurable

  newparam(:name, :namevar => true) do
    desc 'Section/setting name to manage from /usr/lib/systemd/system/convoy.service'
    newvalues(/\S+\/\S+/)
  end

  newproperty(:value) do
    desc 'The value of the setting to be defined.'
    munge do |value|
      value = value.to_s.strip
      value.capitalize! if value =~ /^(true|false)$/i
      value
    end
  end

end

firewalld 配置

模块下载位置

https://github.com/crayfishx/puppet-firewalld

配置语法
一段端口配置方法

firewalld_port { 'Open port 1000-50000 in the public zone tcp':
            ensure   => present,
            zone     => 'public',
            port     => 1000-50000,
            protocol => 'tcp',
}

firewalld_port { 'Open port 1000-50000 in the public zone udp':
            ensure   => present,
            zone     => 'public',
            port     => 1000-50000,
            protocol => 'udp',
}

一个端口配置方法

firewalld_port { 'Open port 2476 in the public zone':
            ensure   => present,
            zone     => 'public',
            port     => 2476,
            protocol => 'tcp',
}

firewalld_port { 'Open port 8080 in the public zone':
            ensure   => present,
            zone     => 'public',
            port     => 8080,
            protocol => 'tcp',
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Write custom plugins for Puppet, including facts, providers, and functions Key Features Grasp recipes that work with centralized and decentralized deployments Explore language differences and enhancements anticipated in Puppet version 5.x Gain expert understanding of Puppet's latest and most advanced features Book Description Puppet is a configuration management system that automates all your IT configurations, giving you control of managing each node.Puppet 5 Cookbook will take you through Puppet's latest and most advanced features, including Docker containers, Hiera, and AWS Cloud Orchestration. Updated with the latest advancements and best practices, this book delves into various aspects of writing good Puppet code, which includes using Puppet community style, checking your manifests with puppet-lint, and learning community best practices with an emphasis on real-world implementation. You will learn to set up, install, and create your first manifests with Puppet version control, and also understand various sysadmin tasks, including managing config files, using Augeas, and generating files from snippets and templates. As the book progresses, you'll explore virtual resources and use Puppet's resource scheduling and auditing features. In the concluding chapters, you'll walk through managing applications and writing your own resource types, providers, and external node classifiers. By the end of this book, you will have learned to report, log, and debug your system. What you will learn Discover the latest and most advanced features of Puppet Bootstrap your Puppet installation using powerful tools like Rake Master techniques to deal with centralized and decentralized Puppet deployments Use exported resources and forge modules to set up Puppet modules Create efficient manifests to streamline your deployments Automate Puppet master deployment using Git hooks and PuppetDB Make Puppet reliable, performant, and scalable Who This Book Is For Puppet 5 Cookbook is for

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值