How to Automatically Run Chef-Client on Linux and Windows

A chef node can contact a Chef server by either scheduling a chef-client push from a remote server, or schedule a local pull from a Chef node. This quick tutorial illustrates how to create a simple Chef recipe to schedule a local pull from our Chef nodes

1. Prerequisites

  • Chef client bootstrapped to either a Windows or Linux machine
  • Hosted Chef or Chef server installed
  • ChefDK installed on a workstation configured with a Chef Server

2. Create a chef-client Cron Cookbook (Linux)

  • We will be using Berkshelf way of creating cookbooks
  • Execute berks cookbook chefcron in a folder in your home folder
[[email protected] chef]# berks cookbook chefcron
      create  chefcron/files/default
      create  chefcron/templates/default
      create  chefcron/attributes
      create  chefcron/libraries
      create  chefcron/providers
      create  chefcron/recipes
      create  chefcron/resources
      create  chefcron/recipes/default.rb
      create  chefcron/metadata.rb
      create  chefcron/LICENSE
      create  chefcron/README.md
      create  chefcron/CHANGELOG.md
      create  chefcron/Berksfile
      create  chefcron/Thorfile
      create  chefcron/chefignore
      create  chefcron/.gitignore
      create  chefcron/Gemfile
      create  .kitchen.yml
      append  Thorfile
      create  test/integration/default
      append  .gitignore
      append  .gitignore
      append  Gemfile
      append  Gemfile
You must run `bundle install' to fetch any new gems.
      create  chefcron/Vagrantfile
  • Go into the chefcron folder that was created and execute bundle install to install dependencies
[[email protected] chefcron]# bundle install
Fetching gem metadata from https://rubygems.org/.......
Fetching additional metadata from https://rubygems.org/..
Resolving dependencies...
Using addressable 2.3.6
Using multipart-post 2.0.0
Using faraday 0.9.0
Using berkshelf-api-client 1.2.0
Using buff-extensions 1.0.0
Using hashie 2.1.2
Using varia_model 0.4.0
Using buff-config 1.0.1
Using buff-ruby_engine 0.1.0
Using buff-shell_out 0.2.0
Using hitimes 1.2.2
Using timers 4.0.1
Using celluloid 0.16.0
Using nio4r 1.0.1
Using celluloid-io 0.16.1
Using cleanroom 1.0.0
Using minitar 0.5.4
Installing sawyer 0.6.0
Installing octokit 3.7.0
Using retryable 1.3.6
Using buff-ignore 1.1.1
Using erubis 2.7.0
Using json 1.8.1
Using mixlib-log 1.6.0
Using mixlib-authentication 1.3.0
Using net-http-persistent 2.9.4
Using semverse 1.2.1
Installing ridley 4.1.1
Using dep-selector-libgecode 1.0.2
Using ffi 1.9.6
Using dep_selector 1.0.3
Using solve 1.2.1
Using thor 0.19.1
Using berkshelf 3.2.1
Installing mixlib-shellout 1.6.1
Using net-ssh 2.9.1
Using net-scp 1.2.1
Using safe_yaml 1.0.4
Using test-kitchen 1.2.1
Using kitchen-vagrant 0.15.0
Using bundler 1.7.5
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
  • Create a chefcron file in the chefcron/files/default/chefcron with the following. This will run the chef-client on the node every 15 mins
*/15 * * * * root /usr/bin/chef-client
  • Modify the following default.rb file in chefcron/recipes/default.rb
#
# Cookbook Name:: chefcron
# Recipe:: default
#
# Copyright (C) 2014 YOUR_NAME
#
# All rights reserved - Do Not Redistribute
#
cookbook_file "/etc/cron.d/chefcron" do
  source "chefcron"
  mode "0600"
end
  • Run the following to upload your cookbook to the chef-server
berks upload --ssl-verify=false
Uploaded chefcron (0.1.0) to: 'https://chefserver:443/organizations/myorg'
  • In Chef Server under Nodes. Go to Edit Node Run List and add chefcron to the Current Run List
  • Go to the server that you’ve bootstrap and execute sudo chef-client or knife ssh ‘name:SERVER-NAME-IN-CHEF’ ‘sudo chef-client’ to execute the script you’ve just created

3. Install chef-client Service on Windows and start service (Windows)

  • Make sure the ChefDK workstation can resolve the WINDOWSHOST that you are trying to push commands to
  • On the ChefDK execute the following to install the chef-service-manager service for windows
[[email protected] ~]# knife winrm 'WINDOWSHOST' 'chef-service-manager -a install' -m -x 'Administrator' -P 'PASSWORD'
WINDOWSHOST Service 'chef-client' has successfully been installed.
WINDOWSHOST
  • Run the following in ChefDK to start up the service
[[email protected] ~]# knife winrm 'WINDOWSHOST' 'chef-service-manager -a install' -m -x 'Administrator' -P 'PASSWORD'
WINDOWSHOST One moment... start pending
WINDOWSHOST
WINDOWSHOST One moment... start pending
WINDOWSHOST
WINDOWSHOST Service 'chef-client' is now 'running'.
WINDOWSHOST
  •  The chef-service-manager also supports these commands to run via knife. If the service fails, try uninstalling and re-installing chef-service-manager and try again

-a, --action ACTION
Action to carry out on chef-service (install, uninstall, status, start, stop, pause, or resume)

  • Note: The default time the chef-client will run is 1800 seconds which is every 30 mins. This may be changed by adding an interval variable in c:\chef\client.rb file.

Leave a Comment