How can i execute a shell script and assign the output of the shell script to a variable inside my salt scripts ? Iam using salt minion and need to get output of a bash script to a salt variable

7,603 views
Skip to first unread message

Melvil JA

unread,
Oct 25, 2015, 10:20:00 AM10/25/15
to Salt-users
How can i execute a shell script and assign the output of the shell script to a variable inside my salt scripts ? Iam using salt minion and need to get output of a bash script to a salt variable 

Loren Gordon

unread,
Oct 25, 2015, 11:22:41 AM10/25/15
to Salt-users
Use jinja to execute the salt module 'cmd.run' and assign the output to a variable:

{% set foo = salt['cmd.run']('/path/to/script') %}

Then you can reference the contents of foo as a jinja variable: {{ foo }}

-Loren

Melvil JA

unread,
Oct 26, 2015, 4:37:53 AM10/26/15
to salt-...@googlegroups.com
perfect..than you

--
You received this message because you are subscribed to a topic in the Google Groups "Salt-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/salt-users/IJo6Z8Hro2w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to salt-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Thanks 

_____________________________________________________________
The information contained in this communication is intended solely for the use of the individual or entity to whom it is addressed and others authorized to receive it. It may contain confidential or legally privileged information. If you are not the intended recipient you are hereby notified that any disclosure, copying, distribution or taking any action in reliance on the contents of this information is strictly prohibited and may be unlawful. If you have received this communication in error, please notify us immediately by responding to this email and then delete it from your system. The firm is neither liable for the proper and complete transmission of the information contained in this communication nor for any delay in its receipt.

Melvil JA

unread,
Nov 21, 2015, 1:32:38 PM11/21/15
to salt-...@googlegroups.com
Hello Loren,iam getting this error 

Unable to manage file: Jinja error: argument of type 'StrictUndefined' is not iterabl

While running the salt call.

Iam using salt minion. if i rerun it again ,it will dissapear.

Melvil JA

unread,
Nov 21, 2015, 1:34:26 PM11/21/15
to salt-...@googlegroups.com

  ID: /opt/inmob/conf/cosmos-config/1.0.6/uat.py

    Function: file.managed

      Result: False

     Comment: Unable to manage file: Jinja error: argument of type 'StrictUndefined' is not iterable

              Traceback (most recent call last):

                File "/usr/lib/python2.7/dist-packages/salt/utils/templates.py", line 306, in render_jinja_tmpl

                  output = template.render(**decoded_context)

                File "/usr/lib/python2.7/dist-packages/jinja2/environment.py", line 894, in render

                  return self.environment.handle_exception(exc_info, True)

                File "<template>", line 1, in top-level template code

                File "/usr/lib/python2.7/dist-packages/salt/utils/jinja.py", line 105, in get_source

                  if '..' in template:

              TypeError: argument of type 'StrictUndefined' is not iterable

              

              ; line 1

              

              ---

              {% import_yaml pillar['cosmos-settings'] as cosmos_settings %}    <======================

              {% set colo = salt['environ.get']('IDP_CLUSTER') %}

              {% set environment = salt['environ.get']("IDP_ENVIRONMENT") %}

              {% set ENV = salt['environ.get']%}

              {% set cosmos = cosmos_settings[environment][colo]["cosmos"]["app"] %}

              {% set hostname = salt['cmd.run']('hostname') %}

              [...]

              ---

     Started: 18:30:08.330336

    Duration: 178.846 ms

     Changes:   


this is the full erro i am getting. Can you have a look ?

Loren Gordon

unread,
Nov 21, 2015, 1:45:52 PM11/21/15
to Salt-users
`import_yaml` works on a file, not pillar. If you just want to retrieve a value from pillar, you would use `set`. i.e.:

{% set cosmos_settings = pillar['cosmos-settings'] %}

-Loren

Melvil JA

