本文介绍了在可播放的剧本任务中,当Shell命令执行失败时,如何仅输出stderr_lines的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ansible剧本中,我有一个执行shell命令的任务.该命令的参数之一是密码.当shell命令失败时,ansible会打印出整个json对象,其中包括具有密码的命令.如果我使用no_log: True,那么我将得到检查输出,而无法获得stderr_lines.当shell命令执行失败时,是否可以自定义输出?

In my ansible playbook I have a task that executes a shell command. One of the parameters of that command is password. When the shell command fails, ansible prints the whole json object that includes the command having password. If I use no_log: True then I get censored output and not able to get stderr_lines. Is there a way to customize the output when shell command execution fails?

推荐答案

您可以利用ansible 及其错误处理功能.

You can take advantage of ansible blocks and their error handling feature.

这是一个示例剧本

---
- name: Block demo for shell
  hosts: localhost
  gather_facts: false

  tasks:

    - block:

        - name: my command
          shell: my_command is bad
          register: cmdresult
          no_log: true

      rescue:

        - name: show error
          debug:
            msg: "{{ cmdresult.stderr }}"

        - name: fail the playbook
          fail:
            msg: Error on command. See debug of stderr above

给出以下结果:

PLAY [Block demo for shell] *********************************************************************************************************************************************************************************************************************************************

TASK [my command] *******************************************************************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"censored": "the output has been hidden due to the fact that 'no_log: true' was specified for this result", "changed": true}

TASK [show error] *******************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "/bin/sh: 1: my_command: not found"
}

TASK [fail the playbook] ************************************************************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Error on command. See debug of stderr above"}

PLAY RECAP **************************************************************************************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=1    ignored=0

这篇关于在可播放的剧本任务中,当Shell命令执行失败时,如何仅输出stderr_lines的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 08:53