简体   繁体   English

如何将文件内容分配给厨师节点属性

[英]How to assign file content to chef node attribute

I have fingreprint.txt at the location "#{node['abc.d']}/fingreprint.txt"我在"#{node['abc.d']}/fingreprint.txt"位置有fingreprint.txt

The contents of the file are as below:文件内容如下:
time="2015-03-25T17:53:12C" level=info msg="SHA1 Fingerprint=7F:D0:19:C5:80:42:66"

Now I want to retrieve the value of fingerprint and assign it to chef attribute现在我想检索指纹的值并将其分配给厨师属性
I am using the following ruby block我正在使用以下 ruby​​ 块

ruby_block "retrieve_fingerprint" do  
    block do  
        path="#{node['abc.d']}/fingreprint.txt"  
        Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)  
        command = 'grep -Po '(?<=Fingerprint=)[^"]*' path '  
        command_out = shell_out(command)  
        node.default['fingerprint'] = command_out.stdout  
    end  
    action :create  
end  

It seems not to be working because of missing escape chars in command = 'grep -Po '(?<=Fingerprint=)[^"]*' path ' . Please let me know if there is some other way of assigning file content to node attribute由于在command = 'grep -Po '(?<=Fingerprint=)[^"]*' path '中缺少转义字符,它似乎不起作用。请告诉我是否有其他方法可以将文件内容分配给节点属性

Two ways to answer this: first I would do the read ( IO.read ) and parsing ( RegExp.new and friends) in Ruby rather than shelling out to grep .回答这个问题的两种方法:首先,我会在 Ruby 中进行读取( IO.read )和解析( RegExp.new和朋友),而不是对grep炮击。

if IO.read("#{node['abc.d']}/fingreprint.txt") =~ /Fingerprint=([^"]+)/
  node.default['fingerprint'] = $1
end

Second, don't do this at all because it probably won't behave how you expect.其次,根本不要这样做,因为它可能不会像您期望的那样表现。 You would have to take in to account both the two-pass loading process and the fact that default attributes are reset on every run.您必须同时考虑两次加载过程以及每次运行时都会重置默认属性的事实。 If you're trying to make an Ohai plugin, do that instead.如果您正在尝试制作 Ohai 插件,请改为这样做。 If you're trying to use this data in later resources, you'll probably want to store it in a global variable and make copious use of the lazy {} helper.如果您尝试在以后的资源中使用此数据,您可能希望将其存储在全局变量中并大量使用lazy {}帮助程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM