变量:http://docs.puppetlabs.com/facter/latest/core_facts.htmlhtml
http://docs.puppetlabs.com/puppet/latest/reference/lang_variables.htmllinux
语句:http://docs.puppetlabs.com/puppet/latest/reference/lang_conditional.htmlexpress
http://docs.puppetlabs.com/puppet/latest/reference/lang_expressions.htmlmacos
一、变量centos
定义变量:以$开头,如:$system、$flagruby
赋值:= ,如:$one = "first one"app
引用变量:有三种方法以下less
$var = "Hello World!" notice "1.$var" notice "2.${var}" notice "3.$::var"
输出:dom
[root@pclient test]# puppet apply 3.pp Notice: Scope(Class[main]): 1.Hello World! Notice: Scope(Class[main]): 2.Hello World! Notice: Scope(Class[main]): 3.Hello World! Notice: Compiled catalog for pclient.onepc.com in environment production in 0.06 seconds Notice: Finished catalog run in 0.05 seconds
puppet代码中能够直接使用的变量:ssh
http://docs.puppetlabs.com/facter/latest/core_facts.html
Summary architecture augeasversion blockdevice_{devicename}_size blockdevice_{devicename}_vendor blockdevice_{devicename}_model blockdevices boardmanufacturer boardproductname boardserialnumber cfkey domain ec2_{EC2 INSTANCE DATA} ec2_userdata facterversion filesystems ldom fqdn gid hardwareisa hardwaremodel hostname id interfaces ipaddress ipaddress_{NETWORK INTERFACE} ipaddress6 ipaddress6_{NETWORK INTERFACE} iphostnumber is_virtual kernel kernelmajversion kernelrelease kernelversion lsbdistcodename lsbdistdescription lsbdistid lsbdistrelease lsbmajdistrelease lsbminordistrelease lsbrelease macaddress macaddress_{NETWORK INTERFACE} macosx_buildversion macosx_productname macosx_productversion macosx_productversion_major macosx_productversion_minor manufacturer memoryfree memorysize netmask netmask_{NETWORK INTERFACE} network_{NETWORK INTERFACE} operatingsystem operatingsystemmajrelease operatingsystemrelease osfamily path physicalprocessorcount processor processor{NUMBER} processorcount productname ps puppetversion rubysitedir rubyversion selinux selinux_config_mode selinux_config_policy selinux_current_mode selinux_enforced selinux_policyversion serialnumber sp_{SYSTEM PROFILER DATA} sshdsakey sshecdsakey sshrsakey swapencrypted swapfree swapsize timezone type uniqueid uptime uptime_days uptime_hours uptime_seconds uuid virtual vlans xendomains zfs_version zonename zones zpool_version
二、if语句
if 条件 {
语句
}
条件:是true执行语句,是false则跳过语句
$varif = 20 if $varif > 10 { notice "$varif > 10" } else { notice "$varif < 10" }
注意:elsif 不是 elseif if $varif == 30 { notice "varif=30" } elsif $varif > 30 {notice "varif>30" } else { notice "varif < 30" }
三、unless语句
unless 条件 {
语句
}
条件:当false时,执行语句。当true时跳过。
$varless = 100 unless $varless > 1000 { notice "$varless > 1000 is false" }
Notice: Scope(Class[main]): 100 > 1000 is false
四、case语句
case 变量/表达式 {
值1 :{语句1}
值2 : {语句2}
default : {不是上面的值时,执行这条语句}
}
$varcase = 100 case $varcase { 10: {notice "varcase = 10"} 20: {notice "varcase = 20"} 100: {notice "varcase = 100"} default: {notice "varcase is match"} }
Notice: Scope(Class[main]): varcase = 100
五、selectors语句
未知变量 = 可知变量 ? {
值1 => 赋值1,
值2 => 赋值2,
default => 赋值3,
}
能够用于系统版本之类的判断
$varselectors = "puppet" $varsel = $varselectors ? { "centos" => "this is centos", "puppet" => "this is puppet", default => "this is default", } notice "varsel is value = \"$varsel\""
Notice: Scope(Class[main]): varsel is value = "this is puppet"
官网例子:
$rootgroup = $osfamily ? { 'Solaris' => 'wheel', /(Darwin|FreeBSD)/ => 'wheel', default => 'root', } file { '/etc/passwd': ensure => file, owner => 'root', group => $rootgroup, }