- Puppet:Mastering Infrastructure Automation
- Felix Frank Martin Alfke Alessandro Franceschi Jaime Soriano Pastor Thomas Uphillis
- 254字
- 2021-07-09 18:59:00
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.
- Boost.Asio C++ Network Programming(Second Edition)
- C語言程序設計教程
- PHP 從入門到項目實踐(超值版)
- MariaDB High Performance
- Modern JavaScript Applications
- SQL Server 2016數據庫應用與開發
- Learning Vaadin 7(Second Edition)
- Python算法指南:程序員經典算法分析與實現
- Building Dynamics CRM 2015 Dashboards with Power BI
- 一步一步跟我學Scratch3.0案例
- Python硬件編程實戰
- 讓Python遇上Office:從編程入門到自動化辦公實踐
- C語言程序設計
- Unity 3D UI Essentials
- Building Web and Mobile ArcGIS Server Applications with JavaScript(Second Edition)