Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SaltStack cmd.run with curl and json body

I have a trivial task that I am stuck to write salt state for. I need to call REST endpoint using curl with json body. This is

curl localhost/endpoint -d '{"some" : "data"}'

My idea was to simply take this and put it into salt state by using cmd.run. Does not work. So far I have this:

{%- set data = {'some': 'data'} %}

Use echo instead of curl:
  cmd.run:
    - name:  echo '{{ data|json }}'

And this gives me

failed: Unknown yaml render error; line 5

Use echo instead of curl:
  cmd.run:
    - name:  echo '{"some": "data"}'    <======================

I have Salt version 2014.7.1

like image 371
Martin Avatar asked Nov 01 '25 02:11

Martin


1 Answers

For me the problem was the ":" within the curl command that was interpreted as YAML (see: How to escape indicator characters (i.e. : or - ) in YAML)

I ended up using the multi-line approach. That allows me to write the command with no escaping while variables (e.g pillar data) are still interpreted correctly.

E.g.

Salt state description:
  cmd.run:
    - name: >-
        curl -X GET  "https://api.example.com/client/{{ pillar['client_id'] }}" -H  "X-Auth-Email: [email protected]" -H "X-Auth-Key: {{ pillar['api_key'] }}" -H "Content-Type: application/json" --data '{"some_json":true}'
like image 67
Heinrich Filter Avatar answered Nov 03 '25 02:11

Heinrich Filter