假设我编写了一个define
,并且能够捕获非法参数组合。如何使用fail
包含至少一个包含我们定义的资源(或者更好的是:完整的调用堆栈)的消息来显示清单?
类似于notice{'message': withpath => true }
答案1
嗯,事实证明是可以的,但是效果并不好。
fail inline_template("<%=
def path(scope)
if scope.parent
path(scope.parent) + '/' + scope.resource.ref
else
''
end
end
path(scope) %> failed: ACTUAL MESSAGE HERE")
它通过在 Ruby 中使用来inline_template
检查解析器的范围来工作。如果您确实要使用它,则应该在解析器函数中实现它。
答案2
1)使用以下命令运行 Puppet -d
:
Info: Applying configuration version '1423947413'
Notice: hello file2
Notice: /Stage[main]/Main/Node[default]/Test::Test_file[file2]/Notify[hello file2]/message: defined 'message' as 'hello file2'
Debug: /Stage[main]/Main/Node[default]/Test::Test_file[file2]/Notify[hello file2]: The container Test::Test_file[file2] will propagate my refresh event
Debug: Test::Test_file[file2]: The container Node[default] will propagate my refresh event
Debug: Node[default]: The container Class[Main] will propagate my refresh event
Debug: Class[Main]: The container Stage[main] will propagate my refresh event
Debug: Finishing transaction 69972484788840
Debug: Storing state
Debug: Stored state in 0.04 seconds
Notice: Finished catalog run in 0.26 seconds
Debug: Using cached connection for https://puppet:8140
Debug: Caching connection for https://puppet:8140
Debug: Closing connection for https://puppet:8140
2)fail('CP1')
在puppet代码中使用
[vagrant@vm-one puppet]$ sudo puppet agent -t
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: CP1 at /etc/puppet/manifests/site.pp:6 on node vm-one.domain
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run
3)使用notify
define test::test_file {
notify {"hello $name":}
}
结果是
[vagrant@vm-one puppet]$ sudo puppet agent -t
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for vm-one.domain
Info: Applying configuration version '1423947413'
Notice: hello file2
Notice: /Stage[main]/Main/Node[default]/Test::Test_file[file2]/Notify[hello file2]/message: defined 'message' as 'hello file2'
Notice: Finished catalog run in 0.11 seconds
4)我认为最重要的是,TDD puppet 使用rspec-puppet。