unread,
Nov 26, 2015, 5:04:03 AM11/26/15
to salt-...@googlegroups.com
infact iam passing a json file that is in salt:// directory and calling the salt-minons like

sudo -E  /usr/bin/salt-call  --file-root=/srv/salt/cosmos-django  state.highstate -l debug --local pillar='{"cosmos-config":"cosmos-config.json","cosmos-settings":"cosmos-settings.json"}'


is there anything wrong ?


--
You received this message because you are subscribed to a topic in the Google Groups "Salt-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/salt-users/IJo6Z8Hro2w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to salt-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Thanks 

Loren Gordon

unread,
Nov 26, 2015, 8:03:54 AM11/26/15
to Salt-users
Well, if you're still using `import_yaml` in the state to try to reference those pillar values, then that still won't work. The command looks fine to me; though I should say that I haven't yet had a cause to pass in pillar values through the command line. I'd expect you should still be able to reference them with the `set` command I sent previously. In this case, `pillar['cosmos-settings']` would evaluate to `'cosmos-settings.json'`.

-Loren

Melvil JA

unread,
Nov 26, 2015, 8:37:00 AM11/26/15
to salt-...@googlegroups.com
ok fine.Coulf you tell me how to pass the pillar "cosmos-settings" to a jinja template

i have template definitions like 

{{conf_dir}}/{{config}}/{{configs[config]["version"]}}/{{environment}}.py:

   file:

    - managed

    - source: {{conf_dir}}/{{config}}/{{configs[config]["version"]}}/active.py

    - template: jinja

    - user: root

    - group: root

    - mode: 644

My commands is 

sudo -E  /usr/bin/salt-call  --file-root=/srv/salt/cosmos-django  state.highstate -l debug --local pillar='{"cosmos-config":"cosmos-config.json","cosmos-settings":"cosmos-settings.json"}'



Inside   {{conf_dir}}/{{config}}/{{configs[config]["version"]}}/active.py file how should i reference to the cosmos-settings.json .


Myobservations is it is failing for first time installtion and then succeesding.

this is the error iam getting for first time.

 File "/usr/lib/python2.7/dist-packages/salt/states/file.py", line 1459, in managed

    **kwargs

  File "/usr/lib/python2.7/dist-packages/salt/modules/file.py", line 2841, in get_managed

    **kwargs)

  File "/usr/lib/python2.7/dist-packages/salt/utils/templates.py", line 121, in render_tmpl

    output = render_str(tmplstr, context, tmplpath)

  File "/usr/lib/python2.7/dist-packages/salt/utils/templates.py", line 324, in render_jinja_tmpl

    buf=tmplstr)

SaltRenderError: Jinja variable 'dict object' has no attribute 'cosmos-settings'


Could you have a look into it ?

Loren Gordon

unread,
Nov 26, 2015, 8:48:45 AM11/26/15
to Salt-users
I'm pretty sure you can reference pillar from a templated file. So inside the templated file, you would use `{{ pillar['cosmos-settings'] }}` where you want to insert that value. If that's not working, then pillar isn't being set properly, which could be a shell interpretation issue passing the value to `pillar=` on the command line.

-Loren

Melvil JA

unread,
Nov 26, 2015, 8:55:00 AM11/26/15
to salt-...@googlegroups.com
my directory strcuture is 

/srv/salt/cosmos-django/cosmos-django/init.sls.

iam setting the files root in command

sudo -E  /usr/bin/salt-call  --file-root=/srv/salt/cosmos-django  state.highstateighstate -l debug --local pillar='{"cosmos-config":"cosmos-config.json","cosmos-settings":"cosmos-settings.json"}'

My pilaar json is at 

 /srv/salt/cosmos-django/cosmos-settings.json

Is that all rite ?


Loren Gordon

unread,
Nov 26, 2015, 9:00:13 AM11/26/15
to Salt-users
If you want to use `state.highstate` on the command line, you'll also need the top.sls targeting file, `/srv/salt/cosmos-django/top.sls`, with these contents:

