How to capture Chef::Util::FileEdit.new operation in why-run mode

392 views Asked by At

I want to have a custom resource to edit a file like "crontab" resource does.

Below is my custom resource which does the job but in 'why-run' mode it doesn't show what string it would add or remove.

resource_name :super_tab
provides :super_tab

property :command, String, name_property: true
property :path, String, required: true
property :allowed_user, String, required: true
property :uid, String,  default: 'root'

action :add do
    ruby_block "edit super.tab" do
        block do
            supertab = Chef::Util::FileEdit.new("/etc/super.tab")
            supertab.search_file_delete_line(/^#{command} /)
            supertab.insert_line_if_no_match(/^#{command} /, "#{command} #{path} #{allowed_user} uid=#{uid}")
            supertab.write_file
        end
    end
end
1

There are 1 answers

1
coderanger On

1) Do not use FileEdit. It is an internal API within Chef and is not considered suitable for public use.

2) The specific issue is that ruby_block resources are not run during whyrun as we have no way to prevent them from having side effects.