Example #1
0
    def _run_play(self, play_source, host_vars):
        host_list = play_source['hosts']

        loader = dataloader.DataLoader()

        # FIXME(jpena): we need to behave differently if we are using
        # Ansible >= 2.4.0.0. Remove when only versions > 2.4 are supported
        if PRE_24_ANSIBLE:
            variable_manager = VariableManager()
            inventory_inst = Inventory(loader=loader,
                                       variable_manager=variable_manager,
                                       host_list=host_list)
            variable_manager.set_inventory(inventory_inst)
        else:
            inventory_inst = Inventory(loader=loader,
                                       sources=','.join(host_list) + ',')
            variable_manager = VariableManager(loader=loader,
                                               inventory=inventory_inst)

        for host, variables in host_vars.items():
            host_inst = inventory_inst.get_host(host)
            for var_name, value in variables.items():
                if value is not None:
                    variable_manager.set_host_variable(
                        host_inst, var_name, value)

        storage = []
        callback = MyCallback(storage)

        tqm = task_queue_manager.TaskQueueManager(
            inventory=inventory_inst,
            variable_manager=variable_manager,
            loader=loader,
            options=self.options,
            passwords=self.passwords,
            stdout_callback=callback,
        )

        # create play
        play_inst = play.Play().load(play_source,
                                     variable_manager=variable_manager,
                                     loader=loader)

        # actually run it
        try:
            tqm.run(play_inst)
        finally:
            tqm.cleanup()

        return storage
Example #2
0
def ansible_runner_24x(playbook_path,
                       module_path,
                       extra_var,
                       inventory_src,
                       console=True):
    loader = DataLoader()
    extra_var["ansible_python_interpreter"] = sys.executable
    variable_manager = VariableManager(loader=loader)
    variable_manager.extra_vars = extra_var
    inventory = Inventory(loader=loader, sources=[inventory_src])
    variable_manager.set_inventory(inventory)
    passwords = {}
    Options = namedtuple('Options', [
        'connection',
        'module_path',
        'forks',
        'become',
        'become_method',
        'become_user',
        'check',
        'diff',
        'listhosts',
        'listtasks',
        'listtags',
        'syntax',
        'remote_user',
        'private_key_file',
        'ssh_common_args',
        'ssh_extra_args',
        'sftp_extra_args',
        'scp_extra_args',
        'verbosity',
    ])
    options = Options(listtags=False,
                      listtasks=False,
                      listhosts=False,
                      syntax=False,
                      connection='local',
                      module_path=module_path,
                      forks=100,
                      remote_user='******',
                      private_key_file=None,
                      ssh_common_args=None,
                      ssh_extra_args=None,
                      sftp_extra_args=None,
                      scp_extra_args=None,
                      become=False,
                      become_method='sudo',
                      become_user='******',
                      verbosity=4,
                      diff=False,
                      check=False)
    pbex = PlaybookExecutor(playbooks=[playbook_path],
                            inventory=inventory,
                            variable_manager=variable_manager,
                            loader=loader,
                            options=options,
                            passwords=passwords)
    return pbex
Example #3
0
 def inventory(self):
     """
     getter function for inventory
     """
     loader = DataLoader()
     inventory = Inventory(loader, self.inventory_path)
     VariableManager(loader, inventory)
     return inventory
Example #4
0
def _load_defaults(inventory_path, extra_vars=None, tags=None, basedir=False):
    """Load common defaults data structures.

    For factorization purpose."""

    extra_vars = extra_vars or {}
    tags = tags or []
    loader = DataLoader()
    if basedir:
        loader.set_basedir(basedir)

    inventory = Inventory(loader=loader, sources=inventory_path)

    variable_manager = VariableManager(loader=loader, inventory=inventory)

    # seems mandatory to load group_vars variable
    if basedir:
        variable_manager.safe_basedir = True

    if extra_vars:
        variable_manager.extra_vars = extra_vars

    # NOTE(msimonin): The ansible api is "low level" in the
    # sense that we are redefining here all the default values
    # that are usually enforce by ansible called from the cli
    Options = namedtuple("Options", [
        "listtags", "listtasks", "listhosts", "syntax", "connection",
        "module_path", "forks", "private_key_file", "ssh_common_args",
        "ssh_extra_args", "sftp_extra_args", "scp_extra_args", "become",
        "become_method", "become_user", "remote_user", "verbosity", "check",
        "tags", "diff", "basedir"
    ])

    options = Options(listtags=False,
                      listtasks=False,
                      listhosts=False,
                      syntax=False,
                      connection="ssh",
                      module_path=None,
                      forks=100,
                      private_key_file=None,
                      ssh_common_args=None,
                      ssh_extra_args=None,
                      sftp_extra_args=None,
                      scp_extra_args=None,
                      become=None,
                      become_method="sudo",
                      become_user="******",
                      remote_user=None,
                      verbosity=2,
                      check=False,
                      tags=tags,
                      diff=None,
                      basedir=basedir)

    return inventory, variable_manager, loader, options
