Home:ALL Converter>Ansible Playbook Retry Logic Using Python API

Ansible Playbook Retry Logic Using Python API

Ask Time:2018-04-14T03:26:35         Author:P Gilson

Json Formatter

Using the Ansible 2 Python API I'm able to run playbooks and handle results with a custom callback handler (thanks to this question). Everything works well, but now I'd like to implement a simple retry loop for the PlaybookExecutor.

All my callback handler is doing is stuffing any failed tasks in an array, and if I see that the array isn't empty count it as a failure and try again.

I have another python module that uses this script to kick off the playbook. The call to run_playbook is nested in a try/except block and I'd like an exception to bubble up so I can properly handle the failure.

I'd like to give my playbook 3 attempts at running and if all fail then raise an exception.

Here is my code:

#! /usr/bin/python
# -*- coding: utf-8 -*-

from __future__ import print_function
import logging
import os
from collections import namedtuple
from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager
from ansible.inventory.manager import InventoryManager
from ansible.executor.playbook_executor import PlaybookExecutor
from ansible.plugins.callback import CallbackBase

class ResultsCallback(CallbackBase):
    """ A callback plugin used for performing an action as results come in """
    def __init__(self):
        super(ResultsCallback, self).__init__()
        # Store all failed results
        self.failed = []

    def v2_runner_on_failed(self, result, ignore_errors=False):
        if ignore_errors:
            self._display.display("...ignoring", color=C.COLOR_SKIP)
        host = result._host
        self.failed.append(result.task_name)


def create_inventory_file(hostnames):
    inv_file = 'ansible_hosts.{0}'.format(os.getppid())
    logging.print('\nCreating Ansible host file: {0}/{1}'.format(os.path.join(os.path.expanduser('~')), inv_file))
    with open(os.path.join(os.path.expanduser('~'), inv_file), 'w') as host_file:
        # If ec2, stuff into an '[ec2]' group.
        # Otherwise don't use a group header
        if 'ec2' in hostnames[0]:
            host_file.write('[ec2]\n')
        for host in hostnames:
            host_file.write('{0}\n'.format(host))

    return os.path.join(os.path.expanduser('~'), inv_file)


def run_playbook(hostnames, playbook, playbook_arguments, host_file=False):
    # If user passes in the optional arg host_file, then just use that one.
    if not host_file:                                                                                                                                                                                          
        host_file = create_inventory_file(hostnames)
    if not os.path.isfile(host_file):
        logging.critical('Host file does not exist. Make sure absolute path is correct.\nInventory: {0}'.format(host_file))
        raise RuntimeError('Host file does not exist')

    loader = DataLoader()
    inventory = InventoryManager(loader=loader, sources=host_file)
    variable_manager = VariableManager(loader=loader, inventory=inventory)

    # Add extra variables to use in playbook like so:
    # variable_manager.extra_vars = {'name': 'value'}
    if playbook_arguments:
        variable_manager.extra_vars = playbook_arguments

    Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection','module_path', 'forks', 'remote_user', 'become', 'become_method', 'become_user', 'verbosity', 'check', 'diff', 'ask_sudo_pass'])

    if 'superuser' in playbook_arguments:
        remote_user = playbook_arguments['superuser']
    else:
        remote_user = 'ec2-user'

    options = Options(listtags=None, listtasks=None, listhosts=None, syntax=None, connection='smart', module_path=None, forks=100, remote_user=remote_user,  become=None, become_method='sudo', become_user='root', verbosity=None, check=False, diff=False, ask_sudo_pass=None)

    pbex = PlaybookExecutor(playbooks=[playbook], inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords={})

    callback = ResultsCallback()
    pbex._tqm._stdout_callback = callback

    logging.print('Provisioning cluster with Ansible...')
    attempts = 3
    for i in range(attempts):
        try:
            pbex.run()
            failed = callback.failed
            if failed:
                logging.critical('Playbook failed!')
                raise RuntimeError('{0} tasks failed'.format(len(failed)))
            break
        except:
            if i < attempts - 1:
                logging.critical('Attempting to re-try playbook')
                continue
            else:
                raise

    logging.print('\nRemoving Ansible Inventory file {0}'.format(host_file))
    try:
        os.remove(host_file)
    except OSError:
        pass

However, when I test the above code using a playbook that is guaranteed to fail, it fails with the following traceback:

Creating Ansible host file: /home/someuser/ansible_hosts.18600
Provisioning cluster with Ansible...
Playbook failed!
Attempting to re-try playbook
Exception during setup; tearing down all created instances
Traceback (most recent call last):
  File "./manage_aws.py", line 486, in cmd_ec2_create
    manage_ansible.run_playbook(hostnames, playbook, playbook_arguments)
  File "/home/someuser/manage_ansible.py", line 88, in run_playbook
    break
  File "/usr/local/lib/python2.7/dist-packages/ansible/executor/playbook_executor.py", line 159, in run
    result = self._tqm.run(play=play)
  File "/usr/local/lib/python2.7/dist-packages/ansible/executor/task_queue_manager.py", line 296, in run
    strategy.cleanup()
  File "/usr/local/lib/python2.7/dist-packages/ansible/plugins/strategy/__init__.py", line 223, in cleanup
    self._final_q.put(_sentinel)
  File "/usr/lib/python2.7/multiprocessing/queues.py", line 100, in put
    assert not self._closed
AssertionError

You'll notice that the exception is properly caught inside of the calling script manage_aws.py ("Exception during setup; tearing down all created instances") and we go to tear down the instances. That's great, but I'd like to properly re-try the playbook before deciding to do so.

I'm no Python master, so if anyone has any tips, or has accomplished something similar, then I would very much appreciate your advice.

Thanks in advance!

Author:P Gilson,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/49823876/ansible-playbook-retry-logic-using-python-api
yy