Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ansible when comparing integer values

Tags:

ansible

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.

If i run this without the when clauses in the role, then correctly it always pauses the container.

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.

Presumably this is because there is something wrong with the way i am writing the when clauses but i dont know what it wrong?

(I know i'm using shell here and not a docker module)

My hosts:

[monitorrpihosts]
host1

[monitorrpihosts:vars]
ansible_connection=ssh
ansible_ssh_user=someusername
highThresholdTrigger=70
highThresholdReset=40

My playbook:

---
- name: Monitor rpi
  hosts: monitorrpihosts
  become: yes
  become_user: root
  become_method: sudo

  roles:
   - monitorrpi

My role:

---
- 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
like image 444
skymoose Avatar asked Nov 13 '18 15:11

skymoose


1 Answers

cpu_temp.stdout_lines is a list of lines. Try with cpu_temp.stdout|int.

like image 88
Konstantin Suvorov Avatar answered Sep 29 '22 10:09

Konstantin Suvorov