From 2bd8bba297e16903c202ffae2d7474834b7a772e Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Mon, 7 Jan 2019 13:12:31 -0600 Subject: [PATCH] [stable-2.7] Fix reverse_inventory order to work on python3 (#49895) (#50048) * [stable-2.7] Fix reverse_inventory order to work on python3 (#49895) (cherry picked from commit a0d71e7) Co-authored-by: Matt Martz * Clarify the change made to reverse_inventory --- changelogs/fragments/playbook-order-reverse_inventory.yaml | 3 +++ lib/ansible/inventory/manager.py | 7 ++++--- 2 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/playbook-order-reverse_inventory.yaml diff --git a/changelogs/fragments/playbook-order-reverse_inventory.yaml b/changelogs/fragments/playbook-order-reverse_inventory.yaml new file mode 100644 index 00000000000..03459d2fc96 --- /dev/null +++ b/changelogs/fragments/playbook-order-reverse_inventory.yaml @@ -0,0 +1,3 @@ +bugfixes: +- host execution order - Fix ``reverse_inventory`` not to change the order of + the items before reversing on python2 and to not backtrace on python3 diff --git a/lib/ansible/inventory/manager.py b/lib/ansible/inventory/manager.py index fc39e25db2b..7bd4abd9ee6 100644 --- a/lib/ansible/inventory/manager.py +++ b/lib/ansible/inventory/manager.py @@ -24,6 +24,9 @@ import os import re import itertools +from operator import attrgetter +from random import shuffle + from ansible import constants as C from ansible.errors import AnsibleError, AnsibleOptionsError, AnsibleParserError from ansible.inventory.data import InventoryData @@ -368,14 +371,12 @@ class InventoryManager(object): # sort hosts list if needed (should only happen when called from strategy) if order in ['sorted', 'reverse_sorted']: - from operator import attrgetter hosts = sorted(self._hosts_patterns_cache[pattern_hash][:], key=attrgetter('name'), reverse=(order == 'reverse_sorted')) elif order == 'reverse_inventory': - hosts = sorted(self._hosts_patterns_cache[pattern_hash][:], reverse=True) + hosts = self._hosts_patterns_cache[pattern_hash][::-1] else: hosts = self._hosts_patterns_cache[pattern_hash][:] if order == 'shuffle': - from random import shuffle shuffle(hosts) elif order not in [None, 'inventory']: raise AnsibleOptionsError("Invalid 'order' specified for inventory hosts: %s" % order)