salt icon indicating copy to clipboard operation
salt copied to clipboard

Can't use Umlauts in Pillar

Open lloesche opened this issue 11 years ago • 6 comments

I have a pillar that looks like this:


---
users:
  lukas:
    fullname: Lukas Lösche
    crypt: ''
    uid: 2012
    gid: 2012
...
...
...

Note the ö in the fullname field.

Trying to create the user using a formula results in the following error

    Data failed to compile:
----------
    Rendering SLS 'base:users' failed: Jinja error: 'ascii' codec can't decode byte 0xc3 in position 7: ordinal not in range(128)
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/salt/utils/templates.py", line 280, in render_jinja_tmpl
    output = template.render(**decoded_context)
  File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "<template>", line 17, in top-level template code
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 7: ordinal not in range(128)

; line 17

---
[...]
    - name: {{ username }}
    - shell: /bin/bash
    - home: /home/{{ username }}
    - uid: {{ details.get('uid', '') }}
    - gid: {{ details.get('gid', '') }}
    - password: {{ details.get('crypt','') }}    <======================
    {% if 'groups' in details %}
    - groups:
      {% for group in details.get('groups', []) %}
      - {{ group }}
      {% endfor %}
[...]

---

The formula looks like this:

{% for username, details in pillar.get('users', {}).items() %}
{{ username }}:
  group:
    - present
    - name: {{ username }}
    - gid: {{ details.get('gid', '') }}

  user:
    - present
    - fullname: {{ details.get('fullname','') }}
    - name: {{ username }}
    - shell: /bin/bash
    - home: /home/{{ username }}
    - uid: {{ details.get('uid', '') }}
    - gid: {{ details.get('gid', '') }}
    - password: {{ details.get('crypt','') }}
    {% if 'groups' in details %}
    - groups:
      {% for group in details.get('groups', []) %}
      - {{ group }}
      {% endfor %}
    {% endif %}
    {% if 'optional_groups' in details %}
    - optional_groups:
      {% for group in details.get('optional_groups', []) %}
      - {{ group }}
      {% endfor %}
    {% endif %}
    - require:
      - group: {{ username }}
  {% if 'pub_ssh_keys' in details %}
  ssh_auth:
    - present
    - user: {{ username }}
    - names:
    {% for pub_ssh_key in details.get('pub_ssh_keys', []) %}
      - {{ pub_ssh_key }}
    {% endfor %}
    - require:
      - user: {{ username }}
  {% endif %}

{% endfor %}

If I replace the 'ö' with a 'o' the formula is working fine. Is this a bug or do I have an encoding problem somewhere? I'm on version 2015.5.0.

lloesche avatar Jun 05 '15 09:06 lloesche

have you enabled utf-8 support?

http://docs.saltstack.com/en/latest/ref/configuration/master.html#yaml-utf8

arnisoph avatar Jun 05 '15 09:06 arnisoph

See also: #3436

eliasp avatar Jun 07 '15 21:06 eliasp

@bechtoldt I had not enabled it, but now I have and the error remains the same.

My environment is CentOS 7 with latest EPEL packages:

[[email protected] ~]# grep utf8 /etc/salt/master
yaml_utf8: True
[[email protected] ~]# salt --versions-report
           Salt: 2015.5.0
         Python: 2.7.5 (default, Jun 17 2014, 18:11:42)
         Jinja2: 2.7.2
       M2Crypto: 0.21.1
 msgpack-python: 0.4.6
   msgpack-pure: Not Installed
       pycrypto: 2.6.1
        libnacl: Not Installed
         PyYAML: 3.10
          ioflo: Not Installed
          PyZMQ: 14.3.1
           RAET: Not Installed
            ZMQ: 3.2.5
           Mako: Not Installed

lloesche avatar Jun 10 '15 17:06 lloesche

@lloesche, thanks for the report. This is a duplicate of #3436. Does the workaround proposed in https://github.com/saltstack/salt/issues/3436#issuecomment-61225863 work?

