[13] DevOps 自动化运维工具Chef----用chef-apply命令单机运行Chef配方(Receipt)

前面的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'"

执行结果如下:


有意思吧!!!!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值