Skip to content

Commit

Permalink
Fix #410
Browse files Browse the repository at this point in the history
  • Loading branch information
micafer committed Sep 19, 2017
1 parent 0ee49c2 commit 5c3f44b
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 27 deletions.
12 changes: 6 additions & 6 deletions IM/ansible_utils/ansible_executor_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,6 @@ def run(self):
self._inventory.set_playbook_basedir(
os.path.dirname(playbook_path))

i = 1

# make sure the tqm has callbacks loaded
self._tqm.load_callbacks()
self._tqm.send_callback('v2_playbook_on_start', pb)
Expand All @@ -393,8 +391,12 @@ def run(self):
# Create a temporary copy of the play here, so we can run post_validate
# on it without the templating changes affecting the
# original object.
all_vars = self._variable_manager.get_vars(
loader=self._loader, play=play)
try:
# for Ansible version 2.3.2 or lower
all_vars = self._variable_manager.get_vars(loader=self._loader, play=play)
except TypeError:
# for Ansible version 2.4.0 or higher
all_vars = self._variable_manager.get_vars(play=play)
templar = Templar(loader=self._loader, variables=all_vars)
new_play = play.copy()
new_play.post_validate(templar)
Expand Down Expand Up @@ -442,8 +444,6 @@ def run(self):
if result not in (0, 3):
break

i = i + 1 # per play

self._tqm.send_callback(
'v2_playbook_on_stats', self._tqm._stats)

Expand Down
74 changes: 53 additions & 21 deletions IM/ansible_utils/ansible_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import subprocess
import signal
import logging
from distutils.version import LooseVersion
from ansible import errors
from ansible import __version__ as ansible_version

Expand All @@ -41,9 +42,11 @@
try:
# for Ansible version 2.4.0 or higher
from ansible.vars.manager import VariableManager
except:
from ansible.inventory.manager import InventoryManager
except ImportError:
# for Ansible version 2.3.2 or lower
from ansible.vars import VariableManager
from ansible.inventory import Inventory
import ansible.inventory

from .ansible_executor_v2 import IMPlaybookExecutor
Expand Down Expand Up @@ -136,6 +139,54 @@ def run(self):
display("ERROR: %s" % e, output=self.output)
self.result.put((0, (1, []), output))

def get_play_prereqs(self, options):
if LooseVersion(ansible_version) >= LooseVersion("2.4.0"):
# for Ansible version 2.4.0 or higher
return self.get_play_prereqs_2_4(options)
else:
# for Ansible version 2.3.2 or lower
return self.get_play_prereqs_2(options)

def get_play_prereqs_2(self, options):
loader = DataLoader()

if self.vault_pass:
loader.set_vault_password(self.vault_pass)

variable_manager = VariableManager()
variable_manager.extra_vars = self.extra_vars

# Add this to avoid the Ansible bug: no host vars as host is not in inventory
# In version 2.0.1 it must be fixed
ansible.inventory.HOSTS_PATTERNS_CACHE = {}

inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=options.inventory)
variable_manager.set_inventory(inventory)

if self.host:
inventory.subset(self.host)
# let inventory know which playbooks are using so it can know the
# basedirs
inventory.set_playbook_basedir(os.path.dirname(self.playbook_file))

return loader, inventory, variable_manager

def get_play_prereqs_2_4(self, options):
loader = DataLoader()

if self.vault_pass:
loader.set_vault_password(self.vault_pass)

# create the inventory, and filter it based on the subset specified (if any)
inventory = InventoryManager(loader=loader, sources=options.inventory)

# create the variable manager, which will be shared throughout
# the code, ensuring a consistent view of global variables
variable_manager = VariableManager(loader=loader, inventory=inventory)
variable_manager.extra_vars = self.extra_vars

return loader, inventory, variable_manager

def launch_playbook_v2(self):
''' run ansible-playbook operations v2.X'''
# create parser for CLI options
Expand Down Expand Up @@ -176,31 +227,12 @@ def launch_playbook_v2(self):
raise errors.AnsibleError(
"the playbook: %s does not appear to be a file" % self.playbook_file)

variable_manager = VariableManager()
variable_manager.extra_vars = self.extra_vars

if self.inventory_file:
options.inventory = self.inventory_file

options.forks = self.threads

loader = DataLoader()
# Add this to avoid the Ansible bug: no host vars as host is not in inventory
# In version 2.0.1 it must be fixed
ansible.inventory.HOSTS_PATTERNS_CACHE = {}

if self.vault_pass:
loader.set_vault_password(self.vault_pass)

inventory = ansible.inventory.Inventory(
loader=loader, variable_manager=variable_manager, host_list=options.inventory)
variable_manager.set_inventory(inventory)

if self.host:
inventory.subset(self.host)
# let inventory know which playbooks are using so it can know the
# basedirs
inventory.set_playbook_basedir(os.path.dirname(self.playbook_file))
loader, inventory, variable_manager = self.get_play_prereqs(options)

num_retries = 0
return_code = 4
Expand Down

0 comments on commit 5c3f44b

Please sign in to comment.