mercredi 21 juillet 2021

Chef ruby code - .rb cookbook - If statement to check config['variable'] exists

I have the following bit of code in my cookbook:

if File.file?('/etc/test.yml')
  config = YAML.load(File.read('/etc/test.yml'))
 else
  config = Hash.new
 end
 config['license_key'] = node['test']['license']
 config['verbose'] = 0
 config['metrics_process_sample_rate'] = 60
 file '/etc/test.yml' do
  content config.to_yaml
 end

My code works in that the .yml file will be created if it doesn't already exist and will populate the file with three variables when creating the .yml file. But what I'm attempting to add to this bit of code is to confirm if the .yml file already exists that these three variables also exist in the .yml file. I'll need to add a check to each config['variable'] = line confirm if this variable exists and if the variable is missing to add the variable to the .yml file.

Any advice on the code required to achieve this would be greatly appreciated.

Thank you.

Aucun commentaire:

Enregistrer un commentaire