多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
### 从 Shell 会话生成 Puppet 配置清单 > I object to being called a chess genius, because I consider myself to be an all around genius who just happens to play chess, which is rather different. > > — Bobby Fischer 并非所有人都是天才。如果你确切地知道安装一个应用程序或服务都需要做些什么的话, 你马上就可以创建 Puppet 的配置清单。尽管如此,你通常还是需要首先做些小小的试验, 比如找到你要安装的软件包、需要编辑哪些配置文件等等。 你可以使用 script 命令记录你的 Shell 会话,然后根据会话文件的记录内容开发 Puppet 的配置清单,这是个不错的方法。 但如果有一个工具能通过读取你的会话文件生成 Puppet 配置清单的话是不是更精彩呢? 为了实现此功能,**cft** (读音为 'sift')应运而生。 一旦你激活它,cft 监视你的 Shell 会话并记住你安装的任何软件包、任何服务的配置、 任何你创建或编辑的文件,等等。 当会话记录结束,cft 会生成一个重现你刚刚所做的所有改变的完整的 Puppet 配置清单。 #### 准备工作 1. 当前完整的 cft 支持仅能用于 Red Hat/CentOS 发行版;针对 Debian/Ubuntu 发行版的完整的支持正在开发中,估计不久之后即可完成。 如果你正在使用 Red Hat/CentOS,只要安装 cft 即可: ``` # yum install cft ``` 2. 对于 Debian/Ubuntu 系统,请参考如下安装说明,网址为: [http://fmtyewtk.blogspot.com/2011/01/porting-cft-to-debian.html](http://fmtyewtk.blogspot.com/2011/01/porting-cft-to-debian.html) 。 #### 操作步骤 1. 在本例中我们将使用 cft 监视 NTP 安装的软件包并生成实现相同功能的配置清单。 ``` # cft begin ntp # apt-get install ntp Reading package lists... Done Building dependency tree Reading state information... Done Suggested packages: ntp-doc The following NEW packages will be installed: ntp 0 upgraded, 1 newly installed, 0 to remove and 385 not upgraded. Need to get 517kB of archives. After this operation, 1,323kB of additional disk space will be used. Get:1 http://us.archive.ubuntu.com/ubuntu/ lucid/main ntp 1:4.2.4p8+dfsg-1ubuntu2 [517kB] Fetched 517kB in 5s (101kB/s) Selecting previously deselected package ntp. (Reading database ... 135278 files and directories currently installed.) Unpacking ntp (from .../ntp_1%3a4.2.4p8+dfsg-1ubuntu2_i386.deb) ... Processing triggers for man-db ... Processing triggers for ureadahead ... ureadahead will be reprofiled on next reboot Setting up ntp (1:4.2.4p8+dfsg-1ubuntu2) ... * Starting NTP server ntpd # vi /etc/ntp.conf # service ntp restart * Stopping NTP server ntpd [ OK ] * Starting NTP server ntpd # cft finish ntp # cft manifest ntp class ntp { package { 'ntp': ensure => '1:4.2.4p8+dfsg-1ubuntu2' } service { 'ntp': enable => 'true', ensure => 'running' } file { '/etc/ntp.conf': group => 'root', owner => 'root', mode => '0644', source => '/tmp/cft/ntp/after/etc/ntp.conf' } } ``` #### 工作原理 首先告诉 cft 开始记录系统的改变,并将其会话存储在 ntp 中?—?cft begin ntp 。 然后,当你安装 ntp 软件包时,cft 会记录这个事实。 软件包安装了服务的启动脚本,配置了在机器启动时启动服务,cft 同时也记录了这些。 最后,cft 注意到你编辑了 /etc/ntp.conf 文件,并保存了一份修改后的拷贝以备后用。 当你运行 cft finish ntp 命令,这会停止记录变化。 现在你可以使用 cft manifest ntp 命令生成与你的控制台会话等效的 Puppet 配置清单。 正如你看到的,生成的配置清单包括了 package 声明(由命令 apt-get install ntp 触发): ``` package { 'ntp': ensure => '1:4.2.4p8+dfsg-1ubuntu2' } ``` 同时包括了再现包安装脚本作用的 service 声明,启动服务并设置开机启动: ``` service { 'ntp': enable => 'true', ensure => 'running' } ``` 这个声明是由于你手动配置了如下命令所生成的: ``` # service ntp start # update-rc.d ntp defaults ``` 配置清单的最后一部分封装了 ntp.conf 文件的改变。 cft 只知道你对这个文件做了改变,但不知道你具体做了哪些改变, 所以 cft 将修改后的整个文件做为一个拷贝,并使其可以通过 Puppet 分发这个文件: ``` file { '/etc/ntp.conf': group => 'root', owner => 'root', mode => '0644', source => '/tmp/cft/ntp/after/etc/ntp.conf' } ``` 当你将此配置清单放入 Puppet,还需要从原始路径(/tmp/cft/ntp/after/etc/ntp.conf) 复制 ntp.conf 文件到你的模块树的适当位置,并根据这个位置修改 source 参数的值。 #### 更多用法 cft 是快速生成 Puppet 配置清单原型的一个强大工具。 你可以找一台构建配置清单的主机,尽可能使用 cft 记录你的安装和配置过程, 并使用它对整个会话进行编码生成 Puppet 的配置清单。 虽然这还需要一些额外的编辑工作,但是比你从头开始编写配置清单要快得多。