how to get dict text from http.query state?

775 views
Skip to first unread message

YANG LI

unread,
Mar 29, 2017, 8:52:15 PM3/29/17
to Salt-users
I am trying to get token from salt-api with following state:

Request token from API:
  http.query:
    - name: https://salt-master:8080/login
    - match: token
    - method: POST
    - header_list: '["Accept: application/json"]'
    - verify_ssl: False
    - data: 'username=myuser&password=changeme&eauth=pam'
    - text: True

this is output of above state:

server:
----------
          ID: Request token from API
    Function: http.query
        Name: https://salt-master:8080/login
      Result: True
     Comment: Match text "token" was found.
        body: {"return": [{"perms": ["@wheel", "@runner"], "start": 1490833459.1142299, "token": "dfd2cf3d2fef128274c43bdc08380c0a3f2bccd6", "expire": 1490876659.1142311, "user": "myuser", "eauth": "pam"}]}
        status: 200
        text: {"return": [{"perms": ["@wheel", "@runner"], "start": 1490833459.1142299, "token": "dfd2cf3d2fef128274c43bdc08380c0a3f2bccd6", "expire": 1490876659.1142311, "user": "myuser", "eauth": "pam"}]}
     Started: 20:24:18.026066
    Duration: 42.561 ms
     Changes:

Summary for server
------------
Succeeded: 1
Failed:    0

am I able to get text as dictionary from above state run? I tried with following state, but it failed:

echo token:
  cmd.run:
    - name: echo {{text.return[0].token}}


    Data failed to compile:
----------
    Rendering SLS 'base:xxxxx.test1' failed: Jinja variable 'text' is undefined
ERROR: Minions returned with non-zero exit code


Thomas Phipps

unread,
Mar 29, 2017, 9:11:53 PM3/29/17
to salt-...@googlegroups.com
jinja is rendered before states are ran. there is no way for a state to add data to jinja like you are trying to do. 

you need to use the execution module. such as {% set return salt.http.query(<your settings>)%}


--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/fb14bfef-ec3f-48e9-a067-6bac4f596b2b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Thomas Phipps
Linux/Openstack/Logstash

Reply all
Reply to author
Forward
0 new messages