Assigning output of a curl command as a Jinja variable?

2,740 views
Skip to first unread message

Varunn M

unread,
Jun 27, 2016, 8:56:57 AM6/27/16
to Salt-users
Hi,

Can someone please shed some light on the issue below.
I am creating a salt state in which I want to set a jinja variable which is the output of a cmd.run command.
A snippet of my code looks like this:


{%- set interface = 'eth0' -%}
{%- set ipaddress= salt['network.interface_ip'](interface) -%}
{%- set var1= salt['cmd.run']("curl http://{{ ipaddress }}/some/url/path") -%}

get-variable:
  cmd
:
   
- run
   
- name: 'curl -X POST http://{{ ipaddress }}/somepath/ -d create=yes&variable={{ var1 }}'


Here the variable ipaddress is resolved but the variable var1 is not.
Also, when running this command from master, I am getting the desired result.

salt minion cmd.run 'curl -sL http://x.y.z.a/some/url/path'
minion
:
    a12bcd3e
-1234-1aa1-qwer-12a345b567c8

Any inputs?

Cheers,
V

Onno

unread,
Jun 27, 2016, 9:03:09 AM6/27/16
to salt-...@googlegroups.com
{%- set var1= salt['cmd.run']("curl http://" ~ {{ ipaddress }} ~
"/some/url/path") -%}

or (what I like);

{%- set var1 = salt.cmd.run('curl http://' ~ {{ ipaddress }} ~
'/some/url/path') %}


Varunn M schreef op 2016-06-27 14:56:
> --
> 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.
> For more options, visit https://groups.google.com/d/optout [1].
>
>
> Links:
> ------
> [1] https://groups.google.com/d/optout

Varunn M

unread,
Jun 27, 2016, 9:41:32 AM6/27/16
to Salt-users, on...@xqi.nl
Hi Onno,

Thanks for the prompt response,one thing I missed is post the ipaddress I am also entering a port and I am still getting an error:

minion1:
    Data failed to compile:
----------
    Rendering SLS 'base:state' failed: Jinja syntax error: expected token 'colon', got '}'; line 4

---
{%- set interface = 'eth0' -%}
{%- set addr = salt['network.interface_ip'](interface) -%}

{%- set var1 = salt.cmd.run('curl http://' ~ {{ ipaddress }} ~ ':8080/some/path') %}    <======================

Cheers,
V

Onno

unread,
Jun 27, 2016, 9:58:57 AM6/27/16
to Varunn M, Salt-users
Shame on me...

{%- set var1 = salt.cmd.run('curl http://' ~ ipaddress ~
':8080/some/path') %}

Varunn M schreef op 2016-06-27 15:41:
>>> salt minion cmd.run 'curl -sL http://x.y.z.a/some/url/path [1]'
>>> minion:
>>>     a12bcd3e-1234-1aa1-qwer-12a345b567c8
>>>
>>> Any inputs?
>>>
>>> Cheers,
>>> V
>>>
>>>  --
>>>  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.
>>>  For more options, visit https://groups.google.com/d/optout [2]
>> [1].
>>>
>>>
>>> Links:
>>> ------
>>> [1] https://groups.google.com/d/optout [2]
>
>
> Links:
> ------
> [1] http://x.y.z.a/some/url/path
> [2] https://groups.google.com/d/optout

Varunn M

unread,
Jun 27, 2016, 10:22:41 AM6/27/16
to Salt-users, mail...@gmail.com, on...@xqi.nl
@Onno, anything else I can try?

Thomas Phipps

unread,
Jun 27, 2016, 10:26:23 AM6/27/16
to Salt-users, mail...@gmail.com, on...@xqi.nl
{%- set var1= salt.http.query("http://" ~ ipaddress ~ "/some/url/path")['body'] -%}

For more options, visit https://groups.google.com/d/optout.
--
Thomas Phipps
Linux/Openstack/Logstash

Varunn M

unread,
Jun 28, 2016, 4:53:08 AM6/28/16
to Salt-users
Thanks @Onno,
This works perfectly: {%- set var1 = salt.cmd.run('curl http://' ~ ipaddress ~ ':8080/some/path') %} 

@Thomas : Have noted the option you gave too. Many Thanks.

Cheers,
Varun
Reply all
Reply to author
Forward
0 new messages