ansible / ansible-playbook版本:2.1.2.0 / 2.2.0.0
我收到错误信息(当我在运行我的ansible-playbook时传递 -vvv
) .
[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 doc来创建这个playbook动作,如何使用 until
loop here,但是使用该语法我有任何名为stdout的键 . 然后我尝试使用result.rc(关键字段),它在CentOS机器上运行但在Ubuntu 14.x可信赖的流浪汉机器上失败,结果如下.rc dict not present error:
- 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对象'在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 )没有 stdout
或 rc
dict / variables当文档说存在或者我可以在一个操作系统上的 -vvv
详细输出中看到它们但是它没有显示在另一个操作系统上?
1 回答
Correct answer and findings:
In CentOS ,如果操作成功并且如果设置了已注册的变量结果,则用户可以看到这些dict变量,例如:result.rc为0,result.stdout = "something\nsomethingEsle\netc etc etc"和/或result.stdout_lines = "...same as above..."和结果 . 在某些情况下会导致如果操作失败,那么我无法看到result.stdout在我的情况下设置为使用
yum
模块,如果由于connection reset
或其他原因导致失败 . 在这种情况下,可用于失败操作的唯一dict变量是result.rc!= 0.因此,在until
循环中,until: result.rc == 0
条件适用于CentOS的成功/失败情况 .In Ubuntu ,如果
apt
模块操作成功,我设置了result.stdout和result.stdout_lines变量但没有result.rc dict变量集 . 对于失败的操作,使用这些字典变量之一's was no result.stdout or result.stdout_lines or result.rc dict variables set. SO, in case of Ubuntu, I couldn' t使用until
循环 .我能想到的唯一解决方法是,将
apt-get install <pacakge>
包装在一个脚本中,然后在BASH等中使用until循环来执行Ansible正在做的事情 . 至少,编码会更容易,如果退出代码不是0,那么我可以使用until
.但是,我 finally found a solution ,它在Ubuntu中可用于成功和失败的情况:
Jinja2过滤器可以帮助,即
when: result|failed
因此,如果我必须使用注册变量result
作为失败条件,然后使用until
,我将使用它(与失败状态相反),即:好处是,上面的语句将适用于CentOS和Ubuntu以及成功/失败状态 .