开发者

How to print something when running Puppet client?

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:

开发者_StackOverflow社区
puppet -t

I don't get those prints.


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):

How to print something when running Puppet client?

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


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


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,
    }


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.


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.


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


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


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 %>'),
}


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