49

I want to print out messages and variables when Puppet runs. I saw there are two functions that might help but couldn't really use them. My site.pp file:

info "running site.pp info"
debug "running site.pp debug"

When I run on the client:

puppet -t

I don't get those prints.

brasofilo
  • 25,496
  • 15
  • 91
  • 179
Iftach Bar
  • 491
  • 1
  • 4
  • 4

10 Answers10

66

Here is the puppet script with all the available puppet log functions.

log_levels.pp

node default {
  notice("try to run this script with -v and -d to see difference between log levels")
  notice("function documentation is available here: http://docs.puppetlabs.com/references/latest/function.html")
  notice("--------------------------------------------------------------------------")

  debug("this is debug. visible only with -d or --debug")
  info("this is info. visible only with -v or --verbose or -d or --debug")
  alert("this is alert. always visible")
  crit("this is crit. always visible")
  emerg("this is emerg. always visible")
  err("this is err. always visible")
  warning("and this is warning. always visible")
  notice("this is notice. always visible")
  #fail will break execution
  fail("this is fail. always visible. fail will break execution process")

}

Script output (on puppet 2.7): different log levels colors

NB: puppet 3.x colours may alter (all the errors will be printed in red)!

Aleksey Timohin
  • 591
  • 1
  • 9
  • 15
  • 10
    When running as a master/agent, these messages are only displayed on the puppet master. eg /var/log/puppet/master.log All puppet function run on the master, and only the master. –  Jan 30 '14 at 00:54
  • 1
    So many warning levels... I think they're all listed/generated at https://github.com/puppetlabs/puppet/blob/master/lib/puppet/util/log.rb#L14 – Ehtesh Choudhury Jul 15 '14 at 17:37
  • 1
    // , Are these supposed to show up on the output of puppet client runs? – Nathan Basanese Mar 03 '17 at 01:21
60

from the Puppet function documentation

info: Log a message on the server at level info.
debug: Log a message on the server at level debug.

You have to look a your puppetmaster logfile to find your info/debug messages.

You may use

notify{"The value is: ${yourvar}": }

to produce some output to your puppet client

Rob Kielty
  • 7,958
  • 8
  • 39
  • 51
Michael Gisbers
  • 792
  • 4
  • 3
  • 11
    You can also use notice("message") to print a message to the puppet master log (located on the puppet master). – GregB Feb 05 '13 at 19:25
  • 3
    The disadvantage of notify() is that you get a double-declaration error when you use it in a defined type that is called more than once. notice() works in that case though. – OmarOthman Oct 03 '18 at 13:45
  • If you get a duplicate declaration error, you can provide something unique in the `notify` declaration. For example, create a `$servername` and `$datetime` variable: `notify { "$servername $datetime: The value is: ${yourvar}": }`. Virtual and exported resources can cause this duplicate issue. – wsams Oct 15 '18 at 21:04
  • 4
    A really important feature of notify is you can pass a metavar 'loglevel'. This means you can make sure that your messages can be shown as warnings or errors as well. `notify{'This is bad': loglevel => 'warning'}` – Ultimation Apr 24 '19 at 09:36
20

If you want to notify user with different type of messages like information, debug, error, warning, alerts, critical and emergency messages then use ‘loglevel’ metaparameter in puppet resources.

With the use of loglevel you can use the same resources for different type of error messages.

e.g for producing debug messages you can use it as,

 notify {"debug message":
      loglevel => debug,
    }
Rahul Khengare
  • 1,186
  • 1
  • 13
  • 15
12

Just as alternative you may consider using execs... (I wouldn't recommend it though)

exec { 'this will output stuff':
  path      => '/bin',
  command   => 'echo Hello World!',
  logoutput => true,
}

So when you run puppet you should find some output like so:

notice: /Stage[main]//Exec[this will output stuff]/returns: Hello World!
notice: /Stage[main]//Exec[this will output stuff]/returns: executed successfully
notice: Finished catalog run in 0.08 seconds

The first line being logged output.

Sekm
  • 1,658
  • 13
  • 22
  • Thanks! Yeah that works, or you could echo the output to a file, so you don't have to remember to set `logoutput` to true. `'echo Hello World! > C:\temp\output.txt'`. – twasbrillig Oct 27 '14 at 19:27
9

You can run the client like this ...

puppet agent --test --debug --noop

with that command you get all the output you can get.

excerpt puppet agent help
* --test:
  Enable the most common options used for testing. These are 'onetime',
  'verbose', 'ignorecache', 'no-daemonize', 'no-usecacheonfailure',
  'detailed-exitcodes', 'no-splay', and 'show_diff'.

NOTE: No need to include --verbose when you use the --test|-t switch, it implies the --verbose as well.

slm
  • 15,396
  • 12
  • 109
  • 124
Simon
  • 181
  • 1
  • 2
6

That does the task for me. I use that to check vars and display notifications..

notify {"hello world $var1":}

Here's the documentation on Puppet website as well: http://docs.puppetlabs.com/learning/ordering.html#notify-and-subscribe

maths
  • 1,399
  • 5
  • 23
  • 38
2

Easier way, use notice. e.g notice("foo.pp works") or notice($foo)

sorabh
  • 297
  • 1
  • 3
  • 11
2

If, like me, you don't have access to the puppet master and need to print debug logs to inspect variables on your puppet client machine, you can try writing to a file from your puppet code itself:

file { '/tmp/puppet_debug.log':
  content => inline_template('<%= @variable_x.to_s %>'),
}
Srikanth
  • 973
  • 2
  • 9
  • 19
0

You could go a step further and break into the puppet code using a breakpoint.

http://logicminds.github.io/blog/2017/04/25/break-into-your-puppet-code/

This would only work with puppet apply or using a rspec test. Or you can manually type your code into the debugger console. Note: puppet still needs to know where your module code is at if you haven't set already.

gem install puppet puppet-debugger 
puppet module install nwops/debug
cat > test.pp <<'EOF'
$var1 = 'test'
debug::break()
EOF

Should show something like.

puppet apply test.pp
From file: test.pp
     1: $var1 = 'test'
     2: # add 'debug::break()' where you want to stop in your code
  => 3: debug::break()
1:>> $var1
=> "test"
2:>>

https://www.puppet-debugger.com

0

Have you tried what is on the sample. I am new to this but here is the command: puppet --test --trace --debug. I hope this helps.

Efox
  • 651
  • 3
  • 8
  • 19