如何将Linux命令输出作为Chef属性获取

16
我想把一个命令的输出作为Chef属性,放到execute资源或bash资源中。有人可以帮助我如何设置吗?
ruby_block "something" do
    block do
        #tricky way to load this Chef::Mixin::ShellOut utilities
        Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)      
        command = 'cat #{fileName}'
        command_out = shell_out(command)
        node.set['my_attribute'] = command_out.stdout
    end
    action :create
end
如何在上述代码中使用属性...

1
请添加一些代码,以便我们看到您面临的问题。 - dax
似乎这是一个 https://serverfault.com/ 上的问题,但仍然不清楚你想要什么。 - Fionn
2
实际问题似乎是 command = 'cat #{fileName}',@SASI 应该使用双引号来启用解析(即 command = "cat #{fileName}")。 - scones
1个回答

24

你的问题的答案在如何将Chef 'execute resource'的输出放入变量中中已经很清楚地给出了。如果我理解问题正确,通过微小的修改,可以像这样解决你的问题:

ruby_block "something" do
    block do
        #tricky way to load this Chef::Mixin::ShellOut utilities
        Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)      
        command = 'cat /etc/hostname'
        command_out = shell_out(command)
        node.set['my_attribute'] = command_out.stdout
    end
    action :create
end

command 的内容替换为您想要运行的命令,将 my_attribute 替换为您想要设置的属性。


我认为这至少回答了你的问题。如果你描述你的问题,我们可能会找到一种更优雅的解决方案。 - StephenKing
谢谢StephenKing..我的命令是"sudo su - grid +ASM asmcmd lsdsk -G data"我必须以grid用户身份登录并进入+ASM进程,然后运行该命令。您能帮我解决如何在recipe中运行它的问题吗? - SASI
1
@user3886612 请编辑您的问题描述用例,这样我们就可以投票重新打开它。 - Tensibai
1
@SASI 在评论中重复自己是没有帮助的。在你的问题下面有一个编辑按钮可以更新它。 - Tensibai
1
看到你后续如何使用这些信息也会很有趣。 - StephenKing
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接