gpt4 book ai didi

stdout - Ansible寄存器结果result.stdout result.rc等dict变量未找到,如何使用直到循环

转载 作者:行者123 更新时间:2023-12-01 15:02:05 29 4
gpt4 key购买 nike

ansible/ansible-playbook 版本:2.1.2.0/2.2.0.0

我正在尝试使用 yum/apt 安装软件包,但由于安装软件包的存储库位于 packagecloud.io 中,有时我会收到一条错误消息(当我传递 -vvv 在运行我的 ansible-playbook 时)。

[Errno 14] curl#56 - \"TCP connection reset by peer\"\nTrying other mirror. ...some ansible verbose text here.. [Errno 256] No more mirrors to try.

这不会一直发生。如果我再次重新运行相同的剧本,它可以正常工作,因此故障(连接重置)是随机出现的。

为了解决这个问题,我想使用 Ansible 的 until 循环,我们必须将它与 register 变量一起使用。

所以,我通过引用 Ansible 文档来创建这个剧本操作,以了解如何使用 until 循环 here ,但使用该语法我得到一个“字典”错误,说结果变量(注册的)字典没有任何名为 stdout 的键。然后我尝试使用 result.rc(关键字段),它在 CentOS 机器上运行,但在 Ubuntu 14.x 可信赖的流浪机器上失败,出现以下 result.rc dict not present 错误:

- name: Install telegraf agent/collector (RedHat)
yum:
name: "{{ agent_collector }}"
state: "installed"
when: ( ansible_os_family == 'RedHat' and company_install_collector == true )
register: result
until: result.stdout.find("Installed:") != -1
#The following works in CentOS/RedHat
#until: result.rc == 0

- debug: msg="result (redhat) = {{ result }}"

或者(更新我的问题很明显)

- name: Install Company Proxy (Ubuntu)
apt:
name: "{{ company_proxy_pkg }}"
state: "installed"
when: ( ansible_distribution == 'Ubuntu' and company_install_proxy == true )
register: result
until: result.rc == 0

- debug: msg="result (ubuntu) = {{ result }}"

得到以下错误信息(result.stdout -- dict object' 在 RedHat/CentOS 和 Ubuntu 中都没有属性 'stdout' 但获取 dict 对象只有在 Ubuntu 服务器中没有属性 'rc'):

fatal: [localhost]: FAILED! => {"failed": true, "msg": "The conditional check '(result.stdout.find(\"Installed:\") != -1)' failed. The error was: error while evaluating conditional ((result.stdout.find(\"Installed:\") != -1)): 'dict object' has no attribute 'stdout'"}

fatal: [localhost]: FAILED! => {
"failed": true,
"msg": "The conditional check 'result.rc == 0' failed. The error was: error while evaluating conditional (result.rc == 0): 'dict object' has no attribute 'rc'"
}

当文档说存在时,为什么寄存器变量(在我的情况下为 result)没有 stdoutrc dict/variables或者如果我可以在一个操作系统上的 -vvv 详细输出中看到它们,但它没有显示在另一个操作系统上?

最佳答案

不推荐使用测试作为过滤器。而不是使用 result|succeeded 使用 result is succeeded。此功能将在 2.9 版中移除。

关于stdout - Ansible寄存器结果result.stdout result.rc等dict变量未找到,如何使用直到循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41355185/

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