Running a shell command with a pipe fails to run within a Jinga if clause. Works okay if not using pipe.

533 views
Skip to first unread message

wayne blair

unread,
Jan 6, 2016, 11:14:45 PM1/6/16
to Salt-users
I'm trying to conditionally defined a variable in a state file.  I want to have one state file that defines a mount point based upon a pattern in the host name.  I do not want to do this test in top.sls then dispatch to different variations of the same .sls file. I'm open to a better solution but I've spent all night trying to find one.

The only solution I've found is to call salt.cmd.retcode within a Jinga conditional. It works except when the shell command contains a pipe. What am I doing wrong or is this a bug?
Also, given the DepricationWarning, what will be the correct way to use cmd.retcode?

I'm running with:
[root@xyzzy-es ~]# salt-call --version
salt-call 2015.8.3 (Beryllium)


This works and resets MOUNTPOINT to /mnt/es. This is a useless example other than showing that it works.

{% set MOUNTPOINT = '/mnt/junk' %}
{% if salt.cmd.retcode('hostname') == 0 %}
{% set MOUNTPOINT = '/mnt/es' %}
{% endif %}


This test fails and does not reset MOUNTPOINT to /mnt/es and looks like it gives me a shell environment dump

{% set MOUNTPOINT = '/mnt/junk' %}
{% if salt.cmd.retcode('hostname|grep -- -es') == 0 %}
{% set MOUNTPOINT = '/mnt/es' %}
{% endif %}

It returns:
[root@xyzzy-es ~]# salt-call --local state.show_sls raid0Dangerous
[INFO    ] Loading fresh modules for state activity
[INFO    ] Fetching file from saltenv 'base', ** done ** u'raid0Dangerous/init.sls'
[WARNING ] /usr/lib/python2.7/site-packages/salt/utils/templates.py:82: DeprecationWarning: Starting in 2015.5, cmd.run uses python_shell=False by default, which doesn't support shellisms (pipes, env variables, etc). cmd.run is currently aliased to cmd.shell to prevent breakage. Please switch to cmd.shell or set python_shell=True to avoid breakage in the future, when this aliasing is removed.

[INFO    ] Executing command 'hostname|grep -- -es' in directory '/root'
[ERROR   ] Rendering exception occurred :Problem running salt function in Jinja template: Unable to run command ['hostname|grep', '--', '-es'] with the context {'with_communicate': True, 'shell': False, 'env': {'LANG': 'en_US.UTF-8', 'TERM': 'xterm-256color', 'SHELL': '/bin/bash', 'MAIL': '/var/spool/mail/root', 'SHLVL': '1', 'HOSTNAME': 'xyzzy-es', 'LS_COLORS': 'rs=0:di=38;5;27:ln=38;5;51:mh=44;38;5;15:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5;232;38;5;11:cd=48;5;232;38;5;3:or=48;5;232;38;5;9:mi=05;48;5;232;38;5;15:su=48;5;196;38;5;15:sg=48;5;11;38;5;16:ca=48;5;196;38;5;226:tw=48;5;10;38;5;16:ow=48;5;10;38;5;21:st=48;5;21;38;5;15:ex=38;5;34:*.tar=38;5;9:*.tgz=38;5;9:*.arc=38;5;9:*.arj=38;5;9:*.taz=38;5;9:*.lha=38;5;9:*.lz4=38;5;9:*.lzh=38;5;9:*.lzma=38;5;9:*.tlz=38;5;9:*.txz=38;5;9:*.tzo=38;5;9:*.t7z=38;5;9:*.zip=38;5;9:*.z=38;5;9:*.Z=38;5;9:*.dz=38;5;9:*.gz=38;5;9:*.lrz=38;5;9:*.lz=38;5;9:*.lzo=38;5;9:*.xz=38;5;9:*.bz2=38;5;9:*.bz=38;5;9:*.tbz=38;5;9:*.tbz2=38;5;9:*.tz=38;5;9:*.deb=38;5;9:*.rpm=38;5;9:*.jar=38;5;9:*.war=38;5;9:*.ear=38;5;9:*.sar=38;5;9:*.rar=38;5;9:*.alz=38;5;9:*.ace=38;5;9:*.zoo=38;5;9:*.cpio=38;5;9:*.7z=38;5;9:*.rz=38;5;9:*.cab=38;5;9:*.jpg=38;5;13:*.jpeg=38;5;13:*.gif=38;5;13:*.bmp=38;5;13:*.pbm=38;5;13:*.pgm=38;5;13:*.ppm=38;5;13:*.tga=38;5;13:*.xbm=38;5;13:*.xpm=38;5;13:*.tif=38;5;13:*.tiff=38;5;13:*.png=38;5;13:*.svg=38;5;13:*.svgz=38;5;13:*.mng=38;5;13:*.pcx=38;5;13:*.mov=38;5;13:*.mpg=38;5;13:*.mpeg=38;5;13:*.m2v=38;5;13:*.mkv=38;5;13:*.webm=38;5;13:*.ogm=38;5;13:*.mp4=38;5;13:*.m4v=38;5;13:*.mp4v=38;5;13:*.vob=38;5;13:*.qt=38;5;13:*.nuv=38;5;13:*.wmv=38;5;13:*.asf=38;5;13:*.rm=38;5;13:*.rmvb=38;5;13:*.flc=38;5;13:*.avi=38;5;13:*.fli=38;5;13:*.flv=38;5;13:*.gl=38;5;13:*.dl=38;5;13:*.xcf=38;5;13:*.xwd=38;5;13:*.yuv=38;5;13:*.cgm=38;5;13:*.emf=38;5;13:*.axv=38;5;13:*.anx=38;5;13:*.ogv=38;5;13:*.ogx=38;5;13:*.aac=38;5;45:*.au=38;5;45:*.flac=38;5;45:*.mid=38;5;45:*.midi=38;5;45:*.mka=38;5;45:*.mp3=38;5;45:*.mpc=38;5;45:*.ogg=38;5;45:*.ra=38;5;45:*.wav=38;5;45:*.axa=38;5;45:*.oga=38;5;45:*.spx=38;5;45:*.xspf=38;5;45:', 'LESSOPEN': '||/usr/bin/lesspipe.sh %s', 'HISTCONTROL': 'ignoredups', 'PWD': '/root', 'LOGNAME': 'root', 'USER': 'root', 'PATH': '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin', 'HOME': '/root', 'HISTSIZE': '1000', 'XDG_SESSION_ID': '43', 'LC_ALL': 'C', '_': '/bin/salt-call'}, 'stdout': -1, 'close_fds': True, 'stdin': None, 'stderr': -2, 'cwd': '/root'}, reason: [Errno 2] No such file or directory; line 5

