简体   繁体   中英

how do I use the output of a shell command as a conditional value in a recipe

This is what I want to do:

  1. First check if a cert is installed on the server
  2. If not installed copy down the pfx to a temp location
  3. Install the pfx
  4. delete the pfx

How do I do this in a recipe?

Here is some semi-pseudo code:

unless "ls Cert:\LocalMachine\My\ | ?{$_.Subject -like '*#{cert_name}*'}"

  cookbook_file cert_temp_path do
    source cert_name
  end

  windows_certificate "c:/test/mycert.pfx" do
      pfx_password    'MyPass!'
  end

end

How do I execute that line of PS code? Can I call the powershell_script resource directly somehow?

This isn't how you would use Chef. Chef requires you rethink in terms of convergent behavior, rather than procedural steps. In this case, your end state would be:

  1. Cookbook file for the cert is copied to cache path.
  2. Cert is imported.

Trust that cookbook_file and windows_certificate are idempotent and convergent themselves, meaning they will take care of only acting when needed.

This is what I needed: Chef NOT_IF and ONLY_IF validation issue in Windows recipes

I needed the guard_interpreter setting.

Adding this to any resource calls that work with the pfx solve my issue:

guard_interpreter :powershell_script
not_if "My powershell code"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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