How to set salt cmd.run changed state based on stderr

Viewed 25

In ansible you can register a variable to capture the output of running a command, specify a changed_when argument, and use that variable to determine if the state changed:

- name: Combine multiple conditions to override 'changed' result
  ansible.builtin.command: /bin/fake_command
  register: result
  ignore_errors: True
  changed_when:
    - '"ERROR" in result.stderr'
    - result.rc == 2

In salt I found that there is a stateful argument to cmd.run but the output appears that it needs to be in a specific json or key=value pair format to do the equivalent. In salt is there a way to do the equivalent of the ansible example to search for the existence of a specific string in the output to determine if the state changed?

1 Answers

You'd need to write a wrapper that returns the stateful-compatible response.

do the thing:
  cmd.run:
    - name: '/bin/fake_command 2>&1 | grep "ERROR" && echo "changed=true" || true'
    - stateful: true

Though outputting "ERROR" seems an odd way for this command to indicate successful changes.

Alternatively, you can write an entirely custom state for more complex logic.

Related