Hi Mike,

On Thu, Mar 24, 2016 at 12:30 PM, Mike Reed <mjohn.r...@gmail.com> wrote:

> Hey Peter,
>
> Thank you for the reply.
>
> If I run the command on a machine that I know is 'joined' to the domain
> and run 'echo $?' immediately after, I get a return of '0'.
>

If you're going solely off of grep's exit status, try this:

```
Facter.add(:pbis_joined) do
  setcode do
      !Facter::Util::Resolution.exec(%q{/opt/pbis/bin/domainjoin-cli query
| grep -i 'domain' | grep -i '<domain>'}).nil? && $?.success?
  end
end
```

Breaking that down:

```
!Facter::Util::Resolution.exec(%q{/opt/pbis/bin/domainjoin-cli query | grep
-i 'domain' | grep -i '<domain>'}).nil?
```

This runs the command and ensures #exec doesn't return nil (which will
happen if it failed to execute the command, e.g. command not found).

```
$?.success?
```

This ensures the executed process exited with a zero exit code (i.e. $? in
a shell == 0).

On an aside: do you need the piped greps?  Seems like the latter grep would
be all that's needed assuming you're looking for just "<domain>", unless
you intended this to be either "domain" or "<domain>", which won't work as
currently constructed.


>
> Thank you again,
>
> Mike
>
> On Thursday, March 24, 2016 at 12:13:53 PM UTC-7, Peter Huene wrote:
>>
>> Hi Mike,
>>
>> On Thu, Mar 24, 2016 at 11:59 AM, Mike Reed <mjohn...@gmail.com> wrote:
>>
>>> Hey all,
>>>
>>> I'm a little stumped on why this fact is returning the incorrect value
>>> and I was hoping somebody would have some advice on this one.
>>>
>>> The fact is very basic and looks like this:
>>>
>>> # custom fact for detecting if machine is joined to the domain
>>> require 'facter'
>>>
>>> Facter.add(:pbis_joined) do
>>>   setcode do
>>>       domain_check = %q{/opt/pbis/bin/domainjoin-cli query | grep -i
>>> 'domain' | grep -i '<domain>'}
>>>     domain_return = Facter::Util::Resolution.exec(domain_check)
>>>     if domain_return == 0
>>>
>>
>> Facter::Util::Resolution.exec returns the command's output as a string or
>> nil if the command failed.  Thus, I would expect this conditional
>> (`domain_return == 0`) to never evaluate to true because a string or a nil
>> can never equal 0.
>>
>> What does the command output if you run it manually when you expect the
>> `pbis_joined` facter to be true?
>>
>>
>>>       pbis_joined = true
>>>     else
>>>       pbis_joined = false
>>>     end
>>>   end
>>> end
>>>
>>
>> Also, the `pbis_joined` local variable serves no purpose here, as it is
>> the return value from the block that sets the fact's value.
>>
>>
>>>
>>> Regardless of whether the domain_check command is a success on the
>>> client, I always get a value of 'true' from the fact.  In fact, if I
>>> completely remove the /opt/pbis folder from the machine and run the fact
>>> again, I still get a value of 'true'.  Additionally, If I manually run the
>>> domain_check command on a machine that I know is not joined to the domain,
>>> I will still get a value of 'true' from the fact.
>>>
>>> I've also tried both single '=' and double '==' for the 'if
>>> domain_return' piece of the fact and neither seem to give me what I want.
>>> I then thought that the domain_check object may not be considered an
>>> integer when comparing against the '0' so I added 'domain_return.to_i' and
>>> still got the same result.
>>>
>>> The code within the manifest that looks to this fact looks like this:
>>> if ($::pbis_joined == true) {etc,etc,etc}
>>>
>>> I do have stringify_facts set to 'false' within puppet.conf on both the
>>> client and the master.
>>>
>>> Puppet version 3.6.2/Hiera version: 1.3.4
>>>
>>> Does anybody have any suggestions they can give me as to why I'm not
>>> getting the correct return value?
>>>
>>> As always, thanks to everybody for their help and support.
>>>
>>> Cheers,
>>>
>>> Mike
>>>
>>>
>>>
>>>
>>>
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Puppet Users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to puppet-users...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/puppet-users/a1f8ece8-627b-478d-a108-97c91d76d654%40googlegroups.com
>>> <https://groups.google.com/d/msgid/puppet-users/a1f8ece8-627b-478d-a108-97c91d76d654%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to puppet-users+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/c7d8d40d-5121-426e-a208-a8a4af3bffb1%40googlegroups.com
> <https://groups.google.com/d/msgid/puppet-users/c7d8d40d-5121-426e-a208-a8a4af3bffb1%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to puppet-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/CACZQQfNU7KMZskD8B2K3V7vVN57VkoRUwMOb817bLy705jVujA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to