Example #5
0
def ansible_runner_24x(playbook_path,
                       extra_vars,
                       options,
                       inventory_src='localhost',
                       console=True):

    loader = DataLoader()
    variable_manager = VariableManager(loader=loader)
    variable_manager._extra_vars = extra_vars
    inventory = Inventory(loader=loader, sources=[inventory_src])
    variable_manager.set_inventory(inventory)
    passwords = {}

    pbex = PlaybookExecutor([playbook_path], inventory, variable_manager,
                            loader, options, passwords)
    return pbex
def ansible_runner_2x(playbook_path,
                      extra_vars,
                      options=None,
                      inventory_src='localhost',
                      console=True):

    variable_manager = VariableManager()
    loader = DataLoader()
    variable_manager.extra_vars = extra_vars
    inventory = Inventory(loader=loader,
                          variable_manager=variable_manager,
                          host_list=inventory_src)
    passwords = {}
    pbex = PlaybookExecutor([playbook_path], inventory, variable_manager,
                            loader, options, passwords)

    return pbex
Example #7
0
    def __init__(self, host, remote_user, connection, pipeline, profile,
                 playbooks_path='playbooks'):
        def set_extra_vars(pipeline=None, profile=None):
            extra_vars = dict()
            if pipeline:
                extra_vars['pipeline_label'] = pipeline['label']
                extra_vars['pipeline_url'] = pipeline['url']
                extra_vars['pipeline_cache_path'] = os.path.join(cache_dir,
                                                                 pipeline['label'])
            if profile:
                for k, v in profile.items():
                    extra_vars[k] = v

            return extra_vars

        loader = DataLoader()

        Options = namedtuple('Options',
                             ['connection', 'forks', 'remote_user', 'become',
                              'become_method', 'become_user', 'diff', 'check',
                              'listhosts', 'listtasks', 'listtags', 'syntax',
                              'module_path'])
        options = Options(connection=connection, forks=100, remote_user=remote_user,
                          become=None, become_method=None, become_user=None,
                          diff=False, check=False, listhosts=False,
                          listtasks=False, listtags=False, syntax=False,
                          module_path="")

        passwords = dict()

        inventory = Inventory(loader=loader, sources="{},".format(host))
        variable_manager = VariableManager(loader=loader, inventory=inventory)

        here = os.path.abspath(os.path.dirname(__file__))
        playbook_list = [os.path.join(here, playbooks_path,
                                      pipeline['playbook'])]

        variable_manager.extra_vars = set_extra_vars(pipeline=pipeline,
                                                     profile=profile)

        self.pbex = PlaybookExecutor(playbooks=playbook_list,
                                     inventory=inventory,
                                     variable_manager=variable_manager,
                                     loader=loader,
                                     options=options,
                                     passwords=passwords)
Example #8
0
#!/usr/bin/env python

import os
import sys
from collections import namedtuple

from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager
from ansible.inventory.manager import Inventory
from ansible.executor.playbook_executor import PlaybookExecutor

loader = DataLoader()

inventory = Inventory(loader=loader, sources='/home/slotlocker/hosts2')
variable_manager = VariableManager(loader=loader, inventory=inventory)
playbook_path = '/home/slotlocker/ls.yml'

if not os.path.exists(playbook_path):
    print '[INFO] The playbook does not exist'
    sys.exit()

Options = namedtuple('Options', [
    'listtags', 'listtasks', 'listhosts', 'syntax', 'connection',
    'module_path', 'forks', 'remote_user', 'private_key_file',
    'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args',
    'become', 'become_method', 'become_user', 'verbosity', 'check', 'diff'
])
options = Options(listtags=False,
                  listtasks=False,
                  listhosts=False,
                  syntax=False,
def setupRemoteInstance(InventoryPath):
    loader = DataLoader()
    inventory = Inventory(loader=loader, sources=InventoryPath)