```
base:
  '*'
    - cosmos-django
```

Alternatively, you could change `state.highstate` to `state.sls cosmos-django`.

-Loren

Melvil JA

unread,
Nov 26, 2015, 9:02:37 AM11/26/15
to salt-...@googlegroups.com
yeah i have it.
everything is working fine except for a fresh deployment.
After failing for first time..it will succeed in all deployments. As i told u iam getting the below error for first time.
any idea what is happening ?

File "/usr/lib/python2.7/dist-packages/salt/states/file.py", line 1459, in managed

    **kwargs

  File "/usr/lib/python2.7/dist-packages/salt/modules/file.py", line 2841, in get_managed

    **kwargs)

  File "/usr/lib/python2.7/dist-packages/salt/utils/templates.py", line 121, in render_tmpl

    output = render_str(tmplstr, context, tmplpath)

  File "/usr/lib/python2.7/dist-packages/salt/utils/templates.py", line 324, in render_jinja_tmpl

    buf=tmplstr)

SaltRenderError: Jinja variable 'dict object' has no attribute 'cosmos-settings'

Melvil JA

unread,
Nov 26, 2015, 9:04:28 AM11/26/15
to salt-...@googlegroups.com
what i can understood is if i delete /var/cache/salt..then it will fail for one time

Melvil JA

unread,
Nov 26, 2015, 9:12:41 AM11/26/15
to salt-...@googlegroups.com
one clarification. my cosmos-settings.json is simply a json file in the salt root.

Loren Gordon

unread,
Nov 26, 2015, 9:41:06 AM11/26/15
to Salt-users
I saw that error message. Typically, that means the pillar value is not present, which is why I mentioned the shell interpreter possibly mangling the value. I'm not sure why you would get the error on the first run and not on any others. If you wanted to test the idea that the pillar value is not present, you could use this syntax in the templated file, in place of {{ pillar['cosmos-settings'] }}:

{{ salt['pillar.get']('cosmos-settings', 'fake.json') }}

This syntax will return the string 'fake.json' if the key 'cosmos-settings' is not present in pillar. But again, I don't know why it would fail on the first run and then succeed later.

-Loren

Loren Gordon

unread,
Nov 26, 2015, 9:54:14 AM11/26/15
to Salt-users
A possible workaround, presuming there's some underlying bug, would be to use the `--pillar-root` command line option instead of `--pillar`, maybe `--pillar-root=/srv/salt/cosmos-django/pillar` and in that directory place two files:

top.sls:
```
base:
  '*'
    - cosmos-django
```

cosmos-django.sls:
```
'cosmos-config': 'cosmos-config.json'
'cosmos-settings': 'cosmos-settings.json'
```

-Loren

Melvil JA

unread,
Nov 26, 2015, 11:22:45 AM11/26/15
to salt-...@googlegroups.com, Rachana K.C.
yeah its a perfecto !
thanks a lot Loren.
Really really appreciate...!

Loren Gordon

unread,
Nov 26, 2015, 1:14:55 PM11/26/15
to Salt-users, rach...@inmobi.com
Cool, glad we found something that works for you! :)

Cheers,
-Loren

Melvil JA

unread,
Nov 30, 2015, 12:07:25 PM11/30/15
to salt-...@googlegroups.com, Rachana K.C.
Loren suppose one of my state failed in salt.
Why echo $? is showing zero for that also ?

Loren Gordon

unread,
Nov 30, 2015, 12:15:24 PM11/30/15
to Salt-users, rach...@inmobi.com
Yep, salt at first feels a little odd in that respect. A failure in a state is not treated as a failure of the application, so the salt-call exit code is '0' even if a state fails. Thankfully, there's a command-line switch that will cause it to work the way you're expecting: `--retcode-passthrough`. Just append that to your command.

Reply all
Reply to author
Forward
0 new messages