use variable in if statement

4,758 views
Skip to first unread message

Brian

unread,
Feb 18, 2014, 3:13:43 AM2/18/14
to salt-...@googlegroups.com
Hi,

This thing has been bugging for quite sometime. I am not able to use a variable in my if statement in a state file. if statement can't seem to see the value of the variable '{{ syslog_server }}'

Example:

## syslog.sls ##

{% set syslog_server = '<minion id>' %}

{% if grains['id'] == '{{ syslog_server }}' %}

rsyslog:
  pkg.latest:
    - skip_verify: True
  service.running:
    - enable: True

{% else %}

syslog-ng:
  pkg.latest:
    - skip_verify: True
  service.running:
    - enable: True

{% endif %}



Raul Hormazabal

unread,
Feb 18, 2014, 3:28:54 AM2/18/14
to salt-...@googlegroups.com
Did you try this?

{% if grains['id'] == syslog_server %}

--
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/groups/opt_out.

Vlad

unread,
Feb 26, 2014, 8:57:06 AM2/26/14
to salt-...@googlegroups.com
I have tried different approaches, with {{ app_name }} and 'app_name' ....
not working example:

test.sls
{% set app_name = 'ad-center-tools' %}
{% if salt['cmd.run']("ls -lt /home/app_name-user/app_name 2>/dev/null| wc -l") != "0" %}

output:
 cmd.run:
     - names:
       - echo "Rollback directory {{ app_name }}"
     - cwd: /root

{% else %}

error_output:
 cmd.run:
     - names:
       - echo "This is the last directory. Can't remove {{ app_name }}"
     - cwd: /root

{% endif  %}




вторник, 18 февраля 2014 г., 3:28:54 UTC-5 пользователь Raul Hormazabal написал:

Sean Coates

unread,
Feb 26, 2014, 9:02:20 AM2/26/14
to salt-...@googlegroups.com
test.sls
{% set app_name = 'ad-center-tools' %}
{% if salt['cmd.run']("ls -lt /home/app_name-user/app_name 2>/dev/null| wc -l") != "0" %}


If I understand correctly, you're expecting something like this, there:
{% if salt['cmd.run']("ls -lt /home/ad-center-tools-user/ad-center-tools 2>/dev/null| wc -l") != "0" %}

If that's the case, you could try something like this (Jinja concatenation):

{% if salt['cmd.run']("ls -lt /home/" ~ app_name ~ "-user/" ~ app_name ~ " 2>/dev/null| wc -l") != "0" %}

The `~` operator is documented here: http://jinja.pocoo.org/docs/templates/#other-operators

S

David Anderson

unread,
Feb 26, 2014, 11:32:35 AM2/26/14
to salt-...@googlegroups.com
There is no 'names' arg for cmd.run. Change to 'name':

...
output:
cmd.run:
- name: echo "Rollback directory {{ app_name }}"
- cwd: /root
...
error_output:
cmd.run:
- name: "This is the last directory. Can't remove {{ app_name }}"
- cwd: /root
...

--
Dave


