问题描述
此角色尝试从树莓派计算机(仅在这种情况下为 host1)获取 cpu 温度,检查 docker 容器 boinc_universeathome 是否正在运行,然后如果容器正在运行并且 cpu temp > highThresholdTrigger 那么它应该暂停容器.
This role attempts to get cpu temperatures from raspberry pi computers (host1 only in this case), check whether docker container boinc_universeathome is running, and then if the container is running and the cpu temp > highThresholdTrigger then it should pause the container.
如果我在角色中没有 when 子句的情况下运行它,那么它总是会正确地暂停容器.
If i run this without the when clauses in the role, then correctly it always pauses the container.
为什么当存在 when 子句时它不起作用?我希望在存在 when 子句的情况下,它应该只在满足条件时暂停容器.但条件永远不会满足.
Why does it not work when the when clauses are present ever? i expect that with the when clauses present, it should only pause the container when the condition is met. But the condition is never met.
大概是因为我写 when 子句的方式有问题,但我不知道哪里出了问题?
Presumably this is because there is something wrong with the way i am writing the when clauses but i dont know what it wrong?
(我知道我在这里使用的是 shell 而不是 docker 模块)
(I know i'm using shell here and not a docker module)
我的主机:
[monitorrpihosts]
host1
[monitorrpihosts:vars]
ansible_connection=ssh
ansible_ssh_user=someusername
highThresholdTrigger=70
highThresholdReset=40
我的剧本:
---
- name: Monitor rpi
hosts: monitorrpihosts
become: yes
become_user: root
become_method: sudo
roles:
- monitorrpi
我的角色:
---
- name: Get current cpu temp
shell: '/opt/vc/bin/vcgencmd measure_temp | grep ^temp= | cut -d= -f2 | cut -d\. -f1'
register: cpu_temp
- name: check if boinc is running
shell: 'docker ps | grep boinc_universeathome | grep -v Paused'
ignore_errors: True
register: boinc_running
- name: pause boinc if cpu temp gt highThreshold
shell: 'docker pause boinc_universeathome'
when:
- cpu_temp.stdout_lines|int > highThresholdTrigger|int
- boinc_running.rc|int == 0
推荐答案
cpu_temp.stdout_lines
是一个行列表.尝试使用 cpu_temp.stdout|int
.
cpu_temp.stdout_lines
is a list of lines. Try with cpu_temp.stdout|int
.
这篇关于ansible 比较整数值时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!