Sorting dicts in lists - works different between my machine and others

6 views
Skip to first unread message

Jon Spriggs

unread,
May 14, 2019, 11:40:57 AM5/14/19
to ansible...@googlegroups.com
Hi,

I have a variable which is a list (sourced from registering the response from a 3rd party module). In that list, each item is a dict, as follows:

[
  {'policyid': '1009', 'other': 'content'},
  {'policyid': '2019', 'other': 'content'},
  {'policyid': '1024', 'other': 'content'},
  {'policyid': '1000', 'other': 'content'},
  {'policyid': '8732', 'other': 'content'},
  {'policyid': '1012', 'other': 'content'}
]

I have some code that works locally on my machine:

- name: sort policy
  set_fact:
    desired_policy: |
      [
        {% for policy in fwpolicy.meta.results | sort(attribute='policyid') %}
          {
            'rule': '{{ policy.policyid }}',
            'after': '{{ (loop.previtem | default({}) )['policyid'] | default(0) }}',
            'before': '{{ (loop.nextitem | default({}) )['policyid'] | default(0) }}'
          },
        {% endfor %}
      ]

When I run this on my machine, I get the following list:

[
  {'policyid': '1000', 'after': 0, 'before': 1009},
  {'policyid': '1009' , 'after': 1000, 'before': 1012},
  {'policyid': '1012' , 'after': 1009, 'before': 1024},
  {'policyid': '1024' , 'after': 1012, 'before': 2019},
  {'policyid': '2019' , 'after': 1024, 'before': 8732},
  {'policyid': '8732' , 'after': 2019, 'before': 0}
]

On my colleague's machine (and on AWX for that matter), I get:

[
  {'policyid': '1000', 'after': 0, 'before': 0},
  {'policyid': '1009' , 'after': 0, 'before': 0},
  {'policyid': '1012' , 'after': 0, 'before': 0},
  {'policyid': '1024' , 'after': 0, 'before': 0},
  {'policyid': '2019' , 'after': 0, 'before': 0},
  {'policyid': '8732' , 'after': 0, 'before': 0}
]

Am I doing something wrong, and should I be doing something differently?
--
Jon "The Nice Guy" Spriggs
@jontheniceguy everywhere...
https://jon.sprig.gs
Reply all
Reply to author
Forward
0 new messages