salt icon indicating copy to clipboard operation
salt copied to clipboard

[BUG] file.managed data are shuffled with distributions shipping only python35 and always trigger a change

Open kiniou opened this issue 6 years ago • 12 comments

Description I'm actually testing 3001.1 in order to perform the migration from 2019.2.5 and I'm experiencing shuffled states that trigger changes every time the minion execute a state.apply. Since we are still using LTS identified distros that ships only python35 (ie. we are mainly operating Debian Stretch and Ubuntu Xenial), I don't think I am alone with this behaviour (... or maybe i'm wrong and everyone has already switched on python3.7+ 😅).

Setup For the convenience, i've pushed a branch on my fork with a rough docker setup that reproduce this issue. It also contains a buster that shows this distro is not affected. You should be able to use docker-compose up and run twice the state.apply inside each minions to see the issue:

$ docker-compose up -d
$ for distro in stretch xenial buster; do echo "===> ${distro} highstate <==="; docker-compose exec ${distro}-minion sh -c "salt-call state.apply"; done

Anyway, here are details about the basic setup i've been using:

pillars
├── test.sls
└── top.sls
states
├── test.d
│   └── test.txt
├── test.sls
└── top.sls
  • pillars/top.sls
base:
  '*':
    - test
  • pillars/test.sls
dummy:
  all: "*"
  foo: "I@roles:foo"
  bar: "I@roles:bar"
  foobar: "I@roles:foo and I@roles:bar"
  0: 'test0'
  1: 'test1'
  2: 'test2'
  • states/top.sls
base:
  '*':
    - test
  • states/test.sls
/opt/test:
  file.managed:
    - source: salt://test.d/test.txt
    - template: jinja
  • states/test.d/test.txt
{% set dummy = pillar.get('dummy', {}) %}
dummy:
  {%- for k,v in dummy.items() %}
  {{ k }}: "{{ v }}"
  {%- endfor %}

Steps to Reproduce the behavior Run state.apply twice and you should observe diff changes on the second execution.

Expected behavior There should be no changes at all after running successive state.apply.

Screenshots

  • stretch highstate
local:
----------
          ID: /opt/test
    Function: file.managed
      Result: True
     Comment: File /opt/test updated
     Started: 15:50:03.628162
    Duration: 36.26 ms
     Changes:   
              ----------                                                                                                      
              diff:
                  --- 
                  +++ 
                  @@ -3,8 +3,8 @@
                   dummy:
                     0: "test0"
                     1: "test1"
                  -  2: "test2"
                  -  all: "*"
                     bar: "I@roles:bar"
                     foobar: "I@roles:foo and I@roles:bar"
                     foo: "I@roles:foo"
                  +  all: "*"
                  +  2: "test2"

Summary for local
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  36.260 ms
  • xenial highstate
local:
----------
          ID: /opt/test
    Function: file.managed
      Result: True
     Comment: File /opt/test updated
     Started: 15:50:05.474830
    Duration: 30.942 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -2,9 +2,9 @@
                   
                   dummy:
                     0: "test0"
                  +  1: "test1"
                  +  2: "test2"
                  +  bar: "I@roles:bar"
                     all: "*"
                  -  2: "test2"
                     foo: "I@roles:foo"
                  -  bar: "I@roles:bar"
                  -  1: "test1"
                     foobar: "I@roles:foo and I@roles:bar"

Summary for local
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:  30.942 ms
  • buster highstate
local:
----------
          ID: /opt/test
    Function: file.managed
      Result: True
     Comment: File /opt/test is in the correct state
     Started: 15:50:07.166412
    Duration: 23.65 ms
     Changes:   

Summary for local
------------
Succeeded: 1
Failed:    0
------------
Total states run:     1
Total run time:  23.650 ms

Versions Report

salt --versions-report
===> stretch <===
Salt Version:
           Salt: 3001.1-366-ge54503d265
 
Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: Not Installed
      docker-py: Not Installed
          gitdb: Not Installed
      gitpython: Not Installed
         Jinja2: 2.11.2
        libgit2: Not Installed
       M2Crypto: Not Installed
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 1.0.0
   mysql-python: Not Installed
      pycparser: Not Installed
       pycrypto: Not Installed
   pycryptodome: 3.9.8
         pygit2: Not Installed
         Python: 3.5.3 (default, Jul  9 2020, 13:00:10)
   python-gnupg: Not Installed
         PyYAML: 5.3.1
          PyZMQ: 19.0.2
          smmap: Not Installed
        timelib: Not Installed
        Tornado: 4.5.3
            ZMQ: 4.3.2
 
System Versions:
           dist: debian 9 stretch
         locale: ANSI_X3.4-1968
        machine: x86_64
        release: 5.4.0-45-generic
         system: Linux
        version: Debian GNU/Linux 9 stretch
 
===> xenial <===
Salt Version:
           Salt: 3001.1-366-ga63df5e
 
Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: Not Installed
      docker-py: Not Installed
          gitdb: Not Installed
      gitpython: Not Installed
         Jinja2: 2.11.2
        libgit2: Not Installed
       M2Crypto: Not Installed
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 1.0.0
   mysql-python: Not Installed
      pycparser: Not Installed
       pycrypto: Not Installed
   pycryptodome: 3.9.8
         pygit2: Not Installed
         Python: 3.5.2 (default, Jul 17 2020, 14:04:10)
   python-gnupg: Not Installed
         PyYAML: 5.3.1
          PyZMQ: 19.0.2
          smmap: Not Installed
        timelib: Not Installed
        Tornado: 4.5.3
            ZMQ: 4.3.2
 
