Problem
Unable to assign the output from cmd.run
in my Salt State. The condition always return true even if the load_avg
in the minion is not really equal or beyond the threshold
. I also included in the configuration the things I have tried.
Configuration
# {% set load_avg = salt['cmd.run']('uptime | sed "s/.*load average: //" | cut -d " " -f2 | cut -d . -f1') %} # Not working
# {% set load_avg = salt['cmd.run']('/bin/sh -c "uptime | sed \"s/.*load average: //\" | cut -d \" \" -f2 | cut -d . -f1"') %} # Not working
# {% set load_avg = salt['cmd.run']('echo 0') %} # Not working
# {% set load_avg = salt['cmd.shell']('uptime | sed "s/.*load average: //" | cut -d " " -f2 | cut -d . -f1') %} # Not working
# {% set load_avg = 0 %} # Working. Output: Load average is normal message
{% set load_avg = 6 %} # Working: Output: Load average is HIGH message
{% set threshold = 5 %}
check_load_avg:
cmd.run:
{% if load_avg >= threshold %}
- name: echo 'Load average is HIGH. load_avg={{ load_avg }}, threshold={{ threshold }}'
{% else %}
- name: echo 'Load average is normal. load_avg={{ load_avg }}, threshold={{ threshold }}'
{% endif %}
Running cmd.run
in CLI
[ec2-user@ip-10-0-1-48 hello]$ sudo salt '*' cmd.run 'uptime | sed "s/.*load average: //" | cut -d " " -f1 | cut -d . -f1'
ip-10-0-1-48.ec2.internal:
0
[ec2-user@ip-10-0-1-48 hello]$ sudo salt '*' cmd.run 'uptime | sed "s/.*load average: //" | cut -d " " -f1 | cut -d . -f1'
ip-10-0-1-48.ec2.internal:
4
[ec2-user@ip-10-0-1-48 hello]$
Salt and OS Version
[ec2-user@ip-10-0-1-48 hello]$ salt --version
salt 2017.7.2 (Nitrogen)
[ec2-user@ip-10-0-1-48 hello]$ uname -a
Linux ip-10-0-1-48 4.9.51-10.52.amzn1.x86_64 #1 SMP Fri Sep 29 01:16:19 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[ec2-user@ip-10-0-1-48 hello]$