SaltStack | Как назначить вывод оболочки cmd.run переменной Jinja?

Проблема

Невозможно назначить вывод из cmd.run в моем солевом состоянии. Условие всегда возвращает истину, даже если load_avg в миньоне на самом деле не равно или превышает порог . Я также включил в конфигурацию то, что пробовал.

Конфигурация

# {% 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 %}

Запуск cmd.run в 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 и версия ОС

[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]$
2
задан 23 October 2017 в 20:18
1 ответ

Я думаю, что основная проблема в том, что load_avg из cmd - это не число, а строка, и вам нужно преобразовать ее. Использование load_avg | float (или даже load_avg | int ) как это работает:

{% set load_avg = salt['cmd.shell']('uptime | sed "s/.*load average: //" | cut -d " " -f2 | cut -d . -f1') %}
{% set threshold = 1 %}

check_load_avg:
  cmd.run:
{% if load_avg|float >= 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 %}

Вывод:

----------
          ID: check_load_avg
    Function: cmd.run
        Name: echo 'Load average is HIGH. load_avg=1, threshold=1'
      Result: True
     Comment: Command "echo 'Load average is HIGH. load_avg=1, threshold=1'" run
     Started: 10:19:42.238409
    Duration: 9.731 ms
     Changes:
              ----------
              pid:
                  5976
              retcode:
                  0
              stderr:
              stdout:
                  Load average is HIGH. load_avg=1, threshold=1

С threshold = 5 :

----------
          ID: check_load_avg
    Function: cmd.run
        Name: echo 'Load average is normal. load_avg=1, threshold=5'
      Result: True
     Comment: Command "echo 'Load average is normal. load_avg=1, threshold=5'" run
     Started: 10:20:31.846864
    Duration: 9.361 ms
     Changes:
              ----------
              pid:
                  6184
              retcode:
                  0
              stderr:
              stdout:
                  Load average is normal. load_avg=1, threshold=5

EDIT :

Вы можете получить среднюю нагрузку, используя непосредственно модуль соли status.loadavg :

# salt-call status.loadavg
local:
    ----------
    1-min:
        0.12
    15-min:
        0.31
    5-min:
        0.25

В jinja:

{% set load_avg = salt['status.loadavg']()['1-min'] %}
4
ответ дан 3 December 2019 в 09:57

Теги

Похожие вопросы