rubychef-infra

Ruby "if file not found do" at the top of the script


I have a few rubys file that I am attempting to reduce duplicate code for. Below is a snippet of a much larger file that conatins a lot of repeditive code. Is it possible to add to the if node['platform_version'] =~ /10.0.14393/ section the not_if { ::File.exist?('C:/programdata/habitat/chef-base/files/missing_GPO_entries_from_patching.flg') } so that I don't have to have it individually listed under every single registry change? Something along the lines of if node = this and flg file not found, then

# If OS is 2016
if node['platform_version'] =~ /10.0.14393/
  registry_key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\DataCollection' do
    values [
      { name: 'AllowTelemetry', type: :dword, data: 0x00000000 },
    ]
    action :create_if_missing
    not_if { ::File.exist?('C:/programdata/habitat/chef-base/files/missing_GPO_entries_from_patching.flg') }
  end
end

Solution

  • Do you mean like this?

    if node['platform_version'] =~ /10.0.14393/ && !File.exist?('C:/programdata/habitat/chef-base/files/missing_GPO_entries_from_patching.flg')
      # ...
    end