---
# Define the raid mount point based upon machine naming convention
# of <something>_XY<something>
#
{% set MOUNTPOINT = '/mnt/junk' %}
{% if salt.cmd.retcode('hostname|grep -- - es') == 0 %}    <======================
{% set MOUNTPOINT = '/mnt/es' %}
{% endif %}

raid_packages:
  pkg.installed:
[...]
---
[CRITICAL] Rendering SLS 'base:raid0Dangerous' failed: Problem running salt function in Jinja template: Unable to run command ['hostname|grep', '--',  '-es'] with the context {'with_communicate': True, 'shell': False, 'env': {'LANG': 'en_US.UTF-8', 'TERM': 'xterm-256color', 'SHELL': '/bin/bash', 'MAIL': '/var/spool/mail/root', 'SHLVL': '1', 'HOSTNAME': 'xyzzy-es', 'LS_COLORS': 'rs=0:di=38;5;27:ln=38;5;51:mh=44;38;5;15:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5;232;38;5;11:cd=48;5;232;38;5;3:or=48;5;232;38;5;9:mi=05;48;5;232;38;5;15:su=48;5;196;38;5;15:sg=48;5;11;38;5;16:ca=48;5;196;38;5;226:tw=48;5;10;38;5;16:ow=48;5;10;38;5;21:st=48;5;21;38;5;15:ex=38;5;34:*.tar=38;5;9:*.tgz=38;5;9:*.arc=38;5;9:*.arj=38;5;9:*.taz=38;5;9:*.lha=38;5;9:*.lz4=38;5;9:*.lzh=38;5;9:*.lzma=38;5;9:*.tlz=38;5;9:*.txz=38;5;9:*.tzo=38;5;9:*.t7z=38;5;9:*.zip=38;5;9:*.z=38;5;9:*.Z=38;5;9:*.dz=38;5;9:*.gz=38;5;9:*.lrz=38;5;9:*.lz=38;5;9:*.lzo=38;5;9:*.xz=38;5;9:*.bz2=38;5;9:*.bz=38;5;9:*.tbz=38;5;9:*.tbz2=38;5;9:*.tz=38;5;9:*.deb=38;5;9:*.rpm=38;5;9:*.jar=38;5;9:*.war=38;5;9:*.ear=38;5;9:*.sar=38;5;9:*.rar=38;5;9:*.alz=38;5;9:*.ace=38;5;9:*.zoo=38;5;9:*.cpio=38;5;9:*.7z=38;5;9:*.rz=38;5;9:*.cab=38;5;9:*.jpg=38;5;13:*.jpeg=38;5;13:*.gif=38;5;13:*.bmp=38;5;13:*.pbm=38;5;13:*.pgm=38;5;13:*.ppm=38;5;13:*.tga=38;5;13:*.xbm=38;5;13:*.xpm=38;5;13:*.tif=38;5;13:*.tiff=38;5;13:*.png=38;5;13:*.svg=38;5;13:*.svgz=38;5;13:*.mng=38;5;13:*.pcx=38;5;13:*.mov=38;5;13:*.mpg=38;5;13:*.mpeg=38;5;13:*.m2v=38;5;13:*.mkv=38;5;13:*.webm=38;5;13:*.ogm=38;5;13:*.mp4=38;5;13:*.m4v=38;5;13:*.mp4v=38;5;13:*.vob=38;5;13:*.qt=38;5;13:*.nuv=38;5;13:*.wmv=38;5;13:*.asf=38;5;13:*.rm=38;5;13:*.rmvb=38;5;13:*.flc=38;5;13:*.avi=38;5;13:*.fli=38;5;13:*.flv=38;5;13:*.gl=38;5;13:*.dl=38;5;13:*.xcf=38;5;13:*.xwd=38;5;13:*.yuv=38;5;13:*.cgm=38;5;13:*.emf=38;5;13:*.axv=38;5;13:*.anx=38;5;13:*.ogv=38;5;13:*.ogx=38;5;13:*.aac=38;5;45:*.au=38;5;45:*.flac=38;5;45:*.mid=38;5;45:*.midi=38;5;45:*.mka=38;5;45:*.mp3=38;5;45:*.mpc=38;5;45:*.ogg=38;5;45:*.ra=38;5;45:*.wav=38;5;45:*.axa=38;5;45:*.oga=38;5;45:*.spx=38;5;45:*.xspf=38;5;45:', 'LESSOPEN': '||/usr/bin/lesspipe.sh %s', 'HISTCONTROL': 'ignoredups', 'PWD': '/root', 'LOGNAME': 'root', 'USER': 'root', 'PATH': '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin', 'HOME': '/root', 'HISTSIZE': '1000', 'XDG_SESSION_ID': '43', 'LC_ALL': 'C', '_': '/bin/salt-call'}, 'stdout': -1, 'close_fds': True, 'stdin': None, 'stderr': -2, 'cwd': '/root'}, reason: [Errno 2] No such file or directory; line 5

