- Puppet 2.7 Cookbook
- John Arundel
- 590字
- 2021-04-02 18:19:56
Deploying changes with Rake
Like everyone who makes his living with a keyboard, I hate unnecessary typing. If you are using the workflow described in the section on using version control, you can add some automation to make this process a little easier. There are several tools that can run commands for you on remote servers, including Capistrano and Fabric, but for this example we'll use Rake.
Getting ready
If you don't have Rake installed already, run the following command:
apt-get install rake
You'll need a working Internet connection.
How to do it...
- Create a file in the top level of your Puppet working copy named
Rakefile
that looks like this:PUPPETMASTER = 'cookbook' SSH = 'ssh -t -A' task :deploy do sh "git push" sh "#{SSH} #{PUPPETMASTER} 'cd /etc/puppet && sudo git pull'" end
- When you make changes in your working copy of the Puppet manifests, you can simply run:
$ rake deploy
- Rake will take care of updating the Git repo and refreshing the Puppetmaster's working copy for you:
$ git push Counting objects: 4, done. Delta compression using 2 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 452 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To ssh:/ /git@cookbook.bitfieldconsulting.com/var/git/cookbook 561e5a6..a8b8c76 master -> master ssh -A -l root cookbook 'cd /etc/puppet && git pull' From ssh://cookbook.bitfieldconsulting.com/var/git/cookbook 561e5a6..a8b8c76 master -> origin/master Updating 561e5a6..a8b8c76 Fast-forward Rakefile | 6 ++++++ 1 files changed, 6 insertions(+), 0 deletions(-) create mode 100644 Rakefile
- You can also add a Rake task to run Puppet on the client machine:
task :apply => [:deploy] do client = ENV['CLIENT'] sh "#{SSH} #{client} 'sudo puppet agent --test'" do |ok, status| puts case status.exitstatus when 0 then "Client is up to date." when 1 then "Puppet couldn't compile the manifest." when 2 then "Puppet made changes." when 4 then "Puppet found errors." end end end
- When you want to test your changes on the client machine, run the following command:
rake CLIENT=cookbook apply
- Replace
cookbook
with the name of the client machine, or set theCLIENT
environment variable so that Rake knows which machine to run Puppet on.info: Caching catalog for cookbook info: Applying configuration version '1292865016' info: Creating state file /var/lib/puppet/state/state.yaml notice: Finished catalog run in 0.03 seconds
- If you want to see what changes Puppet would make, without actually changing anything, use the
--noop
flag:task :noop => [:deploy] do client = ENV['CLIENT'] sh "#{SSH} #{client} 'sudo puppet agent --test --noop'" end
- Now you can run:
$ rake noop
This will show you a preview of the changes.
How it works...
A Rakefile
consists of a series of tasks, identified by the task
keyword. The task definition is a set of steps, in this case the sequence of shell commands required to push your manifest changes to the master repo, and update the Puppetmaster's working copy.
Tasks can be linked, so that one depends on the other. For example, in our Rakefile
the apply
task is linked to deploy
, so that whenever you run rake apply
, Rake will make sure the deploy
task is done first, and the apply
task next.
There's more...
You can extend this Rakefile
to automate more tasks, including running a syntax check on the Puppet manifests before updating them, and even bootstrapping a new machine with Puppet. Rake is a powerful tool and can be a big help in managing a large network with Puppet.
See also
- Using version control in this chapter
- Creating a decentralized Puppet architecture in this chapter
- Using commit hooks in this chapter
- 做好PPT就靠這幾招:圖解力+吸引力+說服力
- Drupal 7 Module Development
- Alice 3 Cookbook
- CorelDRAW X5實用教程(第2版)
- Illustrator CC 2018中文版入門與提高
- Photoshop CS6中文版從入門到精通(核心技法卷):摳圖、修圖、Camera Raw、調色、銳化、合成
- 二維計算機繪圖教程:二維CAD工程師取證全程指導
- After Effects影視特效立體化教程:After Effects 2021(微課版)
- SolidWorks快速入門教程(2022中文版)
- 有趣的Flutter:從0到1構建跨平臺App
- OpenCV項目開發實戰(原書第2版)
- 正則表達式必知必會(修訂版)
- Spark Cookbook 中文版
- Origin 2022科學繪圖與數據分析
- 中文版CorelDRAW X7技術大全