Git Product home page Git Product logo

ansible-barrel's Introduction

Ansible-Barrel

A wrapper of Ansible Python API, provide easier to use invocation methods

Example of task(adhoc):

from ansible_util import TaskRunner

username = "root"
# Get os distribution of target host
task = dict(action=dict(module="setup", args="filter=ansible_distribution*"))

runner = TaskRunner(hosts, default_username, [task],
                    password, key_file)
runner.run()

Get results:

from ansible.plugins.callback import CallbackBase

# Define own result callback class
class TaskCallback(CallbackBase):

    def __init__(self, *args, **kwargs):
        super(TaskCallback, self).__init__(*args, **kwargs)
        self.RESULT = {}
        self.AUTH_FAILED_HOSTS = {}
        self.EXECUTION_FAILED_HOSTS = {}
        self.CONNECTION_FAILED_HOSTS = {}

    def v2_runner_on_ok(self, result, **kwargs):
        host = result._host
        self.RESULT[str(host)] = result._result

    def v2_runner_on_unreachable(self, result):
        host = result._host
        self.CONNECTION_FAILED_HOSTS[str(host)] = result._result
        if 'msg' in result._result:
            if result._result['msg'] == "Authentication failure.":
                self.AUTH_FAILED_HOSTS[str(host)] = result._result

    def v2_runner_on_failed(self, result, ignore_errors=False):
        host = result._host
        self.EXECUTION_FAILED_HOSTS[str(host)] = result._result
        print(result._result)


username = "root"
# Get os distribution of target host
task = dict(action=dict(module="setup", args="filter=ansible_distribution*"))

# Add custom callback for task runner:
runner = TaskRunner(hosts, default_username, [task],
                    password, key_file, callback=TaskCallback())
runner.run()

Example of playbook

from ansible_util import PlaybookResultsCollector

# init result collector
callback = PlaybookResultsCollector()

# Path of playbook file
playbook_path = "./test_playbook.yml"

runner = PlaybookRunner(hosts, account, playbook_path, result_callback=callback)
runner.run()

print(callback.results)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.