Hi,
I am modifying my first cookbook and have difficulties understanding how
processing of commands work in Chef. Consider an extract from the cookbook:
search(:users, ‘groups:sysadmin’) do |u|
execute “delete password” do
command "passwd -d #{u[‘id’]}"
only_if "test $(passwd -S #{u[‘id’]} | awk ‘{print $2}’) = L"
end
end
There are two users in sysadmin data bag: motiejus and gavin. IMHO loop should
be iterated twice. However, from chef-client logs I see this:
[Wed, 11 Apr 2012 20:58:24 +0100] INFO: Processing user[motiejus] action create (users::sysadmins line 42)
[Wed, 11 Apr 2012 20:58:24 +0100] DEBUG: Processing execute[delete password] on chef.src.gla.ac.uk
[Wed, 11 Apr 2012 20:58:24 +0100] INFO: Processing execute[delete password] action run (users::sysadmins line 52)
[Wed, 11 Apr 2012 20:58:24 +0100] DEBUG: sh(test $(passwd -S motiejus | awk '{print 2}') = L)
[Wed, 11 Apr 2012 20:58:24 +0100] DEBUG: Skipping execute[delete password] due to only_if command `test (passwd -S motiejus | awk '{print 2}') = L`
...
[Wed, 11 Apr 2012 20:59:26 +0100] DEBUG: Processing user[gavin] on chef.src.gla.ac.uk
[Wed, 11 Apr 2012 20:59:26 +0100] INFO: Processing user[gavin] action create (users::sysadmins line 42)
...
[Wed, 11 Apr 2012 20:59:26 +0100] DEBUG: Processing execute[delete password] on chef.src.gla.ac.uk
[Wed, 11 Apr 2012 20:59:26 +0100] INFO: Processing execute[delete password] action run (users::sysadmins line 52)
[Wed, 11 Apr 2012 20:59:26 +0100] DEBUG: sh(test (passwd -S motiejus | awk '{print 2}') = L)
[Wed, 11 Apr 2012 20:59:26 +0100] DEBUG: Skipping execute[delete password] due to only_if command `test (passwd -S motiejus | awk ‘{print $2}’) = L`
Notice the command
test $(passwd -S motiejus | awk '{print $2}') = L
is executed twice. I expect it to be executed once “motiejus”, second time
"gavin". Any ideas why?
Another strange thing. If I replace
only_if "test $(passwd -S #{u[‘id’]} | awk ‘{print $2}’) = L"
to
only_if "true " + u[‘id’]
I see this in the logs:
[Wed, 11 Apr 2012 21:03:40 +0100] INFO: Processing execute[delete password] action run (users::sysadmins line 52)
[Wed, 11 Apr 2012 21:03:40 +0100] DEBUG: sh(true motiejus)
[Wed, 11 Apr 2012 21:03:40 +0100] DEBUG: sh(true gavin)
[Wed, 11 Apr 2012 21:03:41 +0100] INFO: execute[delete password] sh(passwd -d motiejus)
passwd: password expiry information changed.
[Wed, 11 Apr 2012 21:03:41 +0100] INFO: execute[delete password] ran successfully
…
[Wed, 11 Apr 2012 21:03:41 +0100] DEBUG: sh(true motiejus)
[Wed, 11 Apr 2012 21:03:41 +0100] DEBUG: sh(true gavin)
[Wed, 11 Apr 2012 21:03:41 +0100] INFO: execute[delete password] sh(passwd -d gavin)
passwd: password expiry information changed.
[Wed, 11 Apr 2012 21:03:41 +0100] INFO: execute[delete password] ran successfully
Why is sh(true ) executed twice? I expect it to run once for motiejus,
and once for gavin.
Do I utterly misunderstand how cookbook execution works? I would like to know
the details of compilation and execution, is there something more higher level
than source code for me to look at (I don’t have much experience with ruby, so
not that familiar with ruby metaprogramming)?
Chef 0.10.8 from opsocde apt repos on squeeze.
Thanks
Motiejus