Skip to main content

Chef: Roles and Environments


  1. Bootstrap another node with your chef-server.
    If you get the following error:
    Failed to read the private key /etc/chef/validation.pem: # In the same error logs you will see a line:
    Delete your validation key in order to use your user credentials instead
    Please check that you don't have any validation.pem key in /etc/chef, if any, then delete it.
    You can verify by executing one of the following:
    knife client list
    knife node show chef-node2

  2. Create a new cookbook postgresql:
    chef generate cookbook cookbooks/postgresql

  3. Edit cookbooks/postgresql/recipes/default.rb
    [code]
    package 'postgresql-server' do
    notifies :run, 'execute[postgresql-init]'
    end

    execute 'postgresql-init' do
    command 'postgresql-setup initdb'
    action :nothing
    end

    service 'postgresql' do
    action [:enable, :start]
    end
    <span data-mce-type="bookmark" id="mce_SELREST_start" data-mce-style="overflow:hidden;line-height:0" style="overflow:hidden;line-height:0" ></span>
    [/code]

    The notifies :run, 'execute[postgresql-init]' will send a run signal to the execute task and execute the init command only when the package is installed.
    Check it with foodcritic and then push it to the git repo.

  4. role1.png

  5. role2.png

  6. role3.png

  7. role4.png

  8. role5role6role7

  9. role8

  10. Login to your chef-manage UI and select your node, click on "Edit run list" and remove all the recipes associated with it.

  11. In your chef-workstation we will now create a role. Execute the following command:
    knife role create web
    You will get the following error:
    ERROR: RuntimeError: Please set EDITOR environment variable
    We will have to set a default editor. Here we'll use vi.
    Execute the following commands:
    vi ~/.bashrc
    export EDITOR= $(which vi)
    source ~/.bashrc
    Then execute the create role command.
    This will openup the role file in vi editor with the following content:
    [code]
    {
    "name": "web",
    "description": "",
    "json_class": "Chef::Role",
    "default_attributes": {

    },
    "override_attributes": {

    },
    "chef_type": "role",
    "run_list": [

    ],
    "env_run_lists": {

    }
    }
    <span data-mce-type="bookmark" id="mce_SELREST_start" data-mce-style="overflow:hidden;line-height:0" style="overflow:hidden;line-height:0" ></span>
    [/code]

  12. Add the following in the run list section:
    "recipe[apache]",
    "recipe[apache::websites]",
    "recipe[apache::motd]"
    Save and close.

  13. You can check the run list for your role in your chef-manage UI in policies section in roles.

  14. You can also edit the existing role with command:
    knife role edit web

  15. You can add the role to the node with the following command:
    knife node run_list set chef-node2 "role[web]"

  16. You can check the newly added role with the following command:
    knife node show chef-node2

  17. Then we need to issue the chef-client command from the workstation for all the nodes having role "web".
    knife ssh "role:web" "sudo chef-client" -x gslab -P
    If you get the following error:
    WARNING: Failed to connect to chef-node2.local -- SocketError: getaddrinfo: Name or service not known.
    Then add the host chef-node2.local to /etc/hosts file. Then execute the knife ssh command again.

  18.  Then upload the postgresql cookbook:
    knife cookbook upload postgresql

  19. Create role database:
    knife create role database

  20. The add the role to a node:
    knife node run_list set chef-node "role[postgresql]"

  21. Then edit the postgresql default.rb recipe. Edit the line to match the following:
    notifies :run, 'execute[postgresql-init]', :immediately
    Then upload the cookbook: knife cookbook upload postgresql

  22. Then execute the following command:
    knife ssh "role:database" "sudo chef-client" -x gslab -P


Search



  1. Edit the websites.rb recipe in the apache cookbook.
    Add the following after the file section in it:
    [code]
    webnodes = search("node", "role:web")

    webnodes.each do |node|
    puts node
    end
    <span data-mce-type="bookmark" id="mce_SELREST_start" data-mce-style="overflow:hidden;line-height:0" style="overflow:hidden;line-height:0" ></span>
    [/code]

     

  2. Upload the cookbook:
    knife cookbook upload apache

  3. Then execute chef-client on chef-node2 you will see an output as below. In the output you can see the hostname added:
    Starting Chef Client, version 13.3.42
    resolving cookbooks for run list: ["apache", "apache::websites", "apache::motd"]
    Synchronizing Cookbooks:
    - apache (0.2.1)
    Installing Cookbook Gems:
    Compiling Cookbooks...
    node[chef-node2]
    Converging 4 resources
    Recipe: apache::default
    * yum_package[apache2] action install (up to date)
    * service[apache2] action start (up to date)
    * service[apache2] action enable (up to date)
    Recipe: apache::websites
    * file[default www] action create (up to date)
    Recipe: apache::motd
    * file[/etc/motd] action create (up to date)Running handlers:
    Running handlers complete
    Chef Client finished, 0/5 resources updated in 08 seconds


Environments



  1. env1
    env2env3

  2. Now edit the apache metadata.rb file and change the version to 1.0. Then edit the websites.rb, change the content to "Hello World v1.0". then upload the apache cookbook.

  3. Again edit the apache metadata.rb file and change the version to 2.0. Then edit the websites.rb, change the content to "Hello World v2.0". then upload the apache cookbook.

  4. Then login to the chef manage UI and then in the policy section -> Environment -> Create.
    Add the name as "staging" and some description. click on Next. Then in the constraints section select the name apache, Operator as "=",  Version as 2.0.0.
    Click on Add then Click on Create Environment.
    Add the name as "production" and some description. click on Next. Then in the constraints section select the name apache, Operator as "=",  Version as 1.0.0.
    Click on Add. Click on Create Environment.

  5. Then on the Nodes page click on chef-node Edit the environment to be Production. Save it.

  6. Then click on the chef-node2 and change the environment to be staging. Save it.

  7. Then execute chef-client on both nodes. You should see that the chef-node is executing the apache version 2.0.0 (Production) and chef-node2 is executing the apache version 1.0.0 (Staging).

  8. You can hit the IP address of both the servers in the browser and see that the webpage show different result.

  9. Thus we have separated the cookbooks and nodes as per environemt.

Comments

Popular posts from this blog

Terraform

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions. Configuration files describe to Terraform the components needed to run a single application or your entire datacenter. Terraform generates an execution plan describing what it will do to reach the desired state, and then executes it to build the described infrastructure. As the configuration changes, Terraform is able to determine what changed and create incremental execution plans which can be applied. The infrastructure Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc. The key features of Terraform are: Infrastructure as Code : Infrastructure is described using a high-level configuration syntax. This allows a blueprint of your datacenter to be versioned and

Java 8 coding challenge: Roy and Profile Picture

Problem:  Roy wants to change his profile picture on Facebook. Now Facebook has some restriction over the dimension of picture that we can upload. Minimum dimension of the picture can be  L x L , where  L  is the length of the side of square. Now Roy has  N  photos of various dimensions. Dimension of a photo is denoted as  W x H where  W  - width of the photo and  H  - Height of the photo When any photo is uploaded following events may occur: [1] If any of the width or height is less than L, user is prompted to upload another one. Print " UPLOAD ANOTHER " in this case. [2] If width and height, both are large enough and (a) if the photo is already square then it is accepted. Print " ACCEPTED " in this case. (b) else user is prompted to crop it. Print " CROP IT " in this case. (quotes are only for clarification) Given L, N, W and H as input, print appropriate text as output. Input: First line contains  L . Second line contains  N , number of

Salt stack issues

The function “state.apply” is running as PID Restart salt-minion with command:  service salt-minion restart No matching sls found for ‘init’ in env ‘base’ Add top.sls file in the directory where your main sls file is present. Create the file as follows: 1 2 3 base: 'web*' : - apache If the sls is present in a subdirectory elasticsearch/init.sls then write the top.sls as: 1 2 3 base: '*' : - elasticsearch.init How to execute saltstack-formulas create file  /srv/pillar/top.sls  with content: base : ' * ' : - salt create file  /srv/pillar/salt.sls  with content: salt : master : worker_threads : 2 fileserver_backend : - roots - git gitfs_remotes : - git://github.com/saltstack-formulas/epel-formula.git - git://github.com/saltstack-formulas/git-formula.git - git://github.com/saltstack-formulas/nano-formula.git - git://github.com/saltstack-f