---
# Define the raid mount point based upon machine naming convention
# of <something>_XY<something>
#
{% set MOUNTPOINT = '/mnt/junk' %}
{% if salt.cmd.retcode('hostname|grep -- -es') == 0 %}    <======================
{% set MOUNTPOINT = '/mnt/es' %}
{% endif %}

raid_packages:
  pkg.installed:
[...]
---
local:
    - Rendering SLS 'base:raid0Dangerous' failed: Problem running salt function in Jinja template: Unable to run command ['hostname|grep', '--', 'es'] with the context {'with_communicate': True, 'shell': False, 'env': {'LANG': 'en_US.UTF-8', 'TERM': 'xterm-256color', 'SHELL': '/bin/bash', 'MAIL': '/var/spool/mail/root', 'SHLVL': '1', 'HOSTNAME': 'xyzzy-es', 'LS_COLORS': 'rs=0:di=38;5;27:ln=38;5;51:mh=44;38;5;15:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5;232;38;5;11:cd=48;5;232;38;5;3:or=48;5;232;38;5;9:mi=05;48;5;232;38;5;15:su=48;5;196;38;5;15:sg=48;5;11;38;5;16:ca=48;5;196;38;5;226:tw=48;5;10;38;5;16:ow=48;5;10;38;5;21:st=48;5;21;38;5;15:ex=38;5;34:*.tar=38;5;9:*.tgz=38;5;9:*.arc=38;5;9:*.arj=38;5;9:*.taz=38;5;9:*.lha=38;5;9:*.lz4=38;5;9:*.lzh=38;5;9:*.lzma=38;5;9:*.tlz=38;5;9:*.txz=38;5;9:*.tzo=38;5;9:*.t7z=38;5;9:*.zip=38;5;9:*.z=38;5;9:*.Z=38;5;9:*.dz=38;5;9:*.gz=38;5;9:*.lrz=38;5;9:*.lz=38;5;9:*.lzo=38;5;9:*.xz=38;5;9:*.bz2=38;5;9:*.bz=38;5;9:*.tbz=38;5;9:*.tbz2=38;5;9:*.tz=38;5;9:*.deb=38;5;9:*.rpm=38;5;9:*.jar=38;5;9:*.war=38;5;9:*.ear=38;5;9:*.sar=38;5;9:*.rar=38;5;9:*.alz=38;5;9:*.ace=38;5;9:*.zoo=38;5;9:*.cpio=38;5;9:*.7z=38;5;9:*.rz=38;5;9:*.cab=38;5;9:*.jpg=38;5;13:*.jpeg=38;5;13:*.gif=38;5;13:*.bmp=38;5;13:*.pbm=38;5;13:*.pgm=38;5;13:*.ppm=38;5;13:*.tga=38;5;13:*.xbm=38;5;13:*.xpm=38;5;13:*.tif=38;5;13:*.tiff=38;5;13:*.png=38;5;13:*.svg=38;5;13:*.svgz=38;5;13:*.mng=38;5;13:*.pcx=38;5;13:*.mov=38;5;13:*.mpg=38;5;13:*.mpeg=38;5;13:*.m2v=38;5;13:*.mkv=38;5;13:*.webm=38;5;13:*.ogm=38;5;13:*.mp4=38;5;13:*.m4v=38;5;13:*.mp4v=38;5;13:*.vob=38;5;13:*.qt=38;5;13:*.nuv=38;5;13:*.wmv=38;5;13:*.asf=38;5;13:*.rm=38;5;13:*.rmvb=38;5;13:*.flc=38;5;13:*.avi=38;5;13:*.fli=38;5;13:*.flv=38;5;13:*.gl=38;5;13:*.dl=38;5;13:*.xcf=38;5;13:*.xwd=38;5;13:*.yuv=38;5;13:*.cgm=38;5;13:*.emf=38;5;13:*.axv=38;5;13:*.anx=38;5;13:*.ogv=38;5;13:*.ogx=38;5;13:*.aac=38;5;45:*.au=38;5;45:*.flac=38;5;45:*.mid=38;5;45:*.midi=38;5;45:*.mka=38;5;45:*.mp3=38;5;45:*.mpc=38;5;45:*.ogg=38;5;45:*.ra=38;5;45:*.wav=38;5;45:*.axa=38;5;45:*.oga=38;5;45:*.spx=38;5;45:*.xspf=38;5;45:', 'LESSOPEN': '||/usr/bin/lesspipe.sh %s', 'HISTCONTROL': 'ignoredups', 'PWD': '/root', 'LOGNAME': 'root', 'USER': 'root', 'PATH': '/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin', 'HOME': '/root', 'HISTSIZE': '1000', 'XDG_SESSION_ID': '43', 'LC_ALL': 'C', '_': '/bin/salt-call'}, 'stdout': -1, 'close_fds': True, 'stdin': None, 'stderr': -2, 'cwd': '/root'}, reason: [Errno 2] No such file or directory; line 5
    - 
    - ---
    - # Define the raid mount point based upon machine naming convention
    - # of <something>_XY<something>
    - #
    - {% set MOUNTPOINT = '/mnt/junk' %}
    - {% if salt.cmd.retcode('hostname|grep -- -es') == 0 %}    <======================
    - {% set MOUNTPOINT = '/mnt/es' %}
    - {% endif %}
    - 
    - raid_packages:
    -   pkg.installed:
    - [...]
    - ---

