Reader small image

You're reading from  Chef Cookbook - Third Edition

Product typeBook
Published inFeb 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781786465351
Edition3rd Edition
Languages
Tools
Right arrow
Author (1)
Matthias Marschall
Matthias Marschall
author image
Matthias Marschall

Matthias Marschall is a Software Engineer "made in Germany". His four children make sure that he feels comfortable in lively environments, and stays in control of chaotic situations. A lean and agile engineering lead, he's passionate about continuous delivery, infrastructure automation, and all things DevOps. In recent years, Matthias has helped build several web-based businesses, first with Java and then with Ruby on Rails. He quickly grew into system administration, writing his own configuration management tool before migrating his whole infrastructure to Chef in its early days. In 2008, he started a blog (http://www.agileweboperations.com) together with Dan Ackerson. There, they have shared their ideas about DevOps since the early days of the continually emerging movement. You can find him on Twitter as @mmarschall. Matthias holds a Master's degree in Computer Science (Dipl.-Inf. (FH)) and teaches courses on Agile Software Development at the University of Augsburg. When not writing or coding, Matthias enjoys drawing cartoons and playing Go. He lives near Munich, Germany.
Read more about Matthias Marschall

Right arrow

Using libraries


You can use arbitrary Ruby code within your recipes. If your logic isn't too complicated, it's totally fine to keep it inside your recipe. However, as soon as you start using plain Ruby more than Chef DSL, it's time to the move the logic into external libraries.

Libraries provide a place to encapsulate Ruby code so that your recipes stay clean and neat. In this section, we'll create a simple library to see how this works.

Getting ready

Make sure you have a cookbook called my_cookbook and that the run_list of your node includes my_cookbook, as described in the Creating and using cookbooks recipe of Chapter 1, Chef Infrastructure.

How to do it...

Let's create a library and use it in a cookbook:

  1. Create a helper method in your own cookbook's library:

    mma@laptop:~/chef-repo $ mkdir -p cookbooks/my_cookbook/libraries
    mma@laptop:~/chef-repo $ subl cookbooks/my_cookbook/libraries/ipaddress.rb
    class Chef::Recipe
      def netmask(ipaddress)
        IPAddress(ipaddress).netmask
      end
    end
    
  2. Use your...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Chef Cookbook - Third Edition
Published in: Feb 2017Publisher: PacktISBN-13: 9781786465351

Author (1)

author image
Matthias Marschall

Matthias Marschall is a Software Engineer "made in Germany". His four children make sure that he feels comfortable in lively environments, and stays in control of chaotic situations. A lean and agile engineering lead, he's passionate about continuous delivery, infrastructure automation, and all things DevOps. In recent years, Matthias has helped build several web-based businesses, first with Java and then with Ruby on Rails. He quickly grew into system administration, writing his own configuration management tool before migrating his whole infrastructure to Chef in its early days. In 2008, he started a blog (http://www.agileweboperations.com) together with Dan Ackerson. There, they have shared their ideas about DevOps since the early days of the continually emerging movement. You can find him on Twitter as @mmarschall. Matthias holds a Master's degree in Computer Science (Dipl.-Inf. (FH)) and teaches courses on Agile Software Development at the University of Augsburg. When not writing or coding, Matthias enjoys drawing cartoons and playing Go. He lives near Munich, Germany.
Read more about Matthias Marschall