puppet自動化運維之條件語句

 


1.if語句:

    Perl 不一樣於其它的一些語言,它沒有Boolean 類型,徹底與別的語言相反正則表達式

若是值爲數字【驗證失敗】,shell

0false學習

其他爲真。spa

若是值爲字符串,【驗證過了,成立】.net

空串('')爲false3d

其他爲真。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.

 

 

2. case語句:

    通常用於判斷給定的值(或變量),是不是case裏的值,若是是,就執行其後面的命令,若是都不在case裏,則執行default後的,通常default需存在,相似shell裏的case。

    case指定一個默認值(default,這是在沒有其它選項匹配的時候纔會使用。

       能夠在selectorscase語句中使用正則表達式。

格式:

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",

}

 

#能夠在selectorscase語句中使用正則表達式,

case $lsbdistdescription {

/Ubuntu (.+)/: {

notify { "You have Ubuntu version ": }

}

/CentOS (.+)/: {

notify { "You have CentOS version ": }

}

}

 

 

 

3. selector選擇器:

    selector指定一個默認值(default,這是在沒有其它選項匹配的時候纔會使用。

       能夠在selectorscase語句中使用正則表達式

格式:

$變量= 給定值 ?{                                                                          

    值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的條件語句就結束了,接下來的是的學習,請聽下回分解!!!

相關文章
相關標籤/搜索