Accessgin facts hash from manifests

35 views
Skip to first unread message

Arnau

unread,
Apr 20, 2018, 8:22:23 AM4/20/18
to puppet...@googlegroups.com
Hi all,

I'm having the first experiences with puppet 5 & facter 3 

In old puppet versions I tend to add debug messages like:

notify { "OS : {::osfamily}: }

and it usually worked.

According to https://puppet.com/docs/puppet/5.3/lang_facts_and_builtin_vars.html#accessing-facts-from-puppet-code I can still access the facts using the old way, but it recommend to use the facts has, so the new code should look like:


  notify { "OS: $facts[os][family]": }

But when I do that I get the full list of facts and [os][family] at the bottom:


Notice: OS: {agent_specified_environment => test, aio_agent_version => 5.5.1, architecture => x86_64, augeas => {version => 1.10.1}
[.... TONS OF FACTS ...]  clientversion => 5.5.1, clientnoop => false}['os]['family']"

If I use the same syntax in a conditiona statement:

  if $facts['os']['family'] == 'RedHat' {
    notify { "I'm a RedHat": }
  }

then it works:

Notice: /Stage[main]/Common::Yumrepos/Notify[I'm a RedHat]/message: defined 'message' as 'I\'m a RedHat'

So, what's wrong with the above notify? 


TIA,
Arnau


Johan De Wit

unread,
Apr 23, 2018, 2:33:21 AM4/23/18
to puppet...@googlegroups.com

first notify, the index should be quoted ....

notify { "OS: $facts['os']['family']": }


Grts




--
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/CAM69jx_sAms0%3Da%2B5MhHBtnydtPKsUDeAAcera8tXSsaANaTchA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Henrik Lindberg

unread,
Apr 23, 2018, 5:01:22 AM4/23/18
to puppet...@googlegroups.com
On 23/04/18 08:33, Johan De Wit wrote:
> first notify, the index should be quoted ....
>
> notify { "OS: $facts['os']['family']": }
>
>
That us unfortunately wrong - it will produce the entire $facts hash as
a string followed by the *text* "['os']['family']".

The correct way is to write:

notify { "OS: ${facts['os']['family']}": }

Or, if you like:

notify { "OS: ${facts.dig('os', 'family')}

(or by using the "pick()" function from stdlib which is similar to "dig()")

Best,
- henrik

> Grts
>
>
>
>
> -----Original message-----
> *From:* Arnau <lists...@gmail.com>
> *Sent:* Friday 20th April 2018 14:22
> *To:* puppet...@googlegroups.com
> *Subject:* [Puppet Users] Accessgin facts hash from manifests
> <mailto:puppet-users...@googlegroups.com>.
> <https://groups.google.com/d/msgid/puppet-users/CAM69jx_sAms0%3Da%2B5MhHBtnydtPKsUDeAAcera8tXSsaANaTchA%40mail.gmail.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...@googlegroups.com
> <mailto:puppet-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/zarafa.5add7e24.7f1a.78dfd5c52ec466fa%40zarafa7.open-future.be
> <https://groups.google.com/d/msgid/puppet-users/zarafa.5add7e24.7f1a.78dfd5c52ec466fa%40zarafa7.open-future.be?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

Arnau

unread,
Apr 25, 2018, 9:22:46 AM4/25/18
to puppet...@googlegroups.com
Hi,

first of all, thanks for your answer.
Using your code I still see the whole $facts hash....

notify { "OS: $facts['os']['family']": }


As Henrik mentioned it must be:

notify { "OS: ${facts['os']['family']}": }


Best,
Arnau

To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/zarafa.5add7e24.7f1a.78dfd5c52ec466fa%40zarafa7.open-future.be.

Arnau

unread,
Apr 25, 2018, 9:23:07 AM4/25/18
to puppet...@googlegroups.com
Thanks a lot for your answer! it worked.

Best,
Arnau

To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com <mailto:puppet-users+unsubscrib...@googlegroups.com>.


--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/
--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/pbk789%242rk%241%40blaine.gmane.org.
Reply all
Reply to author
Forward
0 new messages