YAML中的可解整数变量
我正在使用Ansible部署一个Web应用程序。我希望通过检查给定页面是否返回具有给定键/值的JSON来等待应用程序运行 我希望这项任务在失败之前试几次。因此,我使用了YAML中的可解整数变量,yaml,jinja2,ansible,Yaml,Jinja2,Ansible,我正在使用Ansible部署一个Web应用程序。我希望通过检查给定页面是否返回具有给定键/值的JSON来等待应用程序运行 我希望这项任务在失败之前试几次。因此,我使用了的组合,直到/重试/延迟keybwords 问题是,我希望从变量中获取重试次数。如果我写: retries: {{apache_test_retries}} 我陷入了通常的Yaml Gotcha() 相反,如果我写: retries: "{{apache_test_retries}}" 我是说这个值不是整数 Value
的组合,直到
/重试
/延迟
keybwords
问题是,我希望从变量中获取重试次数。如果我写:
retries: {{apache_test_retries}}
我陷入了通常的Yaml Gotcha()
相反,如果我写:
retries: "{{apache_test_retries}}"
我是说这个值不是整数
ValueError:基数为10的int()的文本无效:“{{apache_test_retries}”
这是我的全部代码:
- name: Wait for the application to be running
local_action:
uri
url=http://{{webapp_url}}/health
timeout=60
register: res
sudo: false
when: updated.changed and apache_test_url is defined
until: res.status == 200 and res['json'] is defined and res['json']['status'] == 'UP'
retries: "{{apache_test_retries}}"
delay: 1
你知道如何解决这个问题吗?谢谢。您应该能够使用以下命令将其转换为整数:
我曾经遇到过类似的问题,在我的例子中,我想重新启动celeryd
服务。有时需要很长时间才能重新启动,我想给它最多30秒的软重启时间,然后强制重启。我为此使用了async
(每5秒轮询一次重启结果)
芹菜/handlers/main.yml
- name: restart celeryd
service:
name=celeryd
state=restarted
register: celeryd_restart_result
ignore_errors: true
async: "{{ async_val | default(30) }}"
poll: 5
- name: check celeryd restart result and force restart if needed
shell: service celeryd kill && service celeryd start
when: celeryd_restart_result|failed
然后我在剧本中使用上面的内容作为任务的处理程序(restart-celeryd
始终是notify
列表中的第一个)
在您的情况下,下面这样的方法可能会起作用。还没有检查它是否有,但它可能会给你一些黑客的想法,以不同的方式解决它。此外,由于您将忽略第一个任务中的错误,因此您需要确保第二个任务中的情况良好:
- name: Poll to check if the application is running
local_action:
uri
url=http://{{webapp_url}}/health
timeout=60
register: res
sudo: false
when: updated.changed and apache_test_url is defined
failed_when: res.status != 200 and res['json'] is not defined and not res['json']['status'] == 'UP'
ignore_errors: true
async: "{{ apache_test_retries | default(60) }}"
poll: 1
# Task above will exit as early as possible on success
# It will keep trying for 60 secs, polling every 1 sec
# You need to make sure it's fine **again** because it has ignore_errors: true
- name: Final UP check
local_action:
uri
url=http://{{webapp_url}}/health
timeout=60
register: res
sudo: false
when: updated.changed and apache_test_url is defined
failed_when: res.status != 200 and res['json'] is not defined and not res['json']['status'] == 'UP'
希望它能帮助你解决这个问题,因为在重试中出现了一个bug
我也遇到了同样的问题,并且尝试了一系列不起作用的事情,所以有一段时间我只是在不使用变量的情况下解决了这个问题,但对于每个有这个问题的人来说都找到了答案
Daniels的解决方案确实应该有效:
retries: "{{ apache_test_retries | int }}"
但是,如果您运行的是Ansible的稍旧版本,它将无法工作。因此,请确保您更新了Ansible。我在1.8.4上进行了测试,它在1.8.2上运行,但在1.8.2上没有
这是ansible上的原始错误:
忘了说,如果我写重试:apache\u test\u retries
,我会得到相同的无效的int()文本。
错误。您使用的是什么版本的ansible?变量和过滤器在不同的版本中发生了更改。我使用的是1.8.2.Ah版本,看起来这个问题已经被报告了:不幸的是,同样的问题:ValueError:int()的无效文本,以10为基数:“{apache_test_retries | int}”
如果你看到这个错误,可能是因为你做了类似{{some_-var/10 | int}
将实际解析为{{some_-var/(10 | int)}
所以您需要{{(some_-var/10)| int}
。除此之外,|int
工作得非常好哇,我已经尝试过类似的选项,你的建议看起来很有希望。问题是,异步
选项适用于长时间运行的命令,任务在第一次请求后立即结束。忽略错误
选项似乎与失败冲突ed_当
1时。这次运气不好:(你是对的,太糟糕了……:/GitHub上的#5865 bug补丁可能是最好的解决方案……它已经存在了一段时间,但是被标记为P3我不希望它很快被合并。现在太多的黑客攻击使得一件简单的事情无法完成。当通过多个变量传递int值时,ansible可能会在d之后将该值转换为字符串espite使用|int
。设置修复了这一问题。与OP无关,但与通过搜索web到达这里的人相关。
retries: "{{ apache_test_retries | int }}"