On 2/26/14, 6:57 AM, Vlad wrote:
> I have tried different approaches, with {{ app_name }} and 'app_name'
> ....
> not working example:
>
> test.sls
> {% set app_name = 'ad-center-tools' %}
> {% if salt['cmd.run']("ls -lt /home/app_name-user/app_name
> 2>/dev/null| wc -l") != "0" %}
>
> output:
> cmd.run:
> - names:
> - echo "Rollback directory {{ app_name }}"
> - cwd: /root
>
> {% else %}
>
> error_output:
> cmd.run:
> - names:
> - echo "This is the last directory. Can't remove {{ app_name }}"
> - cwd: /root
>
> {% endif %}
>
>
>
>
> вторник, 18 февраля 2014 г., 3:28:54 UTC-5 пользователь Raul
> Hormazabal написал:
>
> Did you try this?
>
> /{% if grains['id'] == *syslog_server* %}/
>
>
> On Tue, Feb 18, 2014 at 7:13 PM, Brian <lim.bri...@gmail.com
> <javascript:>> wrote:
>
> Hi,
>
> This thing has been bugging for quite sometime. I am not able
> to use a variable in my if statement in a state file. if
> statement can't seem to see the value of the variable '{{
> syslog_server }}'
>
> Example:
>
> /## syslog.sls ##/
> /
> /
> /{% set syslog_server = '<minion id>' %}/
> /
> /
> /{% if grains['id'] == '*{{ syslog_server }}*' %}
> /
> /
> /
> /rsyslog:/
> / pkg.latest:/
> / - skip_verify: True/
> / service.running:/
> / - enable: True/
> /
> /
> /{% else %}/
> /
> /
> /syslog-ng:/
> / pkg.latest:/
> / - skip_verify: True/
> / service.running:/
> / - enable: True/
> /
> /
> /{% endif %}/
>
>
>
> --
> 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
> <javascript:>.
> For more options, visit
> https://groups.google.com/groups/opt_out
> <https://groups.google.com/groups/opt_out>.

Vlad

unread,
Feb 27, 2014, 8:49:56 AM2/27/14
to salt-...@googlegroups.com
It works :)
I am curious to know Where you have found answer?
I just want to understand salt in deep.
Thanks!


среда, 26 февраля 2014 г., 9:02:20 UTC-5 пользователь Sean Coates написал:

Sam Lai

unread,
Feb 27, 2014, 5:07:54 PM2/27/14
to salt-...@googlegroups.com
On 27 February 2014 03:32, David Anderson <da...@dubkat.com> wrote:
> There is no 'names' arg for cmd.run. Change to 'name':

Actually, there is -
http://docs.saltstack.com/ref/states/highstate.html#term-names-declaration

It is a shortcut to apply the same state definition to multiple states
with different 'name' vallues.

Daniel Schröter

unread,
Sep 17, 2014, 6:22:49 PM9/17/14
to salt-...@googlegroups.com
Short question,
any ideas why I receive the following error if I want to concat the pillar values with ~?

Rendering SLS  ... failed: Jinja syntax error: unexpected '~'; line 12

{% set tibcohomepath = salt['pillar.get']('TIBCO_HOME_PATH') %}
{% set tibcodomainname = salt['pillar.get']('TIBCO_DOMAIN_NAME') %}
{% if salt['file.file_exists'](~ tibcohomepath ~ "\administrator\domain\" ~ tibcodomainname ~ "\bin\tibcoadmin_" ~ tibcodomainname ~ ".exe") == true %}
[...]
{% endif %}

My pillars are:
TIBCO_HOME_PATH: C:\tibco
TIBCO_DOMAIN_NAME: TIBCO_TEST


Thank you!

Florian Ermisch

unread,
Sep 17, 2014, 7:27:47 PM9/17/14
to salt-...@googlegroups.com
Probably b/c you're trying to concat the '(' with 'tibcohomepath'.

Regards, Florian

Am 18.09.2014 um 00:22 schrieb Daniel Schröter:
> Short question,
> any ideas why I receive the following error if I want to concat the pillar
> values with ~?
>
> Rendering SLS ... failed: Jinja syntax error: unexpected '~'; line 12
>
> {% set tibcohomepath = salt['pillar.get']('TIBCO_HOME_PATH') %}
> {% set tibcodomainname = salt['pillar.get']('TIBCO_DOMAIN_NAME') %}
> {% if salt['file.file_exists'](~ tibcohomepath ~ "\administrator\domain\" ~
> tibcodomainname ~ "\bin\tibcoadmin_" ~ tibcodomainname ~ ".exe") == true %}
> [...]
> {% endif %}
>
> My pillars are:
> TIBCO_HOME_PATH: C:\tibco
> TIBCO_DOMAIN_NAME: TIBCO_TEST
>
>
> Thank you!
>
>
> Am Donnerstag, 27. Februar 2014 23:07:54 UTC+1 schrieb Sam Lai:
>> [...]

signature.asc

Daniel Schröter

unread,
Sep 22, 2014, 7:10:50 AM9/22/14
to salt-...@googlegroups.com
The following code solved my issue with the variables:

{% set tibcohomepath = salt['pillar.get']('TIBCO_HOME_PATH') %}
{% set tibcodomainname = salt['pillar.get']('TIBCO_DOMAIN_NAME') %}


{% if salt['file.file_exists'](pillar['TIBCO_HOME_PATH'] + "/administrator/domain/" + pillar['TIBCO_DOMAIN_NAME'] + "/bin/tibcoadmin_" + pillar['TIBCO_DOMAIN_NAME'] + ".exe") %}
...
...
{% endif %}

Thanks!

Arnold Bechtoldt

unread,
Sep 22, 2014, 8:45:43 AM9/22/14
to salt-...@googlegroups.com
{% set tibcohomepath = salt['pillar.get']('TIBCO_HOME_PATH') %}
{% set tibcodomainname = salt['pillar.get']('TIBCO_DOMAIN_NAME') %}
{% set myfilepath = tibcohomepath ~ '/administrator/domain/' ~
tibcodomainname ~ '/bin/tibcoadmin_' ~ tibcodomainname ~ '.exe' %}

{% if salt['file.file_exists'](myfilepath) %}
{% endif %}


RTFM should have helped.


--
Arnold Bechtoldt

Karlsruhe, Germany

Am 22.09.14 13:10, schrieb Daniel Schröter:
> The following code solved my issue with the variables:
>
> |
> {%settibcohomepath =salt['pillar.get']('TIBCO_HOME_PATH')%}
> {%settibcodomainname =salt['pillar.get']('TIBCO_DOMAIN_NAME')%}
>
>
> {%ifsalt['file.file_exists'](pillar['TIBCO_HOME_PATH']+"/administrator/domain/"+pillar['TIBCO_DOMAIN_NAME']+"/bin/tibcoadmin_"+pillar['TIBCO_DOMAIN_NAME']+".exe")%}
> ...
> ...
> {%endif %}
> --
> 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
> <mailto:salt-users+...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.
0xE2356889.asc
signature.asc
Reply all
Reply to author
Forward
0 new messages