Hi
I have this definition on my cookbook
group “sensu” do
action :modify
members “vagrant”
append true
end
it will throw an exception when I run it on ec2 because there is no user
vagrant
How can I avoid the exception? Is there any way to detect the user exist? I
have checked the doc https://docs.chef.io/resource_group.html but there is
nothing obvious
Cheers,
Hey Anthony,
You can use a guard in this case to check and execute the resource
'only_if' the condition is true. You can use ruby or specify a command.
$ cat test.rb
group "sensu" do
action :modify
members "vagrant"
append true
only_if "getent passwd vagrant"
end
$ chef-apply test.rb
Recipe: (chef-apply cookbook)::(chef-apply recipe)
* group[sensu] action modify (*skipped due to only_if*)
More info on Guards - Common Resource Functionality
Grabbed the guard form this post -
linux, chef-infra
HTH
-Eric Helgeson
@nulleric https://twitter.com/nulleric
http://usingchef.com
On Sat, Feb 28, 2015 at 4:42 PM, Anthony Kong anthony.hw.kong@gmail.com
wrote:
Hi
I have this definition on my cookbook
group "sensu" do
action :modify
members "vagrant"
append true
end
it will throw an exception when I run it on ec2 because there is no user
vagrant
How can I avoid the exception? Is there any way to detect the user exist?
I have checked the doc group Resource but there
is nothing obvious
Cheers,
Thanks! It fixed my problem!
On Sun, Mar 1, 2015 at 9:56 AM, Eric Helgeson erichelgeson@gmail.com
wrote:
Hey Anthony,
You can use a guard in this case to check and execute the resource
'only_if' the condition is true. You can use ruby or specify a command.
$ cat test.rb
group "sensu" do
action :modify
members "vagrant"
append true
only_if "getent passwd vagrant"
end
$ chef-apply test.rb
Recipe: (chef-apply cookbook)::(chef-apply recipe)
* group[sensu] action modify (*skipped due to only_if*)
More info on Guards - Common Resource Functionality
Grabbed the guard form this post -
Chef: Initialize new linux user if it doesn't already exist - Stack Overflow
HTH
-Eric Helgeson
@nulleric https://twitter.com/nulleric
http://usingchef.com
On Sat, Feb 28, 2015 at 4:42 PM, Anthony Kong anthony.hw.kong@gmail.com
wrote:
Hi
I have this definition on my cookbook
group "sensu" do
action :modify
members "vagrant"
append true
end
it will throw an exception when I run it on ec2 because there is no user
vagrant
How can I avoid the exception? Is there any way to detect the user exist?
I have checked the doc group Resource but
there is nothing obvious
Cheers,
For a more ruby-esque way you can use the built-in method Etc.
On Feb 28, 2015 5:56 PM, "Eric Helgeson" erichelgeson@gmail.com wrote:
Hey Anthony,
You can use a guard in this case to check and execute the resource
'only_if' the condition is true. You can use ruby or specify a command.
$ cat test.rb
group "sensu" do
action :modify
members "vagrant"
append true
only_if "getent passwd vagrant"
end
$ chef-apply test.rb
Recipe: (chef-apply cookbook)::(chef-apply recipe)
* group[sensu] action modify (*skipped due to only_if*)
More info on Guards - Common Resource Functionality
Grabbed the guard form this post -
Chef: Initialize new linux user if it doesn't already exist - Stack Overflow
HTH
-Eric Helgeson
@nulleric https://twitter.com/nulleric
http://usingchef.com
On Sat, Feb 28, 2015 at 4:42 PM, Anthony Kong anthony.hw.kong@gmail.com
wrote:
Hi
I have this definition on my cookbook
group "sensu" do
action :modify
members "vagrant"
append true
end
it will throw an exception when I run it on ec2 because there is no user
vagrant
How can I avoid the exception? Is there any way to detect the user exist?
I have checked the doc group Resource but
there is nothing obvious
Cheers,
Hi Mark,
Can you shed more light on it? Do you have a url to the documentation or
some example?
Cheers,
Tony Kong
blog: www.ahwkong.com
linkedin: www.linkedin.com/in/anthonykong
Don’t EVER make the mistake that you can design something better than what
you get from ruthless massively parallel trial-and-error with a feedback
cycle. That’s giving your intelligence much too much credit.
On Mon, Mar 2, 2015 at 4:12 AM, Mark Pimentel markpimentel22@gmail.com
wrote:
For a more ruby-esque way you can use the built-in method Etc.
On Feb 28, 2015 5:56 PM, "Eric Helgeson" erichelgeson@gmail.com wrote:
Hey Anthony,
You can use a guard in this case to check and execute the resource
'only_if' the condition is true. You can use ruby or specify a command.
$ cat test.rb
group "sensu" do
action :modify
members "vagrant"
append true
only_if "getent passwd vagrant"
end
$ chef-apply test.rb
Recipe: (chef-apply cookbook)::(chef-apply recipe)
* group[sensu] action modify (*skipped due to only_if*)
More info on Guards - Common Resource Functionality
Grabbed the guard form this post -
Chef: Initialize new linux user if it doesn't already exist - Stack Overflow
HTH
-Eric Helgeson
@nulleric https://twitter.com/nulleric
http://usingchef.com
On Sat, Feb 28, 2015 at 4:42 PM, Anthony Kong anthony.hw.kong@gmail.com
wrote:
Hi
I have this definition on my cookbook
group "sensu" do
action :modify
members "vagrant"
append true
end
it will throw an exception when I run it on ec2 because there is no user
vagrant
How can I avoid the exception? Is there any way to detect the user
exist? I have checked the doc group Resource
but there is nothing obvious
Cheers,
Documentation for Ruby’s ‘Etc’ module is here
http://ruby-doc.org/stdlib-2.0/libdoc/etc/rdoc/Etc.html . You could use it
in place of your shell guard like so:
group "sensu" do
action :modify
members "vagrant"
append true
only_if { Etc.getpwnam('vagrant') rescue false }end
The “rescue” is required because .getpwnam raises an exception if the user
can’t be found. If you want to avoid the sin of in-line “rescue”, this is a
bit better:
group "sensu" do
action :modify
members "vagrant"
append true
only_if do
begin
Etc.getpwnam('vagrant')
rescue ArgumentError
false
end
endend
I’ll be honest, I prefer the “getent passwd vagrant” solution more. It’s
easier to read, and isn’t any less efficient or more platform dependent
than the Ruby solution as far as I can tell.
Matt Moretti
On Sun, Mar 1, 2015 at 8:47 PM, Anthony Kong anthony.hw.kong@gmail.com
wrote:
Hi Mark,
Can you shed more light on it? Do you have a url to the documentation or
some example?
Cheers,
Tony Kong
blog: www.ahwkong.com
linkedin: www.linkedin.com/in/anthonykong
Don’t EVER make the mistake that you can design something better than what
you get from ruthless massively parallel trial-and-error with a feedback
cycle. That’s giving your intelligence much too much credit.
On Mon, Mar 2, 2015 at 4:12 AM, Mark Pimentel markpimentel22@gmail.com
wrote:
For a more ruby-esque way you can use the built-in method Etc.
On Feb 28, 2015 5:56 PM, "Eric Helgeson" erichelgeson@gmail.com wrote:
Hey Anthony,
You can use a guard in this case to check and execute the resource
'only_if' the condition is true. You can use ruby or specify a command.
$ cat test.rb
group "sensu" do
action :modify
members "vagrant"
append true
only_if "getent passwd vagrant"
end
$ chef-apply test.rb
Recipe: (chef-apply cookbook)::(chef-apply recipe)
* group[sensu] action modify (*skipped due to only_if*)
More info on Guards - Common Resource Functionality
Grabbed the guard form this post -
Chef: Initialize new linux user if it doesn't already exist - Stack Overflow
HTH
-Eric Helgeson
@nulleric https://twitter.com/nulleric
http://usingchef.com
On Sat, Feb 28, 2015 at 4:42 PM, Anthony Kong <anthony.hw.kong@gmail.com
wrote:
Hi
I have this definition on my cookbook
group "sensu" do
action :modify
members "vagrant"
append true
end
it will throw an exception when I run it on ec2 because there is no
user vagrant
How can I avoid the exception? Is there any way to detect the user
exist? I have checked the doc group Resource
but there is nothing obvious
Cheers,