System Versions:
           dist: ubuntu 16.04 xenial
         locale: ANSI_X3.4-1968
        machine: x86_64
        release: 5.4.0-45-generic
         system: Linux
        version: Ubuntu 16.04 xenial
 
===> buster <===
Salt Version:
           Salt: 3001.1-366-ga63df5e801
 
Dependency Versions:
           cffi: Not Installed
       cherrypy: Not Installed
       dateutil: Not Installed
      docker-py: Not Installed
          gitdb: Not Installed
      gitpython: Not Installed
         Jinja2: 2.11.2
        libgit2: Not Installed
       M2Crypto: Not Installed
           Mako: Not Installed
   msgpack-pure: Not Installed
 msgpack-python: 1.0.0
   mysql-python: Not Installed
      pycparser: Not Installed
       pycrypto: Not Installed
   pycryptodome: 3.9.8
         pygit2: Not Installed
         Python: 3.7.3 (default, Jul 25 2020, 13:03:44)
   python-gnupg: Not Installed
         PyYAML: 5.3.1
          PyZMQ: 19.0.2
          smmap: Not Installed
        timelib: Not Installed
        Tornado: 4.5.3
            ZMQ: 4.3.2
 
System Versions:
           dist: debian 10 buster
         locale: utf-8
        machine: x86_64
        release: 5.4.0-45-generic
         system: Linux
        version: Debian GNU/Linux 10 buster

kiniou avatar Sep 08 '20 16:09 kiniou

... and i think i forgot to add a bit of context 😅 (thanks @OrangeDog for the replace "workaround"): I'm a happy user of formulas and i've experienced this issue with the salt-formula which triggers a master restart every time.

Also, I'm not sure a PR with the jinja replace trick to every formulas seems the right thing to do ...

kiniou avatar Sep 08 '20 16:09 kiniou

You can't rely on the iteration order of a dict, and you never could.

OrangeDog avatar Sep 08 '20 17:09 OrangeDog

yeah sure, i know that but this has been somehow fixed or reverted since python3.7 (this does not affect buster at all which is on python3.7) and i thought pillar data were somehow wrapped into some OrderedDict 🤷

kiniou avatar Sep 08 '20 17:09 kiniou

You can't rely on the iteration order of a dict, and you never could.

According to python3.7 changelog, this has been officialized in the language spec of python.

kiniou avatar Sep 08 '20 17:09 kiniou

@kiniou I agree with @OrangeDog here. You're iterating through a dict. This operation is not deterministic in order unless you're using Python 3.7+.

DmitryKuzmenko avatar Sep 09 '20 08:09 DmitryKuzmenko

@DmitryKuzmenko like i said earlier, i already know that and the example i provided to reproduced is just picked from the salt formula itself (and i think it's not the only one): https://github.com/saltstack-formulas/salt-formula/blob/master/salt/files/master.d/f_defaults.conf#L1636-L1647

So my only alternatives right now are to upgrade all my minions to debian buster or fork the salt-formula (and maybe others) to work with python3.5.

kiniou avatar Sep 09 '20 09:09 kiniou

@kiniou you can update any formula and provide a pull request with your changes. Here's a small change in your template that helps in your case. I've changed calling of the .items() method with dictsort jinja filter.

{% set dummy = pillar.get('dummy', {}) %}
dummy:
  {%- for k,v in dummy|dictsort %}
  {{ k }}: "{{ v }}"
  {%- endfor %}

I really don't see what we should fix in SaltStack core code here. Anyway if you have a propose, welcome to share your thoughts.

DmitryKuzmenko avatar Sep 09 '20 10:09 DmitryKuzmenko

@saltstack/team-core thoughts on this?

DmitryKuzmenko avatar Sep 09 '20 10:09 DmitryKuzmenko

agree with @DmitryKuzmenko looks like the fix would be in the salt-formulas repo.

Ch3LL avatar Sep 09 '20 10:09 Ch3LL

Here's a small change in your template that helps in your case. I've changed calling of the .items() method with dictsort jinja filter.

Indeed, i was not aware of this dictsort jinja filter and this works ok. I will try to propose the change to the formula, but this will introduce some minor breaking changes to others that are luckily expecting guaranteed dict insertion order with salt <3000 with python2.7 or salt >=3000 with python >3.5 (only on the first state.apply so this can be considered a minor change).

I really don't see what we should fix in SaltStack core code here. Anyway if you have a propose, welcome to share your thoughts.

I understand this might not be saltstack to handle this kind of compatibility issues (i know the pain to handle those) and i was just thinking there could be some way to use Ordererdict somewhere in the renderers' code. And maybe i'm doing a wrong assumption because this issue lies somewhere with either Jinja rendering or YAML loader. Now, I can know my way in some parts of the salt's code but this one is a bit obscure to me and I wrote this issue in order to get insights from the team.

Feel free to close the issue if you think it's not worth it. In the meantime, i will consider the dictsort option (... and i will not take personally the closing of this issue 😉)

Thank you all for your replies 👍

kiniou avatar Sep 09 '20 12:09 kiniou

@Ch3LL thank you for response. @kiniou I always glad to help! Thank you for using Salt and asking questions. This is why we're here! =) I'm closing this since we're done here for now.

DmitryKuzmenko avatar Sep 09 '20 12:09 DmitryKuzmenko