Puppet 简明教程
Puppet - Validating Puppet Setup
在 Puppet 中,可以本地测试设置。因此,一旦我们设置好 Puppet 主服务器和节点,就可以在本地验证设置了。我们需要在本地安装 Vagrant 和 Vagrant 框,它们有助于在本地测试设置。
In Puppet, the setup can be tested locally. Hence, once we have set up Puppet master and node, it’s time to validate the setup locally. We need to have Vagrant and Vagrant box installed locally, which helps in testing the setup locally.
Setting Up the Virtual Machine
由于我们正在本地测试设置,因此实际上不需要运行 Puppet 主服务器。这意味着无需在服务器上实际运行 Puppet 主服务器,我们可以简单地使用 Puppet 来应用命令以进行 Puppet 设置验证。Puppet apply 命令将根据配置文件中虚拟机的主机名从 local/etc/puppet 中应用更改。
As we are testing the setup locally, we do not actually require a running Puppet master. This means without actually running the Puppet master on the server, we can simply use Puppet to apply command for Puppet setup validation. Puppet apply command will apply changes from local/etc/puppet depending on the virtual machine’s hostname in the configuration file.
为了测试设置,我们需要执行的第一步是构建以下 Vagrantfile 并启动计算机并将 /etc/puppet 文件夹装入适当位置。所需的所有文件都将使用以下结构放置在版本控制系统中。
First step which we need to perform in order to test the setup is to build the following Vagrantfile and start a machine and mount the /etc/puppet folder into place. All the files which are required will be place inside the version control system with the following structure.
Directory Structure
- manifests
\- site.pp
- modules
\- your modules
- test
\- update-puppet.sh
\- Vagrantfile
- puppet.conf
Vagrant File
# -*- mode: ruby -*-
# vi: set ft = ruby :
Vagrant.configure("2") do |config|
config.vm.box = "precise32"
config.vm.box_url = "http://files.vagrantup.com/precise64.box"
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", 1028, "--cpus", 2]
end
# Mount our repo onto /etc/puppet
config.vm.synced_folder "../", "/etc/puppet"
# Run our Puppet shell script
config.vm.provision "shell" do |s|
s.path = "update-puppet.sh"
end
config.vm.hostname = "localdev.example.com"
end
在上面的代码中,我们使用了 Shell 配置工具,其中我们尝试运行名为 update-puppet.sh 的 Shell 脚本。此脚本存在于 Vagrant 文件所在的相同目录中,并且脚本的内容如下所示。
In the above code, we have used Shell provisioner in which we are trying to run a Shell script named update-puppet.sh. The script is present in the same directory where the Vagrant file is located and the content of the script are listed below.
!/bin/bash
echo "Puppet version is $(puppet --version)"
if [ $( puppet --version) != "3.4.1" ]; then
echo "Updating puppet"
apt-get install --yes lsb-release
DISTRIB_CODENAME = $(lsb_release --codename --short)
DEB = "puppetlabs-release-${DISTRIB_CODENAME}.deb"
DEB_PROVIDES="/etc/apt/sources.list.d/puppetlabs.list"
if [ ! -e $DEB_PROVIDES ]
then
wget -q http://apt.puppetlabs.com/$DEB
sudo dpkg -i $DEB
fi
sudo apt-get update
sudo apt-get install -o Dpkg::Options:: = "--force-confold"
--force-yes -y puppet
else
echo "Puppet is up to date!"
fi
在进一步处理中,用户需要在 Manifests 目录中创建一个清单文件,其名称为 site.pp ,它将安装一些虚拟机软件。
Further processing, the user needs to create a manifest file inside Manifests directory with the name site.pp which will install some software on VM.
node 'brclelocal03.brcl.com' {
package { ['vim','git'] :
ensure => latest
}
}
echo "Running puppet"
sudo puppet apply /etc/puppet/manifests/site.pp
一旦用户准备好上述脚本以及所需的 Vagrant 文件配置,用户就可以 cd 到测试目录并运行 vagrant up command 。这将启动一个新的虚拟机,稍后安装 Puppet,然后使用 Shell 脚本运行它。
Once the user has the above script ready with the required Vagrant file configuration, the user can cd to the test directory and run the vagrant up command. This will boot a new VM, Later, install Puppet and then run it using the Shell script.
输出将如下所示。
Following will be the output.
Notice: Compiled catalog for localdev.example.com in environment production in 0.09 seconds
Notice: /Stage[main]/Main/Node[brclelocal03.brcl.com]/Package[git]/ensure: created
Notice: /Stage[main]/Main/Node[brcllocal03.brcl.com]/Package[vim]/ensure: ensure changed 'purged' to 'latest'
Validating Multiple Machine Configuration
如果我们需要在本地测试多台计算机的配置,只需更改 Vagrant 配置文件即可轻松完成。
If we need to test the configuration of multiple machines locally, it can be simply done by making a change in Vagrant configuration file.
New Configured Vagrant File
config.vm.define "brclelocal003" do |brclelocal003|
brclelocal03.vm.hostname = "brclelocal003.brcl.com"
end
config.vm.define "production" do |production|
production.vm.hostname = "brcleprod004.brcl.com"
end
我们假设有一台新的生产服务器,需要安装 SSL 实用程序。我们只需要使用以下配置扩展旧清单。
Let’s assume we have a new production server, which needs SSL utility installed. We just need to extend the old manifest with the following configuration.
node 'brcleprod004.brcl.com' inherits 'brcleloacl003.brcl.com' {
package { ['SSL'] :
ensure => latest
}
}
在清单文件中进行配置更改后,我们只需要移动到测试目录并运行基本的 vagrant up 命令,该命令将同时启动 brclelocal003.brcl.com 和 brcleprod004.brcl.com 计算机。在我们的示例中,我们尝试启动生产计算机,可以通过运行 vagrant up production command 来完成。这样将创建一个名称为“production”的新计算机,如 Vagrant 文件中所定义,并且其中将安装 SSL 软件包。
After making configurational changes in the manifest file, we just need to move to the test directory and run the basic vagrant up command which will bring up both brclelocal003.brcl.com and brcleprod004.brcl.com machine. In our case, we are trying to bring up production machine which could be done by running the vagrant up production command. The will create a new machine with the name production as defined in Vagrant file and it will have SSL package installed in it.