Skip to content

Instantly share code, notes, and snippets.

@Arpanbhagat5
Forked from maxivak/readme.md
Created May 25, 2018 02:44
Show Gist options
  • Save Arpanbhagat5/d16fb2b1627565efc8c86d84b57586a1 to your computer and use it in GitHub Desktop.
Save Arpanbhagat5/d16fb2b1627565efc8c86d84b57586a1 to your computer and use it in GitHub Desktop.
Chef. How to run scripts (recipes)

Run Chef scripts locally

There are several options to run recipes:

  • using chef-client with -z option
  • using chef-apply
  • using chef-solo

Before running Chef recipes on the machine, it should be prepared:

  • install necessary software (ChefDK, etc)

Run recipes locally with chef-client -z

  • run script
chef-client -z whatever.rb
  • whatever.rb
file '/tmp/x.txt' do
  content 'hello world'
end

chef-client -z -j my.json

run with json config

chef-client -z -j my.json
  • specify recipes to run in json file

  • my.json

{
    "run_list": [ "recipe[base::default]" ]
}

run recipes from cookbook (--override-runlist)

  • create cookbook 'base'

  • put attributes for the node in json file

  • config.json

{
    "attr1": "v1",
    "attr2": "v2"
}
  • specify recipe using --override-runlist option
chef-client -z -j config.json --override-runlist "recipe[base::default]"

chef-apply

run:

chef-apply hello.rb
  • hello.rb
file 'hello.txt' do
  content 'Welcome to Chef'
end

chef-solo

  • solo.rb
file_cache_path "/root/chef-solo"
cookbook_path "/root/chef-repo/cookbooks"
  • attr.json
{
  "run_list": [ "recipe[apt]", "recipe[phpapp]" ],
  "nginx": {"domainname": "mysite.com", "port": 80}
}

run

root@intro:~/chef-repo# 
chef-solo -c solo.rb -j attr.json

or specify recipe in command line:

chef-solo -c solo.rb -j attr.json -o apt

?? chef-solo -c solo.rb -j attr.json -o "recipe[apt]", "recipe[phpapp]"

== attributes in json ==

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