jinja: for-loop over output of cmd.run

3,172 views
Skip to first unread message

pille

unread,
Feb 13, 2012, 10:18:49 AM2/13/12
to salt-...@googlegroups.com
hi,

i'm currently converting our munin-configuration to salt and would like
to create a state like:

{% for netdev in salt['cmd.run']('ls /sys/class/net/') %}
/etc/munin/plugins/if_{{ netdev }}:
file:
- symlink
- target: /usr/share/munin/plugins/if_
{% endfor %}

'ls /sys/class/net/' should give a list if all network devices
what's the correct syntax to make the loop over netdev work?

cheers
pille

Jeff Schroeder

unread,
Feb 13, 2012, 10:23:26 AM2/13/12
to salt-...@googlegroups.com

Without testing from a quick glance that looks like valid jinja2. One
"gotcha" of cmd.run is that it doesn't strip the trailling \n so you
need to do that in the template. Try something along the lines of:
{% for netdev in salt['cmd.run']('ls /sys/class/net/').rstrip() %}


--
Jeff Schroeder

Don't drink and derive, alcohol and analysis don't mix.
http://www.digitalprognosis.com

Thomas S Hatch

unread,
Feb 13, 2012, 11:39:57 AM2/13/12
to salt-...@googlegroups.com
{% for netdev in salt['cmd.run']('ls /sys/class/net/').split() %}
/etc/munin/plugins/if_{{ netdev }}:
  file:
    - symlink
    - target: /usr/share/munin/plugins/if_
{% endfor %}

The cmd.run will return a string, the output from the command, if you want it to be an itterable list then add a .split('\n') with a delimiter

pille+salt+...@struction.de

unread,
Feb 13, 2012, 11:55:02 AM2/13/12
to salt-...@googlegroups.com, Thomas S Hatch
On 02/13/2012 05:39 PM, Thomas S Hatch wrote:
> {% for netdev in salt['cmd.run']('ls /sys/class/net/').split() %}

thanks, that totally does what i wanted.
in the meantime i came up with:
{% for netdev in salt['cmd.run']('ls /sys/class/net/').replace('\n', '') %}

that created what i wanted, but also many one-character items. funny.

thanks
pille

Jason Dusek

unread,
Jul 14, 2015, 3:46:30 PM7/14/15
to salt-...@googlegroups.com, pille+salt+...@struction.de, that...@gmail.com
On Monday, 13 February 2012 08:55:02 UTC-8, pille+salt+...@struction.de wrote:
On 02/13/2012 05:39 PM, Thomas S Hatch wrote:
> {% for netdev in salt['cmd.run']('ls /sys/class/net/').split() %}

thanks, that totally does what i wanted.
in the meantime i came up with:
{% for netdev in salt['cmd.run']('ls /sys/class/net/').replace('\n', '') %}


This would seem to iterate over every character in the string.

Brad Moulder

unread,
Nov 17, 2017, 11:25:08 AM11/17/17
to Salt-users

i am having a similar issue here. in my top file i have states that download and install the package. i later then have a jinja statement that looks at the contents of the installed package. my state files are rendering before everything is in place so im getting an error. 


top.sls

base:
  server:
    - deploy
    - install
    - configure

in configure sls i have 

{%- set subs = salt['cmd.run']('Get-ChildItem -Path C:\package_path | select-object -expandproperty Name | ConvertTo-Json', shell='powershell') | load_json %}
{% for sub in subs %}
ensure {{ sub }} is configured:
  cmd.script:
    - name: 'myscript'
    - shell: 'powershell'
{% endfor %}

my issue is the set subs line is being rendered before the package is in the C:\package_path is populated.

Daniel Wallace

unread,
Nov 17, 2017, 11:26:47 AM11/17/17
to Salt-users
All jinja is rendered first, before any states are run.

If you want to run states first, and then run jinja afterwards, right now you will need to use the state.orchestrate salt runner, and in the first state run make the directories, and in the second state run do the jinja.


--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/salt-users/0ec730fa-d876-4d85-85c6-e5f30d2055aa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages