Referencing a jinja-variable in cmd.run or cmd.shell.

785 views
Skip to first unread message

Tom K.

unread,
Oct 18, 2019, 7:37:35 PM10/18/19
to Salt-users
Scenario:

{% set mylist = salt['pillar.get']('bigitemlist') %}
{% for block in mylist %}

{% set_var = salt['cmd.run']('cat /some/file | grep -Ei {{ mylist[block][item0] }} | grep blah') %}

... do something ...

{% endfor %}


This errors out, however.  It fails to parse {{ mylist[block][item0] }}  .  A few variations that I've tried include:

{% set_var = salt['cmd.run']('cat /some/file | grep -Ei ' + {{ mylist[block][item0] }} + ' | grep blah') %}
{% set_var = salt['cmd.run']('cat /some/file | grep -Ei '{{ mylist[block][item0] }}' | grep blah') %}

None of these work.  This is Salt 2015.5.  How can I properly reference a salt variable from within cmd.run / cmd.shell?

Thx,
TK

Giandomenico Avelluto

unread,
Oct 19, 2019, 4:11:17 AM10/19/19
to Salt-users
Hi Tom,
as you are already in a Jinja statement you don't need to use {{}} to call a variable. Also you're using shell feature such as pipe so you need to add python_shell option.
I that this will work:

{% set_var = salt['cmd.run']('cat /some/file | grep -Ei ' + mylist[block][item0] + ' | grep blah', python_shell=True) %}

Dafydd Jones (techneg.it)

unread,
Oct 19, 2019, 5:52:41 PM10/19/19
to salt-...@googlegroups.com
I believe you will also need to do:

{% set set_var = ... %}

HTH,
Dafydd

--
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/5fc28068-e269-481d-b554-a544b702fb2e%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages