官术网_书友最值得收藏!

Using selectors and case statements

Although you could write any conditional statement using if, Puppet provides a couple of extra forms to help you express conditionals more easily: the selector and the case statement.

How to do it…

Here are some examples of selector and case statements:

  1. Add the following code to your manifest:
    $systemtype = $::operatingsystem ? {
      'Ubuntu' => 'debianlike',
      'Debian' => 'debianlike',
      'RedHat' => 'redhatlike',
      'Fedora' => 'redhatlike',
      'CentOS' => 'redhatlike',
      default  => 'unknown',
    }
    
    notify { "You have a ${systemtype} system": }
  2. Add the following code to your manifest:
    class debianlike {
      notify { 'Special manifest for Debian-like systems': }
    }
    
    class redhatlike {
      notify { 'Special manifest for RedHat-like systems': }
    }
    
    case $::operatingsystem {
      'Ubuntu',
      'Debian': {
        include debianlike
      }
      'RedHat',
      'Fedora',
      'CentOS': {
        include redhatlike
      }
      default: {
        notify { "I don't know what kind of system you have!": }
      }
    }

How it works…

Our example demonstrates both the selector and the case statement, so let's see in detail how each of them works.

Selector

In the first example, we used a selector (the ? operator) to choose a value for the $systemtype variable depending on the value of $::operatingsystem. This is similar to the ternary operator in C or Ruby, but instead of choosing between two possible values, you can have as many values as you like.

Puppet will compare the value of $::operatingsystem to each of the possible values we have supplied: Ubuntu, Debian, and so on. These values could be regular expressions (for example, for a partial string match, or to use wildcards), but in our case we have just used literal strings. As soon as it finds a match, the selector expression returns whatever value is associated with the matching string. If the value of $::operatingsystem is Fedora, for example, the selector expression will return the string redhatlike and this will be assigned to the variable $systemtype.

Case statement

Unlike selectors, the case statement does not return a value. case statements are handy where you want to execute different code depending on the value of some expression. In our second example, we used the case statement to include either the class debianlike or the class redhatlike, depending on the value of $::operatingsystem.

Again, Puppet compares the value of $::operatingsystem to a list of potential matches. These could be regular expressions or strings, or as in our example, comma-separated lists of strings. When it finds a match, the associated code between curly braces is executed. So, if the value of $::operatingsystem is Ubuntu, then the code include debianlike will be executed.

There's more…

Once you've got the grip with the basic use of selectors and case statements, you may find the following tips useful:

Regular expressions

As with if statements, you can use regular expressions with selectors and case statements, and you can also capture the values of the matched groups and refer to them using $1, $2, and so on:

case $::lsbdistdescription {
  /Ubuntu (.+)/: {
    notify { "You have Ubuntu version ${1}": }
  }
  /CentOS (.+)/: {
    notify { "You have CentOS version ${1}": }
  }
  default: {}
}

Defaults

Both selectors and case statements let you specify a default value, which is chosen if none of the other options match:

$lunch = 'Burger and fries'
$lunchtype =  $lunch ? {
  /fries/ => 'unhealthy',
  /salad/ => 'healthy',
  default => 'unknown',
}

notify { "Your lunch was ${lunchtype}": }
Your lunch was unhealthy
主站蜘蛛池模板: 三门县| 怀宁县| 宜昌市| 新兴县| 岗巴县| 连云港市| 永康市| 湘乡市| 旅游| 克什克腾旗| 依安县| 横山县| 准格尔旗| 阿拉善盟| 温州市| 隆化县| 营口市| 凉城县| 梅州市| 应城市| 特克斯县| 金沙县| 玛纳斯县| 桑植县| 五常市| 南安市| 平远县| 临湘市| 伽师县| 丰镇市| 巴彦淖尔市| 玉林市| 丘北县| 察隅县| 青浦区| 井冈山市| 绿春县| 都江堰市| 泸西县| 临江市| 龙南县|