變量: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, }