前面的12篇文章唠唠叨叨了如何安装Chef,如何查看Chef的数据库的密码,就是没有分享如何跑一个简单的Chef的配方去完成一项任务。这篇文章的目的就是让大家见识一下如何用Chef执行一个简单的任务。安装好Chef客户端之后,就会有一个chef-apply的命令行工具。Chef-apply是在Chef-solo的工具的基础之上建立的一个小工具。而Chef-solo可以让我们在没有Chef服务器的情况下本地运行Chef代码。Chef-apply基于Chef-solo并被设计为可以很方便的应用.rb文件中的Chef代码。
Chef-apply的帮助命令如下:
C:\chef\helloworld>chef-apply -h
Usage: chef-apply [RECIPE_FILE | -e RECIPE_TEXT | -s] [OPTIONS]
--[no-]color Use colored output, defaults to enabled
-e, --execute RECIPE_TEXT Execute resources supplied in a string
--force-formatter Use formatter output instead of logger outp
ut
--force-logger Use logger output instead of formatter outp
ut
-F, --format FORMATTER output format to use
-j JSON_ATTRIBS, Load attributes from a JSON file or URL
--json-attributes
-l, --log_level LEVEL Set the log level (debug, info, warn, error
, fatal)
--minimal-ohai Only run the bare minimum ohai plugins chef
needs to function
--[no-]profile-ruby Dump complete Ruby call graph stack of enti
re Chef run (expert only)
-s, --stdin Execute resources read from STDIN
-v, --version Show chef version
-W, --why-run Enable whyrun mode
-h, --help Show this message
下面就给出一个简单的例子。在我的C:\chef\helloworld目录下面创建一个hello.rb的文件
file 'hello.txt' do
content 'welcome to chef'
puts "I like ruby and chef"
end
然后打开window的cmd窗口,切换到C:\chef\helloworld目录,然后运行
chef-apply hello.rb
其输出结果如下:
并在C:\chef\helloworld目录下生成了一个hello.txt的文件,简单吧。
此外,通过chef-apply -e 还能直接执行命令,比如在Linux下面直接执行下面的命令
chef-apply -e "package 'nano'"
执行结果如下:
有意思吧!!!!