Skip to content

Instantly share code, notes, and snippets.

@ravibhure
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravibhure/189c07843afc15b9ca75 to your computer and use it in GitHub Desktop.
Save ravibhure/189c07843afc15b9ca75 to your computer and use it in GitHub Desktop.
Just an example
  1. Create new cookbook example

     knife cookbook create example
    
  2. Create file name foo.py, Open Editor for examples/files/default/foo.py

     #!/usr/bin/python
     # Author: Sanjay Lokhande <sanjay.lokhande@opexsoftware.com>
     import os
     
     print "I am %s, running a python script" % os.getlogin()
    
  3. Open file example/recipes/default.rb

     #
     # Cookbook Name:: example
     # Recipe:: default
     #
     # Copyright 2014, OpexSoftware
     #
     # All rights reserved - Do Not Redistribute
     #
     #
     filename = "foo.py"
     cmdline = "import os; print "I am %s, running on cmd " % os.getlogin()"
     
     # Copy foo.py
     cookbook_file "/tmp/foo.py" do
       source "foo.py"
       mode "0755"
     end
     
     # Execute sample python one liner
     # cmd: python -c "import os; print "I am %s, running on cmd " % os.getlogin()"
     execute 'who_am_i' do
      cwd '/tmp'
      command "python -c #{cmdline}"
     end
     
     # Execute python script
     execute 'run_my_python_script' do
      cwd '/tmp'
      command "python #{filename}"
     end
    
  4. Now upload it to chef server

     knife cookbook upload example
    
  5. Add run list to node1

     knife node run_list add node1 "recipe[example]"
    
  6. Run chef-client

  7. Open file example/recipes/default.rb again and update with following...

     python "#{filename}" do
       cwd "/tmp" # can be list from https://docs.getchef.com/resource_python.html#Attributes
       action :run # see actions section below
     end
    
  8. upload your cookbook again

     knife cookbook upload example
    
  9. Run chef-client again

  10. Every good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment