- Puppet 4 Essentials(Second Edition)
- Felix Frank Martin Alfke
- 254字
- 2021-07-30 09:58:42
Adding control structures in manifests
You have written three simple manifests while following the instructions in this chapter so far. Each comprised only one resource, and one of them was given on the command line using the -e
option. Of course, you would not want to write distinct manifests for each possible circumstance. Instead, just as how Ruby or Perl scripts branch out into different code paths, there are structures that make your Puppet code flexible and reusable for different circumstances.
The most common control element is the if
/else
block. It is quite similar to its equivalents in many programming languages:
if 'mail_lda' in $needed_services { service { 'dovecot': enable => true } } else { service { 'dovecot': enable => false } }
The Puppet DSL also has a case
statement, which is reminiscent of its counterparts in other languages as well:
case $role { 'imap_server': { package { 'dovecot': ensure => 'installed' } service { 'dovecot': ensure => 'running' } } /_webserver$/: { service { [ 'apache', 'ssh' ]: ensure => 'running' } } default: { service { 'ssh': ensure => running } } }
A variation of the case
statement is the selector. It's an expression, not a statement, and can be used in a fashion similar to the ternary if
/else
operator found in C-like languages:
package { 'dovecot': ensure => $role ? { 'imap_server' => 'installed', /desktop$/ => 'purged', default => 'removed', }, }
It should be used with caution, because in more complex manifests, this syntax will impede readability.
- Deploying Node.js
- What's New in TensorFlow 2.0
- Java高并發(fā)核心編程(卷2):多線程、鎖、JMM、JUC、高并發(fā)設(shè)計模式
- Mastering Objectoriented Python
- OpenCV 3和Qt5計算機(jī)視覺應(yīng)用開發(fā)
- 信息安全技術(shù)
- 深入淺出PostgreSQL
- Learning YARN
- Citrix XenServer企業(yè)運(yùn)維實戰(zhàn)
- 大話Java:程序設(shè)計從入門到精通
- MINECRAFT編程:使用Python語言玩轉(zhuǎn)我的世界
- Spring技術(shù)內(nèi)幕:深入解析Spring架構(gòu)與設(shè)計原理(第2版)
- R語言數(shù)據(jù)可視化:科技圖表繪制
- Mastering Elasticsearch(Second Edition)
- Oracle實用教程