多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
### 显示命令的输出结果 > Computer says no. > > — Little Britain 一个问题的详细反馈会对解决问题有帮助。当你使用 exec 资源在节点上执行命令时, 并不总能轻易地找出其为何没有正确执行。如果命令返回一个非零的退出状态,Puppet 就会返回类似如下的错误消息: ``` err: /Stage[main]//Node[cookbook]/Exec[this-will-fail]/returns: change from notrun to 0 failed: /bin/ls file-that-doesnt-exist returned 2 instead of one of [0] at /etc/puppet/manifests/nodes.pp:10 ``` 通常我们希望看到执行失败时的输出,而不仅仅是一个退出的状态码。 你可以使用 logoutput 参数实现。 #### 操作步骤 用 logoutput 参数定义如下的 exec 资源: ``` exec { "this-will-fail": command => "/bin/ls file-that-doesnt-exist", logoutput => on_failure, } ``` #### 工作原理 现在,如果命令执行失败,Puppet 同时会打印命令的错误输出: ``` notice: /Stage[main]//Node[cookbook]/Exec[this-will-fail]/returns: /bin/ ls: cannot access file-that-doesnt-exist: No such file or directory err: /Stage[main]//Node[cookbook]/Exec[this-will-fail]/returns: change from notrun to 0 failed: /bin/ls file-that-doesnt-exist returned 2 instead of one of [0] at /etc/puppet/manifests/nodes.pp:11 ``` #### 更多用法 你可以使用如下的配置为所有的 exec 资源设置打印错误输出的默认值: ``` Exec { logoutput => on_failure, } ``` 若不管命令执行成功与否,你都想看到其输出,需使用如下的配置: ``` logoutput => true, ```