jfindlay avatar Jun 10 '15 20:06 jfindlay

@jfindlay, I stumbled over some similar issues, recently. After searching and reading arround some of the issues here, I haven't manged it to work, yet. Since this is my first comment on github, I apologize if I've done something wrong and it's completely my fault why my state isn't working.

What I'm trying to do in a short:

  • manage users on a debain salt minion from a debian salt master
  • pillar data contains a user's surname with a german umlaut 'ü'
  • state uses user.present and the surname from pillar data is used in the fullname argument
  • state.highstate fails with "'ascii' codec can't encode character u'\xfc'"

What I've tried so far:

  • adding the yaml_utf8: True to the master config in /etc/salt/master
  • Checking the pillar data with salt '*' pillar.data is okay see pillar.data snippet below
  • Running salt-call -l debug state.highstate on the minion:
    • Excerpts from the debug output:
      • [PROFILE ] Time (in seconds) to render '/var/cache/salt/minion/files/base/user/init.sls' using 'jinja' renderer: 0.0462970733643
      • [DEBUG ] Results of YAML rendering: [...] OrderedDict([('fullname', u' FABOSI Mitr\xfccker')]) [...]
  • splice a decode into the state after the -name argument ({% set usname = user.surname %} {% set usname = usname.decode('utf-8') %} and use {{ usname }} instead of {{ user.surname }}

Any help on this is appreciated, since I thought this has been fixed in 2015.8.7 as @bernieke wrote for 2015.8.1 in issue #3436 (if it's really a bug and not my inproper work ;)

These are some settings and versions: ----- MASTER ------

Salt Version:
           Salt: 2015.8.7

Dependency Versions:
         Jinja2: 2.7.3
       M2Crypto: 0.21.1
           Mako: 1.0.0
         PyYAML: 3.11
          PyZMQ: 14.4.0
         Python: 2.7.9 (default, Mar  1 2015, 12:57:24)
           RAET: Not Installed
        Tornado: 4.2.1
            ZMQ: 4.0.5
           cffi: 0.8.6
       cherrypy: Not Installed
       dateutil: 2.2
          gitdb: 0.5.4
      gitpython: 0.3.2 RC1
          ioflo: Not Installed
        libgit2: Not Installed
        libnacl: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.4.2
   mysql-python: 1.2.3
      pycparser: 2.10
       pycrypto: 2.6.1
         pygit2: Not Installed
   python-gnupg: Not Installed
          smmap: 0.8.2
        timelib: Not Installed

System Versions:
           dist: debian 8.3 
        machine: x86_64
        release: 3.16.0-4-amd64
         system: debian 8.3 

A snippet from /etc/salt/master: (Some lines before and after if the location matters) *** SNIP ***

 # The state_output setting changes if the output is the full multi line
 # output for each changed state if set to 'full', but if set to 'terse'
 # the output will be shortened to a single line.  If set to 'mixed', the output
 # will be terse unless a state failed, in which case that output will be full.
 #state_output: full

yaml_utf8: True


 #####      File Server settings      #####
 ##########################################
 # Salt runs a lightweight file server written in zeromq to deliver files to
 # minions. This file server is built into the master daemon and does not
 # require a dedicated port.

 *************

----- MINION ------

Salt Version:
           Salt: 2015.8.7

Dependency Versions:
         Jinja2: 2.7.3
       M2Crypto: 0.21.1
           Mako: 1.0.0
         PyYAML: 3.11
          PyZMQ: 14.4.0
         Python: 2.7.9 (default, Mar  1 2015, 12:57:24)
           RAET: Not Installed
        Tornado: 4.2.1
            ZMQ: 4.0.5
           cffi: 0.8.6
       cherrypy: Not Installed
       dateutil: 2.2
          gitdb: Not Installed
      gitpython: Not Installed
          ioflo: Not Installed
        libgit2: Not Installed
        libnacl: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 0.4.2
   mysql-python: 1.2.3
      pycparser: 2.10
       pycrypto: 2.6.1
         pygit2: Not Installed
   python-gnupg: Not Installed
          smmap: Not Installed
        timelib: Not Installed

System Versions:
           dist: debian 8.3 
        machine: x86_64
        release: 3.16.0-4-amd64
         system: debian 8.3 

---- THE PILLAR SNIPPET ---- (/srv/pillar/user/init.sls)

users:

  mittruecker:
    password: "CENSORED"
    present: True
    purge: False
    sudo: False
    createhome: True
    title: ""
    ou: ""
    name: "FABOSI"
    surname: "Mittrücker"
    phonework: "+49-CENSORED"
    mobilework:
    faxwork: "+49-CENSORED"
    emailwork: "CENSORED"
    home: "CENSORED"
    shell: "/bin/true"
    primarygroup: CENSORED

---- THE STATE ---- /srv/salt/user/init.sls

{% set users = pillar.get('users', {}) %}

{%- for username, user in users.items() %}
{%- if user.present %}
{% set userpresent = 'present' %}
{%- else %}
{% set userpresent = 'absent' %}
{% endif %} {# if user present #}

Manage {{ userpresent }} user {{ username }}:
  user.{{ userpresent }}:
    - name: {{ username }}
    - password: "{{ user.password }}"
{%- if user.present %}
    - fullname: "{{ user.title }} {{ user.name }} {{ user.surname }}"
    - workphone: {{ user.phonework }}
    - homephone: {{ user.faxwork }}
    - home: {{ user.home }}
    - createhome: {{ user.createhome }}
    - shell: {{ user.shell }}
{%- else %}
    - purge: {{ user.purge }}
 {% endif %} {# if user present #}
 {%- endfor %} {# endfor users #}

---- PILLAR.DATA ---- (salt '*' pillar.data)

        mittruecker:
            ----------
            createhome:
                True
            emailwork:
                CENSRORED
            faxwork:
                CENSORED
            home:
                CENSORED
            mobilework:
                None
            name:
                FABOSI
            ou:
            password:
                CENSORED
            phonework:
                CENSORED
            present:
                True
            primarygroup:
                CENSORED
            purge:
                False
            shell:
                /bin/true
            sudo:
                False
            surname:
                Mittrücker
            title:

--- SALT-CALL ---- (salt-call -l debug state.highstate on minion)

          ID: Manage present user mittruecker
    Function: user.present
        Name: mittruecker
      Result: False
     Comment: An exception occurred in this state: Traceback (most recent call last):
                File "/usr/lib/python2.7/dist-packages/salt/state.py", line 1624, in call
                  **cdata['kwargs'])
                File "/usr/lib/python2.7/dist-packages/salt/loader.py", line 1491, in wrapper
                  return f(*args, **kwargs)
                File "/usr/lib/python2.7/dist-packages/salt/states/user.py", line 507, in present
                  __salt__['user.ch{0}'.format(key)](name, val)
                File "/usr/lib/python2.7/dist-packages/salt/modules/useradd.py", line 406, in chfullname
                  return _update_gecos(name, 'fullname', fullname)
                File "/usr/lib/python2.7/dist-packages/salt/modules/useradd.py", line 79, in _update_gecos
                  cmd = ['usermod', '-c', _build_gecos(gecos_data), name]
                File "/usr/lib/python2.7/dist-packages/salt/modules/useradd.py", line 61, in _build_gecos
                  gecos_dict.get('homephone', ''))
              UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 12: ordinal not in range(128)
     Started: 16:04:39.493284
    Duration: 5.104 ms
     Changes:   

fabosi avatar Mar 16 '16 16:03 fabosi

@fabosi, thanks for the excellent report. Since your issue seems to be arising from a different place in the code I have created a new issue, #31928.

jfindlay avatar Mar 16 '16 19:03 jfindlay