Sunday, July 26, 2015

Getting started with CHEF

CHEF is a configuration management tool which comes in handy when it comes to managing thousands of software components in production & development environments. It’s similar in functionality to puppet but use more procedural approach to do the job done. This example going to use CHEF solo

CHEF glossary

CHEF cookbook
        More like an artifact which contains all the things need to have to complete a scenario which include Recipes, data bags, templates and dependencies to other cookbook's

CHEF Server
        Which contains all the cookbook's and data bags which act as central management hub

CHEF Client
        Act as the agent of the CHEF server for each node

WorkStation
        Where user can communicate with CHEF server

Data Bag
        JSON variable which used to store data

Environment setup

In this example going to use plain Ubuntu server 14.04.2. And to install CHEF solo
        root@ubuntu2:~# sudo apt-get install chef

Configuring CHEF solo
Default location for the chef solo is located in /etc/chef/solo.rb
    CHEF solo.rb
        checksum_path "/var/chef/checksums"
        cookbook_path [
            "/var/chef/cookbooks",
            "/var/chef/site-cookbooks"
        ]
        data_bag_path repo "/var/chef/data_bags"
        environment_path "/var/chef/environments"
        file_backup_path "/var/chef/backup"
        file_cache_path "/var/chef/cache"
        role_path "/var/chef/roles"
        log_level :debug
        log_location "/var/chef/logs/chef.log"

Creating a cookbook
Go to site-cookbooks folder and create directory called example and file structure under it

        root@ubuntu2:/var/chef/site-cookbooks/example# tree
            .
            +-- recipes
            ¦   +-- default.rb
            +-- templates
                +-- default
                    +-- apache.conf.erb

Now need to enter execution instruction for our cookbook which is going to create configuration file using template and a data bag
        default.rb          
        item = data_bag_item("config", data_bag("config").last)
            template '/tmp/apache.conf' do
            source 'apache.conf.erb'
            owner 'root'
            group 'root'
            mode '644'
            variables(:config=> item)
        end

Then need to create template file for the recipe
        apache.conf.erb
        <VirtualHost <%= @config['ip']%>:<%= @config['port']%>>
            DocumentRoot /www/<%= @config['folder']%>
            ServerName <%= @config['hostName']%>
        </VirtualHost>

In the data bag directory create a file config and two json files under it
        root@ubuntu2:/var/chef/data_bags# tree
            .
            +-- config
                +-- config1.json
                +-- config2.json
      
        ex :
            {
              "id": "config1",
              "ip": "10.10.10.1",
              "port": "8881",
              "folder": "sample1",
              "hostName": "host1"
            }

Now we can run our cookbook typing chef-solo -o 'recipe[example]'

No comments:

Post a Comment