How do i create my own chef cookbook?



  • chef man page: http://bit.ly/2oPbYOn
    knife node man page: http://bit.ly/2pCe5sO
    knife cookbook man page: http://bit.ly/2q8P6wS

    Start by creating a structure for a cookbook.

    $ chef generate cookbook cowsay
    Generating cookbook cowsay
    - Ensuring correct cookbook file content
    - Ensuring delivery configuration
    - Ensuring correct delivery build cookbook content
    
    Your cookbook is ready. Type `cd cowsay` to enter it.
    
    There are several commands you can run to get started locally developing and testing your cookbook.
    Type `delivery local --help` to see a full list.
    
    Why not start by writing a test? Tests for the default recipe are stored at:
    
    test/smoke/default/default_test.rb
    
    If you'd prefer to dive right in, the default recipe can be found at:
    
    recipes/default.rb
    

    This creates the following structure

    $ tree cowsay
    cowsay
    ├── Berksfile
    ├── chefignore
    ├── metadata.rb
    ├── README.md
    ├── recipes
    │   └── default.rb
    ├── spec
    │   ├── spec_helper.rb
    │   └── unit
    │       └── recipes
    │           └── default_spec.rb
    └── test
        └── smoke
            └── default
                └── default_test.rb
    
    7 directories, 8 files
    

    This is for demonstration purposes and to show the steps, we are only going to use recipes/default.rb. This is one of the files where you define what you want your cookbook to do. You can see the other files and directories from the tree structure, each has a function within the cookbook. See the descriptions in this link: https://docs.chef.io/cookbooks.html

    Edit the file recipes/default.rb add the following and save. This very basic action to install one package called cowsay, that's it.

    package "cowsay" do
       action :install
    end
    

    Instruct the node on what to install. Our node is called sector12, edit the node, where it says "run_list": under that line add "recipe[cowsay]", and save.

    $ sudo knife node edit sector12
    
    {
      "name": "sector12",
      "chef_environment": "_default",
      "normal": {
        "tags": [
    
        ]
      },
      "policy_name": null,
      "policy_group": null,
      "run_list": [
          "recipe[cowsay]"
    ]
    
    }
    

    Upload the cookbook to the chef server.

    $ knife cookbook upload  cowsay
    Uploading cowsay         [0.1.0]
    Uploaded all cookbook.
    

    Run chef-client on sector12 (client/node).

    sector12> $ sudo chef-client
    Starting Chef Client, version 13.0.118
    resolving cookbooks for run list: ["cowsay"]
    Synchronizing Cookbooks:
      - cowsay (0.1.0)
    Installing Cookbook Gems:
    Compiling Cookbooks...
    Converging 1 resources
    Recipe: cowsay::default
      * apt_package[cowsay] action install
        - install version 3.03+dfsg1-16 of package cowsay
    
    Running handlers:
    Running handlers complete
    Chef Client finished, 1/1 resources updated in 04 seconds
    
    sector12> $ cowsay -f tux "Hello World!"
     ______________
    < Hello World! >
     --------------
       \
        \
            .--.
           |o_o |
           |:_/ |
          //   \ \
         (|     | )
        /'\_   _/`\
        \___)=(___/
    

Log in to reply
 

© Lightnetics 2024