Reputation: 13
First off, I'm very new to coding and to Chef so please bear with me if I ask stupid questions.
Is there a way to have my execute block within a recipe only run if the user doesn't match. It's just a simple one line statement: chown -R aaa:aaa file/path. So if the owner of file/path is already aaa, don't run this.
If I wasn't clear on the question, I apologize and I'll try to clear it up.
Any help would be greatly appreciated. Thanks for your time.
Upvotes: 1
Views: 260
Reputation: 54181
So the other answer is technically correct, but the real way to do this is to use a file
resource:
file 'file/path' do
owner 'aaa'
group 'aaa'
end
That is already idempotent (a fancy word for "don't change things that are already fine") internally.
Upvotes: 4
Reputation: 24541
You can use not_if
, like this:
require 'etc'
execute "chown file/path" do
command "chown -R aaa:aaa file/path"
not_if { Etc.getpwuid(File.stat("file/path").uid).name == "aaa" }
end
If you want to also check the group, just add that to the condition.
Note that your approach is not really idempotent though, since the directory could be owned by aaa
but include files by others. It seems better to just always run the chown
, with no condition, so that it forces all the contents to aaa:aaa
. If the files are already aaa:aaa
, then nothing happens.
Upvotes: 0