Megazord
Megazord

Reputation: 71

How to get the return code in saltstack state

Do you know how I can parse the data from stdout and searching within it?

check_satellite_registration:
  cmd.run:
    - name: subscription-manager list

For example, see the return data below. How can I evaluate the Status in stdout?

retcode:
  0
stderr:
stdout:
  Status: Subscribed

If the above status is "Subscribed" then continue the execution of the states, otherwise redo the installation:

deploy the http.conf file:
  file.managed:
    - name: /etc/http/conf/http.conf
    - source: salt://apache/http.conf

Upvotes: 1

Views: 518

Answers (1)

OrangeDog
OrangeDog

Reputation: 38777

There are a couple of options. They all involve not using a state for the check, because it doesn't do anything stateful.

deploy the http.conf file:
  file.managed:
    - name: /etc/http/conf/http.conf
    - source: salt://apache/http.conf
    - onlyif:
      - subscription-manager list | grep 'Status: Subscribed'

do something else:
  cmd.run:
    - unless:
      - subscription-manager list | grep 'Status: Subscribed'
{% set status = salt["cmd.run"]("subscription-manager list") %}
{% if "Status: Subscribed" in status %}
deploy the http.conf file:
  file.managed:
    - name: /etc/http/conf/http.conf
    - source: salt://apache/http.conf
{% else %}
do something else:
  cmd.run: []
{% endif %}

Though if your "something else" action is supposed to result in the status becoming subscribed, then you should do it like this instead:

register satellite:
  cmd.run:
    - name: my-register-command
    - unless:
      - subscription-manager list | grep 'Status: Subscribed'

deploy the http.conf file:
  file.managed:
    - name: /etc/http/conf/http.conf
    - source: salt://apache/http.conf
    - require:
      - register satellite

Upvotes: 1

Related Questions