chef [SOLVED]: What is the chef appropriate way to ensure idempotency when an execute block's "creates" value is not fully known?

chef [SOLVED]: What is the chef appropriate way to ensure idempotency when an execute block's "creates" value is not fully known?

Home Forums Automation Tools Chef Tutorials chef [SOLVED]: What is the chef appropriate way to ensure idempotency when an execute block's "creates" value is not fully known?

Tagged: 

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #193376

    Cloudy Point
    Keymaster

    QuestionQuestion

    Within the following code’s creates directive, the specified value contains a (actually, more than one) wildcard.

    execute "Install Vagrant plugin: #{plugin}" do
       environment ({ 'HOME' => ::Dir.home(user), 'USER' => user })
       command "vagrant plugin install #{plugin}"
       creates "/home/#{user}/.vagrant.d/gems/*/gems/vagrant-berkshelf-*"
    end
    

    I expect that there is not a chef intended method of handling a situation where only a directory pattern is known beforehand and not the actual full path. The Chef documentation for execute does not explicitly say whether or not creates will pattern match, but after testing, I believe it does not and is not intended to pattern match.

    This being the case, would the above be an appropriate use for Chef::Mixin::ShellOut? Or would there be another more chef intended method?

    If the former, I’m anticipating using examples from stackoverflow (1, 2) to determine whether a directory exists matching a pattern resembling the value of the creates directive above and using that return value to set whether or not to run the execute block in question.

    Any tips, hints, or suggestions are greatly appreciated!

    • Informatician

    #193377

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    You would use a not_if or only_if guard clause. The creates property is just a helper for not_if { ::File.exist?(the_path) }. In this case what you probably want is either not_if "vagrant plugin list | grep #{plugin}" or the slightly cleaner (IMO) not_if { shell_out!('vagrant plugin list').stdout.include?(plugin) }.

    Source: https://stackoverflow.com/questions/46313114/what-is-the-chef-appropriate-way-to-ensure-idempotency-when-an-execute-blocks
    Author: coderanger
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.