"puppet apply --noop -e" and overiding facter values for testing

879 views
Skip to first unread message

Peter

unread,
May 3, 2014, 11:41:17 AM5/3/14
to puppet...@googlegroups.com
Hi Puppet Users,

Firstly I already know that I can override facter variables by adding them to the commandline.  Eg:

root@dna:~# FACTER_fqdn=foo.bar.info puppet apply --noop -e 'notice("${fqdn}")'
Notice: Scope(Class[main]): foo.bar.info
Notice: Compiled catalog for dna.local in environment production in 0.03 seconds
Notice: Finished catalog run in 0.04 seconds

I would like to use a number of different FACTER overrides, I can add them all to the command line but I want to test different values at different times.  I was hoping that I could have a file like:
root@dna:~# cat test_values
FACTER_hostname=bob
FACTER_domain=mgnt.local
FACTER_fqdn=bob.mgnt.local
FACTER_foo=foo

Then like with the hiera command line tool use an option to tell puppet apply to use this file and override any facts with the same name.  However there isnt an option.

I have found a bash command which will take the values from the file and concat them together to form one line:
root@dna:~# cat test_values | tr '\n' ' '
FACTER_hostname=bob FACTER_domain=mgnt.local FACTER_fqdn=bob.mgnt.local FACTER_foo=foo

I was then hoping I could do something like:
root@dna:~# $(cat test_values | tr '\n' ' ' ) puppet apply --noop -e 'notice("${fqdn}")'
-bash: FACTER_hostname=bob: command not found

However it doesn't work.

My question is, does anyone know a puppet way pass a file to puppet apply which will override facts or if there are any bash experts out there how I can have the output of the command as plan text.

Thanks,

Peter

liu.cy

unread,
May 4, 2014, 1:49:52 PM5/4/14
to puppet...@googlegroups.com
try add "export" before Facter_hostname=bob

Brian Mathis

unread,
May 5, 2014, 10:29:35 PM5/5/14
to puppet...@googlegroups.com
The problem is that your variable names are being returned from the $(cat...) after bash has already evaluated the environment, so it's taking it as a literal string and trying to execute the command.

To get bash to interpret it as a variable, use 'eval':
   eval $(cat test_values | tr '\n' ' ' ) puppet apply --noop -e 'notice("${fqdn}")'

Also, on modern versions of facter you can place facts in /etc/facter/facts.d and they will override the detected versions.


❧ Brian Mathis


--
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/fc389ac2-c451-434e-804e-1bbaf9e00117%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jakov Sosic

unread,
May 6, 2014, 11:43:21 AM5/6/14
to puppet...@googlegroups.com
On 05/06/2014 12:29 AM, Brian Mathis wrote:
> The problem is that your variable names are being returned from the
> $(cat...) after bash has already evaluated the environment, so it's
> taking it as a literal string and trying to execute the command.
>
> To get bash to interpret it as a variable, use 'eval':
> eval $(cat test_values | tr '\n' ' ' ) puppet apply --noop -e
> 'notice("${fqdn}")'
>
> Also, on modern versions of facter you can place facts in
> /etc/facter/facts.d and they will override the detected versions.

Can you show an example how to override facter value with file in
/etc/facter/facts.d/ ? For example, how to override hostname?

Brian Mathis

unread,
May 6, 2014, 12:38:56 PM5/6/14
to puppet...@googlegroups.com
This applies to facter version 1.7 and above.  See docs: http://docs.puppetlabs.com/guides/custom_facts.html#external-facts

You can create a file like this (the name does not matter):
    /etc/facter/facts.d/myfacts.txt:
    hostname=value

Then when you run 'facter hostname', "value" will be returned.


❧ Brian Mathis


--
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/5368CAD9.1030302%40gmail.com.

Jakov Sosic

unread,
May 6, 2014, 10:01:57 PM5/6/14
to puppet...@googlegroups.com
On 05/06/2014 02:38 PM, Brian Mathis wrote:
> This applies to facter version 1.7 and above. See docs:
> http://docs.puppetlabs.com/guides/custom_facts.html#external-facts
>
> You can create a file like this (the name does not matter):
> /etc/facter/facts.d/myfacts.txt:
> hostname=value
>
> Then when you run 'facter hostname', "value" will be returned.

Thank you!

Peter

unread,
May 8, 2014, 6:41:45 AM5/8/14
to puppet...@googlegroups.com
Hi Brian,

Sorry about the delay in responding.  Unfortunately eval didn't work or at least fqdn does not output the value I set it to in test_values.

I am just going to rework my testing to dump a file into /etc/facter/facts.d and remove it after use.  Not ideal but it works with the least amount of fussing around.  I would have loved to get the commandline option working as I would not have to add and remove files when I am testing things out.

Thanks,

Peter.

José Luis Ledesma

unread,
May 8, 2014, 2:54:30 PM5/8/14
to puppet...@googlegroups.com
Hi Peter,

   I have been following this thread, but I always readed it with the mobile, so sorry for the late answer. The solution is:

# sh -c "$(cat test_values | tr '\n' ' ' ) puppet apply --noop -e 'notify{\"\${fqdn}\":}'"
Notice: Compiled catalog for bob.mgnt.local in environment production in 0.02 seconds
Notice: /Stage[main]/Main/Notify[bob.mgnt.local]/message: current_value absent, should be bob.mgnt.local (noop)
Notice: Class[Main]: Would have triggered 'refresh' from 1 events
Notice: Stage[main]: Would have triggered 'refresh' from 1 events
Notice: Finished catalog run in 0.13 seconds

I have changed the notice for a notify (but thats just a test). Keep in mind to escape " and $

regards,



For more options, visit https://groups.google.com/d/optout.



--
José Luis Ledesma

Peter

unread,
May 23, 2014, 1:54:42 PM5/23/14
to puppet...@googlegroups.com
Hi José

Sorry about the delay in responding I was sidetracked with other work.  A massive thank you!!!!!

Your solution works!  As a temporary work around I ended up just moving files in and out of the facter fact.d, this solution is a bit more convenient.

Thanks,

Peter
Reply all
Reply to author
Forward
0 new messages