From a0d71e7735b611e97aa9f5ad464265b0e3e26ce6 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Mon, 17 Dec 2018 15:39:29 -0600 Subject: [PATCH] Fix reverse_inventory order to work on python3 (#49895) --- changelogs/fragments/playbook-order-py3.yaml | 2 ++ lib/ansible/inventory/manager.py | 7 ++++--- 2 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/playbook-order-py3.yaml diff --git a/changelogs/fragments/playbook-order-py3.yaml b/changelogs/fragments/playbook-order-py3.yaml new file mode 100644 index 0000000000..87fdadf209 --- /dev/null +++ b/changelogs/fragments/playbook-order-py3.yaml @@ -0,0 +1,2 @@ +bugfixes: +- host execution order - Fix ``reverse_inventory`` to work on python3 diff --git a/lib/ansible/inventory/manager.py b/lib/ansible/inventory/manager.py index 62d3e87b9a..de2041a68f 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 @@ -367,14 +370,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)