pillar values of 'yes' and 'no' in fact return as 'True' and 'False'

1,971 views
Skip to first unread message

Badmeischter

unread,
Jul 11, 2013, 12:12:18 PM7/11/13
to salt-...@googlegroups.com
hi all, a comment up front, I'm using salt 0.14.0-1. I want a pillar file providing a default value for 'all' and specific values for certain hosts, in the example below for the sshd_config file.

pillar

riet@ux0515 - none > cat ssh.sls
ssh:
  MaxStartups:
    all: '10'
    ux0112: '50'
    ux7080: '100'

  PermitRootLogin:
    all: 'no'
    ux0936: 'yes'

state file

riet@ux0515 - none > cat test.sls
{% from 'lib.jinja' import attribute_val with context %}
/tmp/foobar:
  file.managed:
    - source: salt://fs/tmp/foobar
    - template: jinja
    - context:
      MaxStartups: {{attribute_val('ssh', 'MaxStartups')}}
      PermitRootLogin: {{attribute_val('ssh', 'PermitRootLogin')}}

template include

riet@ux0515 - none > cat lib.jinja
{%- macro attribute_val(key, attr) -%}
  {%- if grains['id'] in pillar[key][attr] -%}
    {{ salt['pillar.get'](key ~ ':' ~ attr ~ ':' ~ grains['id'], 'yes') }}
  {%- else -%}
    {{pillar[key][attr]['all']}}
  {%- endif -%}
{%- endmacro %}

/tmp/foobar

riet@ux0515 - none > cat fs/tmp/foobar
First line
{%- if grains['id'] == 'ux0933' %}
Hallo ux0933
{%- endif %}
Hallo Hans
MaxStartups {{MaxStartups}}
PermitRootLogin {{PermitRootLogin}}

The problem I'm having, in the expanded /tmp/foobar file 'MaxStartups' does receive a number (10, 50 or 100 depending on hostname) but 'PermitRootLogin' ends up with values of 'True' and 'False', depending on whether the pillar data resolves to yes or no. The strange thing is, if I run the pillar module from the command line it works:

[root]# salt 'ux0936' pillar.get ssh:PermitUserEnvironment:ux7086
ux0936:
    yes
[root]# salt 'ux0936' pillar.get ssh:PermitUserEnvironment:all
ux0936:
    no

Am I missing something here, sorry I'm bot fluent enough in Python to dig this up myself. 
Is there a smarter way to do something like this?

Thanks for your help
Hans

Shaun Kruger

unread,
Jul 11, 2013, 12:38:28 PM7/11/13
to salt-...@googlegroups.com
You may be having a yaml problem.


If you want the string 'yes' or 'no' you need to put it in quotes so it doesn't get evaluated by the yaml parser as a boolean True or False.  The link above shows all of the unquoted strings that will be interpreted as a bool type.  I recently ran into this when generating a mysql config template.

Shaun



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

Colton Myers

unread,
Jul 11, 2013, 3:29:17 PM7/11/13
to salt-...@googlegroups.com
Can you try putting the 'yes' and 'no' in double quotes instead of single quotes?  Maybe YAML parses them differently.  I do know that it tries to magically convert "truthy" values into boolean values.

--
Colton Myers

Seth House

unread,
Jul 11, 2013, 3:59:18 PM7/11/13
to salt-...@googlegroups.com
Since the Jinja macro is pulling those values from a YAML source (the pillar) and into another YAML template those values are getting processed by YAML twice. To Shaun's suggestion, I'll bet it'll work if you put a second pair of quotes in a place that will surround the value *after* it has been processed by the Jinja but before it is processed by the second YAML pass. Perhaps the following will work (untested):

Erik Johnson

unread,
Jul 11, 2013, 6:43:48 PM7/11/13
to salt-...@googlegroups.com
On Thu, Jul 11, 2013 at 11:38 AM, Shaun Kruger <shaun....@gmail.com> wrote:
You may be having a yaml problem.


If you want the string 'yes' or 'no' you need to put it in quotes so it doesn't get evaluated by the yaml parser as a boolean True or False.  The link above shows all of the unquoted strings that will be interpreted as a bool type.  I recently ran into this when generating a mysql config template.

Shaun



Yes, this is a known issue. PyYAML loads yes/no, on/off, and true/false (even lowercase true/false) as booleans. Only way around this is to quote the string.


--

-Erik

"For me, it is far better to grasp the universe as it really is than to persist in delusion, however satisfying and reassuring."  -- Carl Sagan

martin f krafft

unread,
Jul 12, 2013, 2:03:57 AM7/12/13
to salt-...@googlegroups.com
also sprach Seth House <whit...@gmail.com> [2013.07.11.2159 +0200]:
> Since the Jinja macro is pulling those values from a YAML source
> (the pillar) and into another YAML template those values are
> getting processed by YAML twice.

Seriously? This seems like it ought to be fixed.

--
martin | http://madduck.net/ | http://two.sentenc.es/

life is a sexually transmitted disease with 100% mortality.

spamtraps: madduc...@madduck.net
digital_signature_gpg.asc

Badmeischter

unread,
Jul 12, 2013, 2:55:20 AM7/12/13
to salt-...@googlegroups.com
Hi all, thanks a lot for you suggestions and help, here is what fixed the problem, either use:

<doublequote><singlequote>yes|no<singlequote><doublequote>
<singlequote><doublequote>yes|no<doublequote><singlequote>

Simply doubling the same type of quotes is upsetting the renderer for reasons I did not further explore.

Again, thanks a lot!

Colton Myers

unread,
Jul 12, 2013, 12:32:43 PM7/12/13
to salt-...@googlegroups.com
Yep, as Seth pointed out, we're processing the YAML twice, once in pillar and once in the statefile, for example.  We lose the quotes in the process.  So the workaround is two sets of quotes as badmeischter mentioned.  We're trying to figure out a way to fix properly.

--
Colton Myers
Reply all
Reply to author
Forward
0 new messages