puppet 语法学习

  1. 条件语句
    • case 语句
case $operatingsystem {
redhat: { service { "httpd":   ensure => running }}
debian:  { service { "apache":  ensure => running }}
default: { service { "apache2": ensure => running }}
} 
  •  if 条件语句
 if $is_virtual {
warning('target is virtual.')
}
elsif $operatingsystem == 'CentOS' {
warning('module doesnt work on this OS.')
}
else {
include roles::base
}
  • 选择语句
$rootgroup = $osfamily ? {
 'Solaris' => 'wheel',
 /(Darwin|FreeBSD)/ => 'wheel',
 default => 'root',
}
  • 除非语句
 unless $memorysize > 2048 {
$maxclient = 1000
}
  • in语句
if $monitortool in [ 'zabbix','nagios','cacti' ]

 

puppet resouces 使用示例

  • user 使用示例:
user {"nginx":
ensure => present,
shell => "/sbin/nologin",
mangehome => true,
comment => "nginx user",
}
  • group 使用示例:
group { "nginx":
        ensure => present,
        gid => 1000,
       }
  • package 使用示例:
 packages { "nginx":
           ensure =>present,
        } 
  • service 使用示例:
service { "nginx": 
        ensure => running,
        enable => true, 
     hasstatus => true,
    hasrestart => true,
       pattern => nginx,
       require => Package['nginx'],
    }
  • cron 使用示例:
 cron { "clean-files":
ensure => present,
user => 'root',
command => '/usr/local/bin/clean-files',
minute => '1',
hour => '3',
weekday => [ '2', '6' ],
environment => 'MAILTO=puppetfans@163.com',
} 
  • exec 使用示例:

exec { 'perl -MCPAN -e "install YAML"':
path => '/bin:/usr/bin',
unless => 'cpan -l | grep -qP ^YAML\\b',
}
  • file 使用示例:
 file { "nginx.conf":
 ensure  => present,
 path    => "/etc/nginx/nginx.conf",
 mode    => 644,
 owner   => root,
 group   => root,
 require => Package['nginx'],
 notify  => Service['nginx'],
 source  => "puppet:///modules/hotpu/nginx/nginx.conf",
 }
  • mount 使用示例:
mount { '/media/gluster-data':
ensure => 'mounted',
device => 'gluster01:/data',
fstype => 'glusterfs',
options => 'defaults,_netdev',
dump => 0,
pass => 0,
}
  • 虚拟资源使用示例:
@@user { "nginx":
 ensure => present,
 tag => nginx,
 }
  • 实现虚拟资源使用示例:
 realize User["nginx"]
User <| tag == nginx |>