Perl 不一样于其它的一些语言,它没有Boolean 类型,彻底与别的语言相反。正则表达式
若是值为数字【验证失败】,shell
0是false;学习
其他为真。spa
若是值为字符串,【验证过了,成立】.net
空串('')为false;3d
其他为真。server
最好使用:ip
true 真md5
false 假ci
#true if true { $a='true' }else { $a='false' }
file {"/tmp/temp6.txt": content => "$a\n", } #check [root@client ~]# puppet agent -vv --test --server master.perofu.com info: Caching catalog for client.perofu.com info: Applying configuration version '1395175538' notice: /Stage[main]//File[/tmp/temp6.txt]/ensure: defined content as '{md5}74d9a83219cabaab06a69fd318873f33' notice: Finished catalog run in 0.03 seconds [root@client ~]# [root@client ~]# cat /tmp/temp6.txt true
#false if false { $a='true' }else { $a='false' }
file {"/tmp/temp6.txt": content => "$a\n", }
#check [root@client ~]# puppet agent -vv --test --server master.perofu.com info: Caching catalog for client.perofu.com info: Applying configuration version '1395175598' notice: /Stage[main]//File[/tmp/temp6.txt]/ensure: defined content as '{md5}d42f2da1df5ecdf29be4ac27edda0c12' notice: Finished catalog run in 0.03 seconds [root@client ~]# [root@client ~]# cat /tmp/temp6.txt false
|
格式:
①:
if 条件{ 命令 } |
②:
if 条件 { 命令 } else { 命令 } |
③:
if 条件 { 命令 } elseif{ 命令 } |
④:
if 条件 { 命令 } elseif{ 命令 } else { 命令 } |
实例:
if $lsbdistid == "Ubuntu" { $a="Running on Ubuntu" }elsif $lsbdistid == "Debian" { $a="Close enough..." } else { $a="Non-Ubuntu system detected. Please upgrade to Ubuntu immediately." } file {"/tmp/temp6.txt": content => "$a", }
# [root@client ~]# puppet agent -vv --test --server master.perofu.com info: Caching catalog for client.perofu.com info: Applying configuration version '1395165888' notice: /Stage[main]//File[/tmp/temp6.txt]/ensure: defined content as '{md5}158229a70450d3d384f4ec323716b680' notice: Finished catalog run in 0.02 seconds [root@client ~]# cat /tmp/temp6.txt Non-Ubuntu system detected. Please upgrade to Ubuntu immediately. |
通常用于判断给定的值(或变量),是不是case里的值,若是是,就执行其后面的命令,若是都不在case里,则执行default后的,通常default需存在,相似shell里的case。
case指定一个默认值(default),这是在没有其它选项匹配的时候才会使用。
能够在selectors和case语句中使用正则表达式。
格式:
case 给定值{ 值1: {命令} … 值n: {命令} default: {命令} } |
实例:
case $operatingsystem { CentOS: {$mycontent="my system is CentOS"} redhat: {$mycontent="my system is redhat"} default: {$mycontent="my system is unknow"} }
file {"/tmp/temp6.txt": content => "$mycontent", }
[root@client ~]# puppet agent -vv --test --server master.perofu.com info: Caching catalog for client.perofu.com info: Applying configuration version '1395166863' notice: /Stage[main]//File[/tmp/temp6.txt]/ensure: defined content as '{md5}e42a98516324383115c060b663002217' notice: Finished catalog run in 0.02 seconds [root@client ~]# cat /tmp/temp6.txt my system is CentOS
#同上 case "CentOS" { CentOS: {$mycontent="my system is CentOS"} redhat: {$mycontent="my system is redhat"} default: {$mycontent="my system is unknow"} }
file {"/tmp/temp6.txt": content => "$mycontent", }
#能够在selectors和case语句中使用正则表达式, case $lsbdistdescription { /Ubuntu (.+)/: { notify { "You have Ubuntu version ": } } /CentOS (.+)/: { notify { "You have CentOS version ": } } } |
selector指定一个默认值(default),这是在没有其它选项匹配的时候才会使用。
能够在selectors和case语句中使用正则表达式。
格式:
$变量= 给定值 ?{ 值1 => "字符串", … 值n => "字符串", default => "字符串", } |
详解:若是给定值和值1,匹配,则将值1的字符串赋值给$变量,以此类推,若是给定值没有匹配到值n,则将default的值,赋值给$变量。
至关于,给定值和值1,是中间人,$变量和字符串,才是结果。
实例:
# $mycontent= $operatingsystem ? { "Ubuntu" => "debianlike", "Debian" => "debianlike", "RedHat" => "redhatlike", "Fedora" => "redhatlike", "CentOS" => "redhatlike", default => "unknown", }
file {"/tmp/temp6.txt": content => "$mycontent", }
# [root@client ~]# puppet agent -vv --test --server master.perofu.com info: Caching catalog for client.perofu.com info: Applying configuration version '1395169003' notice: /Stage[main]//File[/tmp/temp6.txt]/ensure: defined content as '{md5}a4a4954ad056af69d1675d6b6ff72244' notice: Finished catalog run in 0.04 seconds [root@client ~]# cat /tmp/temp6.txt redhatlike
#正则表达式,使用正则表达式,须要使用//括起。 $lunch = "Sausage and chips" $lunchtype = $lunch ? { /chips/ => "unhealthy", /salad/ => "healthy", default => "unknown", } |
至此,puppet的条件语句就结束了,接下来的是类的学习,请听下回分解!!!