# How to detect user does exist?

**URL:** <https://discourse.chef.io/t/how-to-detect-user-does-exist/6363>\
**Category:** Chef Infra (archive)\
**Created:** [February 28, 2015, 10:42pm UTC](https://discourse.chef.io/t/how-to-detect-user-does-exist/6363 "2015-02-28T22:42:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anthony\_Kong](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/anthony_kong/32/73_2.png) [@Anthony\_Kong](https://discourse.chef.io/u/Anthony_Kong)\
**Post date:** [February 28, 2015, 10:42pm UTC](https://discourse.chef.io/t/how-to-detect-user-does-exist/6363/1 "2015-02-28T22:42:11Z")

</div>

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](https://docs.chef.io/resource_group.html) but there is  
nothing obvious

Cheers,

---

<div class="post-metadata">

**Author:** ![Eric\_Helgeson](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/eric_helgeson/32/67_2.png) [@Eric\_Helgeson](https://discourse.chef.io/u/Eric_Helgeson)\
**Post date:** [February 28, 2015, 10:56pm UTC](https://discourse.chef.io/t/how-to-detect-user-does-exist/6363/2 "2015-02-28T22:56:31Z")

</div>

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.

```auto
$ 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](https://docs.chef.io/resource_common.html#guards)

Grabbed the guard form this post -

> <https://stackoverflow.com/questions/24856339/chef-initialize-new-linux-user-if-it-doesnt-already-exist>

​  
HTH​

-Eric Helgeson  
@nulleric [https://twitter.com/nulleric](https://twitter.com/nulleric)  
[http://usingchef.com](http://usingchef.com)

On Sat, Feb 28, 2015 at 4:42 PM, Anthony Kong [anthony.hw.kong@gmail.com](mailto: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](https://docs.chef.io/resource_group.html) but there  
> is nothing obvious
> 
> Cheers,

---

<div class="post-metadata">

**Author:** ![Anthony\_Kong](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/anthony_kong/32/73_2.png) [@Anthony\_Kong](https://discourse.chef.io/u/Anthony_Kong)\
**Post date:** [February 28, 2015, 11:40pm UTC](https://discourse.chef.io/t/how-to-detect-user-does-exist/6363/3 "2015-02-28T23:40:56Z")

</div>

Thanks! It fixed my problem!

On Sun, Mar 1, 2015 at 9:56 AM, Eric Helgeson [erichelgeson@gmail.com](mailto: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.
> 
> ```auto
> $ 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](https://docs.chef.io/resource_common.html#guards)
> 
> Grabbed the guard form this post -  
> [Chef: Initialize new linux user if it doesn't already exist - Stack Overflow](http://stackoverflow.com/questions/24856339/chef-initialize-new-linux-user-if-it-doesnt-already-exist)  
> ​  
> HTH​
> 
> -Eric Helgeson  
> @nulleric [https://twitter.com/nulleric](https://twitter.com/nulleric)  
> [http://usingchef.com](http://usingchef.com)
> 
> On Sat, Feb 28, 2015 at 4:42 PM, Anthony Kong [anthony.hw.kong@gmail.com](mailto: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](https://docs.chef.io/resource_group.html) but  
> > there is nothing obvious
> > 
> > Cheers,

---

<div class="post-metadata">

**Author:** ![Mark\_Pimentel](https://avatars.discourse-cdn.com/v4/letter/m/9f8e36/32.png) [@Mark\_Pimentel](https://discourse.chef.io/u/Mark_Pimentel)\
**Post date:** [March 1, 2015, 5:12pm UTC](https://discourse.chef.io/t/how-to-detect-user-does-exist/6363/4 "2015-03-01T17:12:55Z")

</div>

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](mailto: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.
> 
> ```auto
> $ 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](https://docs.chef.io/resource_common.html#guards)
> 
> Grabbed the guard form this post -  
> [Chef: Initialize new linux user if it doesn't already exist - Stack Overflow](http://stackoverflow.com/questions/24856339/chef-initialize-new-linux-user-if-it-doesnt-already-exist)  
> ​  
> HTH​
> 
> -Eric Helgeson  
> @nulleric [https://twitter.com/nulleric](https://twitter.com/nulleric)  
> [http://usingchef.com](http://usingchef.com)
> 
> On Sat, Feb 28, 2015 at 4:42 PM, Anthony Kong [anthony.hw.kong@gmail.com](mailto: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](https://docs.chef.io/resource_group.html) but  
> > there is nothing obvious
> > 
> > Cheers,

---

<div class="post-metadata">

**Author:** ![Anthony\_Kong](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/anthony_kong/32/73_2.png) [@Anthony\_Kong](https://discourse.chef.io/u/Anthony_Kong)\
**Post date:** [March 2, 2015, 1:47am UTC](https://discourse.chef.io/t/how-to-detect-user-does-exist/6363/5 "2015-03-02T01:47:37Z")

</div>

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](http://www.ahwkong.com)  
_linkedin:_ [www.linkedin.com/in/anthonykong](http://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.

- Linus Torvalds

On Mon, Mar 2, 2015 at 4:12 AM, Mark Pimentel [markpimentel22@gmail.com](mailto: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](mailto: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.
> > 
> > ```auto
> > $ 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](https://docs.chef.io/resource_common.html#guards)
> > 
> > Grabbed the guard form this post -  
> > [Chef: Initialize new linux user if it doesn't already exist - Stack Overflow](http://stackoverflow.com/questions/24856339/chef-initialize-new-linux-user-if-it-doesnt-already-exist)  
> > ​  
> > HTH​
> > 
> > -Eric Helgeson  
> > @nulleric [https://twitter.com/nulleric](https://twitter.com/nulleric)  
> > [http://usingchef.com](http://usingchef.com)
> > 
> > On Sat, Feb 28, 2015 at 4:42 PM, Anthony Kong [anthony.hw.kong@gmail.com](mailto: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](https://docs.chef.io/resource_group.html)  
> > > but there is nothing obvious
> > > 
> > > Cheers,

---

<div class="post-metadata">

**Author:** ![werebus](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/werebus/32/24_2.png) [@werebus](https://discourse.chef.io/u/werebus)\
**Post date:** [March 2, 2015, 3:54am UTC](https://discourse.chef.io/t/how-to-detect-user-does-exist/6363/6 "2015-03-02T03:54:03Z")

</div>

Documentation for Ruby’s ‘Etc’ module is here  
[http://ruby-doc.org/stdlib-2.0/libdoc/etc/rdoc/Etc.html](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](mailto: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](http://www.ahwkong.com)  
> _linkedin:_ [www.linkedin.com/in/anthonykong](http://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.
> 
> - Linus Torvalds
> 
> On Mon, Mar 2, 2015 at 4:12 AM, Mark Pimentel [markpimentel22@gmail.com](mailto: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](mailto: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.
> > > 
> > > ```auto
> > > $ 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](https://docs.chef.io/resource_common.html#guards)
> > > 
> > > Grabbed the guard form this post -  
> > > [Chef: Initialize new linux user if it doesn't already exist - Stack Overflow](http://stackoverflow.com/questions/24856339/chef-initialize-new-linux-user-if-it-doesnt-already-exist)  
> > > ​  
> > > HTH​
> > > 
> > > -Eric Helgeson  
> > > @nulleric [https://twitter.com/nulleric](https://twitter.com/nulleric)  
> > > [http://usingchef.com](http://usingchef.com)
> > > 
> > > On Sat, Feb 28, 2015 at 4:42 PM, Anthony Kong \<[anthony.hw.kong@gmail.com](mailto: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](https://docs.chef.io/resource_group.html)  
> > > > but there is nothing obvious
> > > > 
> > > > Cheers,
