如何在运行Puppet客户端时打印一些东西?

时间:2010-11-09 15:34:10

标签: puppet

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

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

当我在客户端上运行时:

puppet -t

我没有得到那些照片。

10 个答案:

答案 0 :(得分:56)

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

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上): different log levels colors

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

答案 1 :(得分:51)

来自Puppet函数documentation

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}": }

向你的木偶客户端产生一些输出

答案 2 :(得分:16)

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

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

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

 notify {"debug message":
      loglevel => debug,
    }

答案 3 :(得分:8)

作为替代方案,你可以考虑使用高管......(我不推荐它)

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

第一行是记录输出。

答案 4 :(得分:6)

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

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

答案 5 :(得分:5)

这对我来说是一项任务。我用它来检查变量并显示通知..

notify {"hello world $var1":}

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

答案 6 :(得分:1)

更简单的方法,使用通知。 例如 通知(“foo.pp作品”) 要么 通知($富)

答案 7 :(得分:0)

您是否尝试过样本上的内容。我是新手,但这里是命令:puppet --test --trace --debug。我希望这会有所帮助。

答案 8 :(得分:0)

你可以更进一步,使用断点闯入木偶代码。

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

这只适用于木偶申请或使用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

答案 9 :(得分:0)

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

file { '/tmp/puppet_debug.log':
  content => inline_template('<%= @variable_x.to_s %>'),
}