pillar based conditional in statefile not working(?)

83 views
Skip to first unread message

Chris Meisinger

unread,
May 20, 2013, 8:23:00 PM5/20/13
to salt-...@googlegroups.com
Hey there, 

I'm trying to use a conditional based on pillar data within a statefile that creates pgsql users/databases based on a flag in pillar and I'm not having any luck.  I've attempted to use variants on the following  "if" methods without much luck....  To confirm, calling salt hostname\* pillar.data key=postgres_config, returns the appropriate information as does calling pillar.get.   It just seems to never evaluate as "true" in this instances.

Any ideas?


Pillar Data:
=========
postgres_config:
  devel_staging:
    database_host: localhost
    database_name: devel_staging
    database_username:  username
    database_password:  lulpasswordlul
    database_port: 5432
    database_local: 1
===========

Relevant state portion:

==============
{% for dbname, args in pillar['postgres_config'].iteritems() %}
{% if {{ args['database_username'] }} == "1" %}
create-role-{{ dbname }}:
  postgres_user.present:
    - name: {{ args['database_username'] }}
    - createdb: False
    - createuser: False
    - superuser: False
    - encrypted: True
    - password: {{ args['database_password'] }}
    - runas: postgres
    - require:
      - service: postgresql-9.1
  postgres_database.present:
     - owner: {{ args['database_username'] }}
     - name:  {{ args['database_name'] }}
     - runas: postgres
     - lc_ctype:  en_US.UTF-8
     - lc_collate:  en_US.UTF-8
     - encoding:  UTF8
     - require:
       - postgres_user: {{ args['database_username'] }}
{% endif %}
{% endfor %}
===================

========
variants
========
{% if salt['pillar.get']('postgres_config:{{ dbname }}:database_local') == "1" %}
{% if pillar.get('postgres_config:{{ dbname }}:database_local', {}) == "1" %}
========

Thanks!

Chris

Mina Naguib

unread,
May 20, 2013, 9:07:03 PM5/20/13
to salt-...@googlegroups.com
On 2013-05-20, at 8:23 PM, Chris Meisinger <cmeis...@gmail.com> wrote:

Hey there, 

I'm trying to use a conditional based on pillar data 
[snip]
Relevant state portion:

==============
{% for dbname, args in pillar['postgres_config'].iteritems() %}
{% if {{ args['database_username'] }} == "1" %}

Try:

Chris Meisinger

unread,
May 20, 2013, 10:54:39 PM5/20/13
to salt-...@googlegroups.com
Hey there, 

So based on your suggestion,  I gave that a shot and didn't have much luck either.  I removed the '== '1'' portion, just to see if a pure existence check on the key would pass, which it did.  Fully removing that key caused an UNDEF (and salt-master to dump a stacktrace), so I didn't have my 'toggle' just yet.

The interesting thing here is that when I changed to matching based on a normal word and not True/False/1/0 as boolean values, it worked.  Changing the {% if %} portion to read as follows works as expected:
{% if args['database_location'] == "local" %}

It looks as if the values "1/0" won't get fully passed through to the calling state when retrieved from pillar, or at least in this situation. I'm not sure if this is a bug, or I'm just tripping over reserved words here.

Thanks for your help!

Mrten

unread,
May 21, 2013, 4:31:11 AM5/21/13
to salt-...@googlegroups.com
On 21/5/2013 04:54 , Chris Meisinger wrote:
> Hey there,
>
> So based on your suggestion, I gave that a shot and didn't have much
> luck either. I removed the '== '1'' portion, just to see if a pure
> existence check on the key would pass, which it did. Fully removing
> that key caused an UNDEF (and salt-master to dump a stacktrace), so I
> didn't have my 'toggle' just yet.
>
> The interesting thing here is that when I changed to matching based on a
> normal word and not True/False/1/0 as boolean values, it worked.
> Changing the {% if %} portion to read as follows works as expected:
> {% if args['database_location'] == "local" %}
>
> It looks as if the values "1/0" won't get fully passed through to the
> calling state when retrieved from pillar, or at least in this situation.
> I'm not sure if this is a bug, or I'm just tripping over reserved words
> here.

There's something with YAML.

Does

{% if args['database_location'] == 1 %}

work? I use that all the time.

M.

Mina Naguib

unread,
May 21, 2013, 10:43:39 AM5/21/13
to salt-...@googlegroups.com

YAML parsers autodetect data types of entities they encounter. See:

Odds are if you have in your YAML config:
foo: 1

Then it will be parsed as an integer, which means you need to test for it and use it as an integer.

If you need it as a string, set it to "1" in the YAML config.  If you're using 1/0 as truths, you may be better off using Yes/No in YAML (which get translated to python True/False)



On 2013-05-20, at 10:54 PM, Chris Meisinger <cmeis...@gmail.com> wrote:

Hey there, 

So based on your suggestion,  I gave that a shot and didn't have much luck either.  I removed the '== '1'' portion, just to see if a pure existence check on the key would pass, which it did.  Fully removing that key caused an UNDEF (and salt-master to dump a stacktrace), so I didn't have my 'toggle' just yet.

The interesting thing here is that when I changed to matching based on a normal word and not True/False/1/0 as boolean values, it worked.  Changing the {% if %} portion to read as follows works as expected:
{% if args['database_location'] == "local" %}

It looks as if the values "1/0" won't get fully passed through to the calling state when retrieved from pillar, or at least in this situation. I'm not sure if this is a bug, or I'm just tripping over reserved words here.

Thanks for your help!

On Monday, May 20, 2013 8:07:03 PM UTC-5, Mina Naguib wrote:


On 2013-05-20, at 8:23 PM, Chris Meisinger <cmeis...@gmail.com> wrote:

Hey there, 

I'm trying to use a conditional based on pillar data 
[snip]
Relevant state portion:

==============
{% for dbname, args in pillar['postgres_config'].iteritems() %}
{% if {{ args['database_username'] }} == "1" %}

Try:

{% if args['database_username'] == "1" %}




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

Reply all
Reply to author
Forward
0 new messages