gpt4 book ai didi

ansible - 使用Ansible : How to get stderr and stdout from failing commands to be printed respecting newlines so as to be human readable?进行调试

转载 作者:行者123 更新时间:2023-12-04 12:28:04 25 4
gpt4 key购买 nike

如果我运行一个简单的ansible剧本,那么经常会从失败的任务中获得难以阅读的输出,如下所示。大问题:

  • 标准输出中的换行符打印为\n,而不是实际的换行符。这使得诸如python追溯之类的东西非常令人讨厌。
  • stdout,stderr,cmd ...输出的json blob包含很多有用的东西,但是由于它们都在同一行上一起运行,因此
    人类很难解析。

  • 我如何能以易于阅读的格式打印其输出,以便进行调试?

    这是令人讨厌的输出:
    $ ansible-playbook playbooks/backUpWebsite.yml

    PLAY [localhost]
    ***************************************************************

    TASK [command]
    *****************************************************************
    fatal: [localhost]: FAILED! => {"changed": true, "cmd": "python -c 'ksjfasdlkjf'", "delta": "0:00:00.037459", "end": "2017-10-03 19:58:50.525257", "failed": true, "rc": 1, "start": "2017-10-03 19:58:50.487798", "stderr": "Traceback (most recent call last):\n File \"<string>\", line 1, in <module>\nNameError: name 'ksjfasdlkjf' is not defined", "stdout": "", "stdout_lines": [], "warnings": []}
    to retry, use: --limit @<snip>playbooks/backUpWebsite.retry

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

    这是生成它的脚本:
    ---
    - hosts: localhost
    gather_facts: False
    tasks:
    #wrong on purpose!
    - shell: "python -c 'ksjfasdlkjf'"
    register: unobtainable

    最佳答案

    为了能够尊重输出中的换行符,以便您可以读取失败任务的标准输出,将stdout_callback更改为“minimum”。

    在您的ansible.cfg中,更改stdout_callback选项:

    stdout_callback=minimal

    “最小”输出有点简洁,但是它会打印有关换行符的输出,以便您可以看到正在发生的情况:
     $ ansible-playbook playbooks/backUpWebsite.yml
    localhost | FAILED | rc=1 >>
    Traceback (most recent call last):
    File "<string>", line 1, in <module>
    NameError: name 'ksjfasdlkjf' is not defined

    to retry, use: --limit @<snip>/playbooks/backUpWebsite.retry

    我听到的其他事情可以起作用
  • 您可以为stdout编写自己的回调处理程序。 People have done this似乎很简单,但是我从未尝试过。

  • 我尝试过的无效的操作:

    三个vs 。使用“ansible-playbook -vvv”。您会得到大量输出,其中确实包含json很好的换行符,但是不会输出stdout输出中的换行符,因此仍很难阅读stdout/stderr:
    $ ansible-playbook playbooks/backUpWebsite.yml -vvv
    Using <snip>/ansible.cfg as config file

    PLAYBOOK: backUpWebsite.yml ****************************************************
    1 plays in playbooks/backUpWebsite.yml

    PLAY [localhost] ***************************************************************

    TASK [command] *****************************************************************
    task path: <snip>/playbooks/backUpWebsite.yml:6
    Using module file <snip>/ansible/lib/ansible/modules/core/commands/command.py
    <127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: <snip>
    <127.0.0.1> EXEC /bin/sh -c 'echo ~ && sleep 0'
    <127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo <snip>/.ansible/tmp/ansible-tmp-1507083519.4247632-54588405727205 `" && echo ansible-tmp-1507083519.4247632-54588405727205="` echo <snip>/.ansible/tmp/ansible-tmp-1507083519.4247632-54588405727205 `" ) && sleep 0'
    <127.0.0.1> PUT /tmp/tmpumtlourx TO <snip>/.ansible/tmp/ansible-tmp-1507083519.4247632-54588405727205/command.py
    <127.0.0.1> EXEC /bin/sh -c 'chmod u+x <snip>/.ansible/tmp/ansible-tmp-1507083519.4247632-54588405727205/ <snip>/.ansible/tmp/ansible-tmp-1507083519.4247632-54588405727205/command.py && sleep 0'
    <127.0.0.1> EXEC /bin/sh -c '<snip>/anaconda3/bin/python <snip>/.ansible/tmp/ansible-tmp-1507083519.4247632-54588405727205/command.py; rm -rf "<snip>/.ansible/tmp/ansible-tmp-1507083519.4247632-54588405727205/" > /dev/null 2>&1 && sleep 0'
    fatal: [localhost]: FAILED! => {
    "changed": true,
    "cmd": "python -c 'ksjfasdlkjf'",
    "delta": "0:00:00.037657",
    "end": "2017-10-03 20:18:39.843933",
    "failed": true,
    "invocation": {
    "module_args": {
    "_raw_params": "python -c 'ksjfasdlkjf'",
    "_uses_shell": true,
    "chdir": null,
    "creates": null,
    "executable": null,
    "removes": null,
    "warn": true
    },
    "module_name": "command"
    },
    "rc": 1,
    "start": "2017-10-03 20:18:39.806276",
    "stderr": "Traceback (most recent call last):\n File \"<string>\", line 1, in <module>\nNameError: name 'ksjfasdlkjf' is not defined",
    "stdout": "",
    "stdout_lines": [],
    "warnings": []
    }
    to retry, use: --limit @<snip>/sys/ansible_readynet/playbooks/backUpWebsite.retry

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

    信用

    I posted this issue反对github上的ansible,希望说服人们使用ansible的默认值是无济于事的。 jhawkesworth帮助我介绍了“最小” stdout回调。

    此后,我发现 this page in the ansible documentation以非常非突出的方式提及了此插件。

    恕我直言,从ansible获取可读取错误输出的 secret 对于新用户来说太隐秘了,我亲眼目睹了我认识的使用ansible的一些人。

    关于ansible - 使用Ansible : How to get stderr and stdout from failing commands to be printed respecting newlines so as to be human readable?进行调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46556214/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com