48

我想在 Puppet 运行时打印出消息和变量。我看到有两个功能可能会有所帮助,但不能真正使用它们。我的site.pp文件:

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

当我在客户端上运行时:

puppet -t

我没有得到那些指纹。

4

10 回答 10

63

这是具有所有可用 puppet 日志功能的 puppet 脚本。

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

}

脚本输出(在 puppet 2.7 上): 不同的日志级别颜色

注意:puppet 3.x 颜色可能会改变(所有错误都会以红色打印)!

于 2013-05-21T13:47:31.967 回答
56

来自 Puppet 函数文档

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

您必须查看您的 puppetmaster 日志文件才能找到您的信息/调试消息。

您可以使用

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

为您的 puppet 客户端生成一些输出

于 2010-11-25T13:20:57.443 回答
19

如果您想通过不同类型的消息通知用户,例如信息、调试、错误、警告、警报、关键和紧急消息,请在 puppet 资源中使用“loglevel”元参数。

通过使用 loglevel,您可以将相同的资源用于不同类型的错误消息。

例如,为了生成调试消息,您可以将其用作,

 notify {"debug message":
      loglevel => debug,
    }
于 2013-10-04T10:23:17.067 回答
12

作为替代方案,您可以考虑使用 execs ......(虽然我不推荐它)

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

因此,当您运行 puppet 时,您应该会找到如下输出:

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

第一行被记录输出。

于 2013-07-16T04:25:55.113 回答
9

您可以像这样运行客户端...

puppet agent --test --debug --noop

使用该命令,您可以获得所有可以得到的输出。

节选傀儡代理帮助
* --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'.

注意:--verbose使用开关时不需要包括--test|-t,它也意味着--verbose

于 2013-05-19T08:52:52.333 回答
6

这为我完成了任务。我用它来检查变量并显示通知..

notify {"hello world $var1":}

以下是 Puppet 网站上的文档:http: //docs.puppetlabs.com/learning/ordering.html#notify-and-subscribe

于 2014-04-24T23:39:59.743 回答
2

更简单的方法,使用通知。例如 notice("foo.pp works") 或 notice($foo)

于 2014-01-30T17:49:49.680 回答
2

如果像我一样,您无法访问 puppet master 并且需要打印调试日志以检查 puppet 客户端计算机上的变量,您可以尝试从 puppet 代码本身写入文件:

file { '/tmp/puppet_debug.log':
  content => inline_template('<%= @variable_x.to_s %>'),
}
于 2018-05-21T22:37:24.097 回答
0

你试过样品上的东西吗?我是新手,但这里是命令:puppet --test --trace --debug。我希望这有帮助。

于 2011-05-04T05:45:14.337 回答
0

您可以更进一步,使用断点中断 puppet 代码。

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

这仅适用于 puppet apply 或使用 rspec 测试。或者,您可以手动将代码键入调试器控制台。注意:如果你还没有设置,puppet 仍然需要知道你的模块代码在哪里。

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

应该显示类似的东西。

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

于 2018-05-11T02:21:36.147 回答