Florian Ermisch

unread,
Jan 7, 2016, 1:29:54 AM1/7/16
to salt-...@googlegroups.com
Hi Wayne,

why don't you use the "hostname" grain instead? `{% if grains['hostname'].endswith('-es') %} ` or something like that.

If you still want to run a command containing a pipe try setting `shell=True` like the deprecation warning suggests ;)

Regards, Florian

Sandy

unread,
Jan 7, 2016, 9:57:31 AM1/7/16
to Salt-users
Or maybe "{% if grains['fqdn'].find('-es') != -1 %}" if the substring can be anywhere.

Side note - I cannot find documentation on it, but is seems that within jinja one has access to all (TBD?) python string methods:


In any case the above seems to work.

wayne blair

unread,
Jan 7, 2016, 10:38:52 AM1/7/16
to Salt-users
Thanks to both of you.  Sandy's suggestion does what I needs for now given current naming conventions!


On Wednesday, January 6, 2016 at 11:14:45 PM UTC-5, wayne blair wrote:

Colton Myers

unread,
Jan 12, 2016, 7:46:13 PM1/12/16
to salt-...@googlegroups.com
By default in salt, we set python_shell=False for most cmd.run and similar functions, to prevent shell injection. With this setting, shellisms such as pipes will not work. There are a couple of exceptions: when you use cmd.run on the command line, since you can already execute arbitrary commands we default to python_shell=True. The same is also true in jinja, but only for cmd.run, we didn't override all of them, which is why it's not working for you.

If you need to use pipes in jinja in the future, use cmd.run, or just set python_shell=True in the cmd.retcode call.

--
Colton Myers
Core Engineer, SaltStack
@basepi on Twitter/Github/IRC

Registration for SaltConf 2016 is open! http://saltconf.com/register/

--
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.

Reply all
Reply to author
Forward
0 new messages