- Puppet 5 Essentials(Third Edition)
- Martin Alfke Felix Frank
- 667字
- 2021-07-02 18:22:24
The exec resource type
There is one oddball resource type in the Puppet core. Remember our earlier assertion that Puppet is not a specialized scripting engine, but a tool that allows you to model part of your system state in a compelling DSL, and which is capable of altering your system to meet the defined goal. This is why you declare user and group, instead of invoking groupadd and useradd in order. You can do this because Puppet comes with support to manage such entities. This is vastly beneficial because Puppet also knows that, on different platforms, other commands are used for account management, and that the arguments can be subtly different on some systems.
Of course, Puppet does not have knowledge of all the conceivable particulars of any supported system. Say that you wish to manage an OpenAFS file server. There are no specific resource types to aid you with this. The ideal solution is to exploit Puppet's plugin system and to write your own types and providers so that your manifests can just reflect the AFS-specific configuration. This is not simple, though, and also not worthwhile in cases where you only need Puppet to invoke some exotic commands from very few places in your manifest.
For such cases, Puppet ships with the exec resource type, which allows the execution of custom commands in lieu of an abstract sync action.
For example, it can be used to unpack a tar ball in the absence of a proper package:
exec { 'tar cjf /opt/packages/homebrewn-3.2.tar.bz2':
cwd => '/opt',
path => '/bin:/usr/bin',
creates => '/opt/homebrewn-3.2',
}
The creates parameter is important for Puppet to tell whether the command needs running. Once the specified path exists, the resource counts as synchronized. For commands that do not create a telltale file or directory, there are the alternative parameters, onlyif and unless, to allow Puppet to query the sync state:
exec { 'perl -MCPAN -e "install YAML"':
path => '/bin:/usr/bin',
unless => 'cpan -l | grep -qP ^YAML\\b',
}
The query command's exit code determines the state. In the case of unless, the exec command runs if the query fails. This is how the exec type maintains idempotency. Puppet does this automatically for most resource types, but this is not possible for exec because synchronization is defined so arbitrarily. It becomes your responsibility as the user to define the appropriate queries per resource.
Finally, the exec type resources are the second notable case of receivers for events using notify and subscribe:
exec { 'apt-get update':
path => '/bin:/usr/bin',
subscribe => File['/etc/apt/sources.list.d/jenkins.list'],
refreshonly => true,
}
You can even chain multiple exec resources in this fashion so that each invocation triggers the next one. However, this is bad practice, and degrades Puppet to a (rather flawed) scripting engine. The exec resources should be avoided in favor of regular resources whenever possible. Some resource types that are not part of the core are available as plugins from the Puppet Forge. You will learn more about this topic in Chapter 5, Combining Classes, Configuration Files, and Extensions into Modules.
Since exec resources can be used to perform virtually any operation, they are sometimes abused to stand in for more proper resource types. This is a typical antipattern in Puppet manifests. It is safer to regard exec resources as the last resort or emergency exit that is only to be used if all other alternatives have been exhausted.
Ideally, your exec resource types are built as one-time only commands.
puppet describe <type> [-s]
In case you are unsure whether a type exists, you can tell puppet describe to return a full list of all available resource types:
puppet describe --list
Let's briefly discuss two more types that are supported out of the box. They allow the management of cron jobs, mounted partitions, and shares respectively, which are all frequent requirements in server operations.
- PHP動態網站程序設計
- MySQL數據庫管理實戰
- Cocos2D-X權威指南(第2版)
- 程序員數學:用Python學透線性代數和微積分
- 趣學Python算法100例
- 64位匯編語言的編程藝術
- Python機器學習編程與實戰
- AppInventor實踐教程:Android智能應用開發前傳
- Learning R for Geospatial Analysis
- OpenCV 4計算機視覺項目實戰(原書第2版)
- Python程序設計與算法基礎教程(第2版)(微課版)
- Spark技術內幕:深入解析Spark內核架構設計與實現原理
- Offer來了:Java面試核心知識點精講(框架篇)
- Learn Linux Quickly
- 前端架構設計