Dir.glob 未在同一 chef-client 中显示新设备 运行

Dir.glob not showing new device in same chef-client run

我正在尝试安装一个设备,稍后在同一个 chef-client 运行 中,我需要 Dir.glob('/dev/xvd?') 所有设备,但是我刚安装的设备的文件没有一直存在到下一位厨师 运行。

为清楚起见,我的挂载确实正确发生并且我没有收到任何错误 - 我只是在下一个 chef-client 运行.[=25= 之前看不到来自 Dir.glob 的设备结果]

在这种情况下,mount_point = /datadevice_id = /dev/xvdf

mount "#{mount_point}" do                                                        
    device device_id                                                             
    fstype 'ext4'                                                                
    options 'noatime,nobootwait'                                                 
    action [:enable, :mount]                                                     
end      

如果我尝试获取所有设备并在使用 mount resource 后立即注销它们,/dev/xvdf 不会出现在列表中。

devices = Dir.glob('/dev/xvd?')                                                                                                                  
devices.each do |device|                                                         
    log "devices available: #{device}"                                         
end 

我的日志输出是这样的。挂载资源中的一行和我的日志输出中只有一行缺少新设备 /dev/xvdf 并显示现有设备 /dev/xvda

- mount /dev/xvdf to /data
* log[devices available: /dev/xvda] action write

** 更新 ** 正在尝试重新加载 ohai 以获取可用设备。

ohai "reload_filesystem" do                                                      
  action :nothing                                                              
end                                                                              

# now we can enable and mount it and we're done!                                 
mount "#{mount_point}" do                                                        
  device device_id                                                             
  fstype 'ext4'                                                                
  options 'noatime,nobootwait'                                                 
  action [:enable, :mount]                                                     
  notifies :reload, "ohai[reload_filesystem]", :immediately                    
end                                                                              

log "***** testing ohai reload 1 ****"                                           
devices = Dir.glob('/dev/xvd?')                                                  
devcount = devices.count                                                         
log "devices count: #{devcount}"                                                 
devices.each do |d|                                                              
 log "devices available: #{d}"                                                   
end                                                                              

ohai "reload2" do                                                                
    action :reload                                                               
end                                                                              

log "***** testing ohai reload 2 ****"                                           
devices = Dir.glob('/dev/xvd?')                                                  
devcount = devices.count                                                         
log "devices count: #{devcount}"                                                 
devices.each do |d|                                                              
 log "devices available: #{d}"                                                   
end 

** 输出 **

xxx.xx.x.xxx   * mount[/data] action mount                                       
xxx.xx.x.xxx     - mount /dev/xvdf to /data                                      
xxx.xx.x.xxx   * ohai[reload_filesystem] action reload/opt/chef/embedded/lib/ruby/gems/2.1.0/gems/ohai-8.5.0/lib/ohai/plugins/solaris2/network.rb:57: warning: already initialized constant ETHERNET_ENCAPS
xxx.xx.x.xxx /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/ohai-8.5.0/lib/ohai/plugins/solaris2/network.rb:57: warning: previous definition of ETHERNET_ENCAPS was here
xxx.xx.x.xxx                                                                     
xxx.xx.x.xxx     - re-run ohai and merge results into node attributes            
xxx.xx.x.xxx   * log[***** testing ohai reload 1 ****] action write              
xxx.xx.x.xxx                                                                     
xxx.xx.x.xxx   * log[devices count: 1] action write                              
xxx.xx.x.xxx                                                                     
xxx.xx.x.xxx   * log[devices available: /dev/xvda] action write                  
xxx.xx.x.xxx                                                                     
xxx.xx.x.xxx   * ohai[reload2] action reload/opt/chef/embedded/lib/ruby/gems/2.1.0/gems/ohai-8.5.0/lib/ohai/plugins/solaris2/network.rb:57: warning: already initialized constant ETHERNET_ENCAPS
xxx.xx.x.xxx /opt/chef/embedded/lib/ruby/gems/2.1.0/gems/ohai-8.5.0/lib/ohai/plugins/solaris2/network.rb:57: warning: previous definition of ETHERNET_ENCAPS was here
xxx.xx.x.xxx                                                                     
xxx.xx.x.xxx     - re-run ohai and merge results into node attributes            
xxx.xx.x.xxx   * log[***** testing ohai reload 2 ****] action write              
xxx.xx.x.xxx                                                                     
xxx.xx.x.xxx   * log[devices count: 1] action write                              
xxx.xx.x.xxx                                                                     
xxx.xx.x.xxx   * log[devices available: /dev/xvda] action write 

你被两个 pass 运行 See this 也命名为 compile vs converge。

Chef 按特定顺序执行操作,它首先编译食谱以构建资源集合(编译时),完成后它会执行每个资源的代码以确保其处于所需状态(收敛时)。

你的Dir.glob在配方中,当它被执行时(在编译时)挂载资源还没有运行并且没有挂载任何东西。

您可以将现有代码嵌入到 ruby_block 资源中,这样它将 运行 在收敛时和挂载之后,如果它在后面的配方代码中。

ruby_block 'List available devices' do 
  block do
    devices = Dir.glob('/dev/xvd?')                                                                                                                  
    devices.each do |device|                                                         
      Chef::Log.info("devices available: #{device}")                                        
    end
  end 
end

您不能直接使用 ruby_block 中的资源,这就是我使用 Chef::Log.info 的原因,但文档中有相关示例。


根据评论更新:

试试这个:

mount "/data" do 
  source "/dev/xvdf" # do whatever needs to be done there.
end

ohai "reload filsystem" do
  action :reload
  plugin "filesystem"
end

ruby_block "List filesystems" do
  block do
    node['filesystem'].each do |dev,properties|
      Chef::Log.warn("#{dev} is mounted on #{properties['mount']}")
    end
  end
end

这次我使用了警告级别,所以你会看到一些打印出来的东西,如果没有的话试试运行 chef-client -l info