How Do You Create a Chef Recipe to Install Java on Linux Chef-Client Nodes?

Problem scenario
You have Linux servers that need Java 1.8 installed on them.  How do you write a Chef recipe to deploy Java?

This solution requires that the Chef server must have access to GitHub.  If you want to install Chef server, see this posting.  If you want to install Chef client, see this posting.  You need to have Git installed on the Chef server.

1. Log into the Chef server. 

2. Go to the cookbooks directory (e.g., sudo find / -name knife.rb).  Find the "cookbook_path" stanza in the knife.rb file.  If you need to create this file (e.g., you recently configured knife but did not create the path listed in the cookbook_path stanza), manually create the directory path and the file.  Then cd into the directory that cookbook_path (in knife.rb) is set to.

3.  Run this command:
git clone java1.8

4.  a)  If you are not using the ChefDK on the Chef server, do this substep (a).  The command "chef -v" will show you if you are using Chef Development Kit.  From the Chef server if knife has been installed, or from a workstation with knife, run this command:

knife cookbook create java1.8

  b)  If you are using ChefDK (the command "chef -v" will show you if you are using Chef Development Kit), from the Chef server run this command:

chef generate cookbook java1.8

5.  Run this command from the Chef server if knife has been installed or from a workstation with knife:

knife cookbook upload java

6. a) knife node edit <nameOfNode>
    b)  Under the run list, add no commas if it the run list between the [] brackets is currently empty; otherwise put a comma after the last (or single) recipe you find in the run list, then add this stanza after that comma:

    c)  Save the changes.

7.  You are done creating the recipe.  You can now run the Chef client service.

If there is unreliability in the Chef client runs of installing Java, place a duplicate "recipe[java]" stanza with a comma after the top one after you use "knife node edit <nameOfNode>".  It should look like this:

Leave a comment

Your email address will not be published. Required fields are marked *