Exemplo n.º 1
0
    def run(self, terms, inject=None, **kwargs):
        terms = utils.listify_lookup_plugin_terms(terms, self.basedir, inject)

        if not isinstance(terms, list):
            raise errors.AnsibleError(
                "with_inventory_hostnames expects a list")
        return flatten(inventory.Inventory(self.host_list).list_hosts(terms))
Exemplo n.º 2
0
    def prepare_runner(self,
                       _playbook_path,
                       options={},
                       tasks=None,
                       _extra_vars={},
                       verbosity=3):
        _OPTIONS = self.__prepare_runner_metadata(options, tasks)
        _vb = _OPTIONS['verbosity']
        _inventory = inventory.Inventory(self.HOSTS_INI_FILE)
        stats = callbacks.AggregateStats()
        playbook_cb = callbacks.PlaybookCallbacks(verbose=_vb)
        runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=_vb)

        runner = playbook.PlayBook(
            playbook=_playbook_path,
            inventory=_inventory,
            extra_vars=_extra_vars,
            private_key_file=self.PRIVATE_KEY,
            #vault_password=vaultpass,
            only_tags=tasks,
            stats=stats,
            callbacks=playbook_cb,
            runner_callbacks=runner_cb)

        return runner
Exemplo n.º 3
0
def scan_inventory(path):
    """
    Given an infrasim inventory path, analyze all host and group in dict.
    """
    dict_group = {}
    for one_type in node_type:
        dict_group[one_type] = []
    global ssh_username
    global ssh_password
    global hosts

    variable_manager = VariableManager()
    loader = DataLoader()

    inv = inventory.Inventory(loader=loader, variable_manager=variable_manager, host_list=path)
    groups = inv.get_groups()
    for g in groups:
        group = groups[g]
        if group.name in node_type:
            hosts = group.get_hosts()
            for host in hosts:
                dict_group[group.name].append(host.name)
        elif group.name == full_group:
            vars = group.get_vars()
            ssh_username = vars["ansible_become_user"]
            ssh_password = vars["ansible_become_pass"]

    hosts = dict_group
    return
Exemplo n.º 4
0
def HostsList():
    """

    :rtype: object
    """
    hosts = []
    a = inventory.Inventory()
    a= AnsibleV1Inv.get_inv()
    data = a.list_hosts()
    # data example
    # ['192.168.233.129', '192.168.233.131', '192.168.0.211']
    #data = a.list_hosts()
    # groups example
    # {'ungrouped': [],
    #  'all': ['192.168.233.129', '192.168.233.131', '192.168.0.211'],
    #  'vmware': ['192.168.233.129', '192.168.233.131'],
    #  'atom': ['192.168.0.211']}
    groups = a.groups_list()
    for i in range(len(data)):
        group = search(groups,data[i])
        try:
            host = {
                'id': hosts[-1]['id'] + 1,
                'host': str(data[i]),
                'groups': group
            }
        except:
            host = {
                'id': 1,
                'host': data[i],
                'groups': group
            }
        hosts.append(host)
    return hosts
Exemplo n.º 5
0
    def run_module(self, module_name='ping', module_args=None, hosts="all",
                   inventory_file=None, **kwargs):

        if not module_args:
            check_raw = module_name in ('command', 'win_command', 'shell',
                                        'win_shell', 'script', 'raw')
            module_args = parse_kv(constants.DEFAULT_MODULE_ARGS, check_raw)

        conn_pass = None
        if 'conn_pass' in kwargs:
            conn_pass = kwargs['conn_pass']

        become_pass = None
        if 'become_pass' in kwargs:
            become_pass = kwargs['become_pass']

        passwords = {'conn_pass': conn_pass, 'become_pass': become_pass}

        options = self._build_opt_dict(inventory_file, **kwargs)

        variable_manager = vars.VariableManager()
        loader = dataloader.DataLoader()
        variable_manager.extra_vars = options.extra_vars

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

        play_ds = self._play_ds(hosts, module_name, module_args)
        play_obj = play.Play().load(play_ds, variable_manager=variable_manager,
                                    loader=loader)

        try:
            tqm = task_queue_manager.TaskQueueManager(
                inventory=ansible_inv,
                variable_manager=variable_manager,
                loader=loader,
                options=options,
                passwords=passwords,
                stdout_callback='minimal',
                run_additional_callbacks=True
            )

            # There is no public API for adding callbacks, hence we use a
            # private property to add callbacks
            tqm._callback_plugins.extend(self._callbacks)

            result = tqm.run(play_obj)
        finally:
            if tqm:
                tqm.cleanup()
            if loader:
                loader.cleanup_all_tmp_files()

        stats = tqm._stats
        result = self._process_stats(stats)
        return result
Exemplo n.º 6
0
    def run_playbook(self, playbook_file, inventory_file=None, **kwargs):
        reload(constants)

        if not os.path.isfile(playbook_file):
            raise exceptions.FileNotFound(name=playbook_file)

        if inventory_file is None:
            inventory_file = self.inventory_file

        LOG.debug('Running with inventory file: %s', inventory_file)
        LOG.debug('Running with playbook file: %s', playbook_file)

        conn_pass = None
        if 'conn_pass' in kwargs:
            conn_pass = kwargs['conn_pass']

        become_pass = None
        if 'become_pass' in kwargs:
            become_pass = kwargs['become_pass']

        passwords = {'conn_pass': conn_pass, 'become_pass': become_pass}

        playbooks = [playbook_file]

        options = self._build_opt_dict(inventory_file, **kwargs)

        variable_manager = vars.VariableManager()
        loader = dataloader.DataLoader()
        options.extra_vars = {six.u(key): six.u(value)
                              for key, value in options.extra_vars.items()}
        variable_manager.extra_vars = options.extra_vars

        ansible_inv = inventory.Inventory(loader=loader,
                                          variable_manager=variable_manager,
                                          host_list=options.inventory)
        ansible_inv.set_playbook_basedir(os.path.dirname(playbook_file))
        variable_manager.set_inventory(ansible_inv)
        ansible_inv.subset(options.subset)

        pbex = playbook_executor.PlaybookExecutor(
            playbooks=playbooks,
            inventory=ansible_inv,
            variable_manager=variable_manager,
            loader=loader,
            options=options,
            passwords=passwords)
        self.tqm = pbex._tqm
        errors_callback = ErrorsCallback()
        self.add_callback(errors_callback)
        # There is no public API for adding callbacks, hence we use a private
        # property to add callbacks
        pbex._tqm._callback_plugins.extend(self._callbacks)

        status = pbex.run()
        stats = pbex._tqm._stats
        failed_results = errors_callback.failed_results
        result = self._process_stats(stats, failed_results)
        return result
Exemplo n.º 7
0
def ssh_to_host(hostname, remote_command=None):
    """ Compose cmd string of ssh and execute

        Uses Ansible to parse inventory file, gets ssh connection options
        :param hostname: str. Hostname from inventory
    """

    workspace_manager = CoreServices.workspace_manager()
    workspace = workspace_manager.get_active_workspace()
    if workspace is None:
        raise exceptions.IRNoActiveWorkspaceFound()
    inventory_file = workspace.inventory

    invent = inventory.Inventory(DataLoader(),
                                 VariableManager(),
                                 host_list=inventory_file)

    host = invent.get_host(hostname)
    if host is None:
        raise exceptions.IRSshException(
            "Host {} is not in inventory {}".format(hostname, inventory_file))

    if _get_magic_var(host, "connection") == "local":
        raise exceptions.IRSshException("Only ssh transport acceptable.")

    cmd = " ".join([
        "ssh {priv_key} {comm_args}", "{extra_args} -p {port} -t {user}@{host}"
    ])

    cmd_fields = {}
    cmd_fields["user"] = _get_magic_var(host, "remote_user", default="root")
    cmd_fields["port"] = _get_magic_var(host, "port", default=22)
    cmd_fields["host"] = _get_magic_var(host, "remote_addr")

    priv_key = _get_magic_var(host, "private_key_file")
    # NOTE(yfried):
    # ssh client needs key to be in the directory you're running one from
    # ('ssh -i id_rsa ...') or to be provided by absolute path.
    # assume paths are relative to inventory file.
    abspath = os.path.join(os.path.abspath(os.path.dirname(inventory_file)),
                           priv_key)
    priv_key = abspath if os.path.exists(abspath) else priv_key

    cmd_fields["priv_key"] = "-i {}".format(priv_key if priv_key else "")

    cmd_fields["comm_args"] = _get_magic_var(host, "ssh_common_args")
    cmd_fields["extra_args"] = _get_magic_var(host, "ssh_extra_args")

    LOG.debug("Establishing ssh connection to {}".format(cmd_fields["host"]))

    compiled_cmd = cmd.format(**cmd_fields)
    if remote_command is not None:
        compiled_cmd = " ".join([compiled_cmd, '"{}"'.format(remote_command)])

    result = os.WEXITSTATUS(os.system(compiled_cmd))
    LOG.debug("Connection to {} closed".format(cmd_fields["host"]))
    return result
Exemplo n.º 8
0
def get_inventory(loader, variable_manager, inventory_file):
    try:
        # Ansible 2.4
        from ansible.inventory.manager import InventoryManager
        return InventoryManager(loader=loader, sources=inventory_file)
    except:
        # Ansible 2.3
        from ansible import inventory
        inventory.HOSTS_PATTERNS_CACHE = {}  # clean up previous cached hosts
        return inventory.Inventory(loader=loader,
                                   variable_manager=variable_manager,
                                   host_list=inventory_file)
Exemplo n.º 9
0
def run_playbook(playbook_file_name, inventory_file_name):
    stats = callbacks.AggregateStats()
    playbook_cb = callbacks.PlaybookCallbacks(verbose=0)
    runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=0)

    pb = ansible.playbook.PlayBook(
        playbook_file_name,
        inventory=inventory.Inventory(inventory_file_name),
        callbacks=playbook_cb,
        runner_callbacks=runner_cb,
        stats=stats)

    pb.run()
Exemplo n.º 10
0
def set_group(name, host):
    a = inventory.Inventory()
    groups = a.groups_list()
    group_exist = False
    for i in groups:
        if name in i:
            group_exist=True
    if group_exist is True:
        inv_group = a.get_group(name)
        inv_group.add_host(host)
    else:
        inv_group = inventory.group.Group(name = name)
    return inv_group
Exemplo n.º 11
0
def ssh_to_host(hostname, remote_command=None):
    """ Compose cmd string of ssh and execute

        Uses Ansible to parse inventory file, gets ssh connection options
        :param hostname: str. Hostname from inventory
    """

    workspace_manager = CoreServices.workspace_manager()
    workspace = workspace_manager.get_active_workspace()
    if workspace is None:
        raise exceptions.IRNoActiveWorkspaceFound()
    inventory_file = workspace.inventory

    invent = inventory.Inventory(DataLoader(),
                                 VariableManager(),
                                 host_list=inventory_file)

    host = invent.get_host(hostname)
    if host is None:
        raise exceptions.IRSshException(
            "Host {} is not in inventory {}".format(hostname, inventory_file))

    if _get_magic_var(host, "connection") == "local":
        raise exceptions.IRSshException("Only ssh transport acceptable.")

    cmd = " ".join([
        "ssh {priv_key} {comm_args}", "{extra_args} -p {port} -t {user}@{host}"
    ])

    cmd_fields = {}
    cmd_fields["user"] = _get_magic_var(host, "remote_user", default="root")
    cmd_fields["port"] = _get_magic_var(host, "port", default=22)
    cmd_fields["host"] = _get_magic_var(host, "remote_addr")

    priv_key = _get_magic_var(host, "private_key_file")
    cmd_fields["priv_key"] = "-i {}".format(priv_key if priv_key else "")

    cmd_fields["comm_args"] = _get_magic_var(host, "ssh_common_args")
    cmd_fields["extra_args"] = _get_magic_var(host, "ssh_extra_args")

    LOG.debug("Establishing ssh connection to {}".format(cmd_fields["host"]))

    compiled_cmd = cmd.format(**cmd_fields)
    if remote_command is not None:
        compiled_cmd = " ".join([compiled_cmd, '"{}"'.format(remote_command)])

    os.system(compiled_cmd)

    LOG.debug("Connection to {} closed".format(cmd_fields["host"]))
Exemplo n.º 12
0
def get_inventory_info(inv_file=inventory_source):
    """
    """
    print(os.getcwd())
    if not os.path.isfile(inv_file):
        return
    inv = inventory.Inventory(inv_file)

    ret_dict = OrderedDict()
    for grp in inv.get_groups():
        host_list = list()
        for host in grp.get_hosts():
            host_list.append(host.name)

        ret_dict[grp.name] = host_list

    return ret_dict
Exemplo n.º 13
0
    def _get_inventory(self, workspace_name=None):
        """Returns Inventory object for the provided workspace.
           Use active workspace as default

           :param workspace_name: workspace name to list nodes from.
        """
        workspace = self.get(
            workspace_name) if workspace_name else self.get_active_workspace()

        if workspace is None:
            if workspace_name is None:
                raise exceptions.IRNoActiveWorkspaceFound()
            else:
                raise exceptions.IRWorkspaceMissing(workspace=workspace_name)

        return inventory.Inventory(DataLoader(),
                                   VariableManager(),
                                   host_list=workspace.inventory)
Exemplo n.º 14
0
def run_ansible():
    vaultpass = "******"
    I = inventory.Inventory("hosts.ini")

    parsed_extra_vars = { 'tags' : ['two'],
                          'date': 2
    }
    stats = callbacks.AggregateStats()
    playbook_cb = callbacks.PlaybookCallbacks(verbose=3)

    runner_cb = callbacks.PlaybookRunnerCallbacks(stats,
                                                  verbose=3)
    pb = playbook.PlayBook(
        playbook='sample_playbook.yaml',
        inventory=I,
        extra_vars=parsed_extra_vars,
        #private_key_file="/path/to/key.pem",
        #vault_password=vaultpass,
        stats=stats,
        callbacks=playbook_cb,
        runner_callbacks=runner_cb
    )
    pb.run()
    
    hosts = sorted(pb.stats.processed.keys())
    
    failed_hosts = []
    unreachable_hosts = []
    for h in hosts:
        t = pb.stats.summarize(h)
        if t['failures'] > 0:
            failed_hosts.append(h)
        if t['unreachable'] > 0:
            unreachable_hosts.append(h)

    print("failed hosts: ", failed_hosts)
    print("unreachable hosts: ", unreachable_hosts)
    
    retries = failed_hosts + unreachable_hosts
    print("retries:", retries)
    if len(retries) > 0:
        return 1
    return 0
Exemplo n.º 15
0
    def _run_play(self, play_source, host_vars):
        host_list = play_source['hosts']

        loader = dataloader.DataLoader()
        variable_manager = VariableManager()
        inventory_inst = inventory.Inventory(loader=loader,
                                             variable_manager=variable_manager,
                                             host_list=host_list)
        variable_manager.set_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
Exemplo n.º 16
0
def GroupsList():
    """

    :rtype: object
    """
    groups = []
    a = inventory.Inventory()
    data = a.groups_list()
    for i in data:
        try:
            group = {
                'id': groups[-1]['id'] + 1,
                'group': str(i),
                'hosts': str(data[i]),
            }
        except:
            group = {
                'id': 1,
                'group': i,
                'hosts': data[i],
            }
        groups.append(group)
    return groups
Exemplo n.º 17
0
    def node_list(self, workspace_name=None):
        """Lists nodes and connection types from workspace's inventory

           nodes with connection type 'local' are skipped
           :param workspace_name: workspace name to list nodes from.
                                Use active workspace as default
        """

        workspace = self.get(
            workspace_name) if workspace_name else self.get_active_workspace()

        if workspace is None:
            if workspace_name is None:
                raise exceptions.IRNoActiveWorkspaceFound()
            else:
                raise exceptions.IRWorkspaceMissing(workspace=workspace_name)

        invent = inventory.Inventory(DataLoader(),
                                     VariableManager(),
                                     host_list=workspace.inventory)
        hosts = invent.get_hosts()
        return [(host.name, host.address) for host in hosts
                if host.vars.get("ansible_connection") != "local"]
Exemplo n.º 18
0
    def __init__(self, playbook, dci_context, options=None, verbosity=3):

        if options is None:
            self._options = Options()
            self._options.verbosity = verbosity
            self._options.connection = 'ssh'
            self._options.become = True
            self._options.become_method = 'sudo'
            self._options.become_user = '******'

        self._loader = dataloader.DataLoader()
        self._variable_manager = vars.VariableManager()

        task_queue_manager.display.verbosity = verbosity
        callback.global_display.verbosity = verbosity

        self._inventory = inventory.Inventory(
            loader=self._loader,
            variable_manager=self._variable_manager,
            host_list='/etc/ansible/hosts')
        self._variable_manager.set_inventory(self._inventory)

        # Instantiate our Callback plugin
        self._results_callback = DciCallback(dci_context=dci_context)

        self._playbook = Playbook.load(playbook,
                                       variable_manager=self._variable_manager,
                                       loader=self._loader)

        self._pbex = playbook_executor.PlaybookExecutor(
            playbooks=[playbook],
            inventory=self._inventory,
            variable_manager=self._variable_manager,
            loader=self._loader,
            options=self._options,
            passwords={})
Exemplo n.º 19
0
def GroupVarsList(group):
    """
    lists variables for group

    :rtype: object
    """
    vars = []
    a = inventory.Inventory()
    group_vars = a.get_group(group).get_variables()
    for i in range(len(vars)):
        try:
            host = {
                'id': len(vars)[-1]['id'] + 1,
                'group': str(group[i]),
                'groups': group
            }
        except:
            host = {
                'id': 1,
                'group': str(group[i]),
                'groups': group
            }
        vars.append(host)
    return vars
Exemplo n.º 20
0
    def _run_play(self, play_source):
        host_list = play_source['hosts']

        loader = dataloader.DataLoader()
        variable_manager = VariableManager()
        inventory_inst = inventory.Inventory(loader=loader,
                                             variable_manager=variable_manager,
                                             host_list=host_list)
        variable_manager.set_inventory(inventory_inst)
        passwords = dict(vault_pass='******')

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

        storage = []
        callback = MyCallback(storage)

        # actually run it
        tqm = None
        try:
            tqm = task_queue_manager.TaskQueueManager(
                inventory=inventory_inst,
                variable_manager=variable_manager,
                loader=loader,
                options=self.options,
                passwords=passwords,
                stdout_callback=callback,
            )
            tqm.run(play_inst)
        finally:
            if tqm is not None:
                tqm.cleanup()

        return storage
Exemplo n.º 21
0
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Eisen is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Eisen.  If not, see <http://www.gnu.org/licenses/>.

# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
from ansible import inventory

__metaclass__ = type
__version__ = '0.0.1'
__author__ = 'Eisen'

# Contain the async executed tasks result
tasks_result = {}
tasks_package = {}

recepies_result = {}
recepies_package = {}

result2Db = {}

# contain the dynamic inventory
dynamic_inventory = inventory.Inventory()
Exemplo n.º 22
0
def human_log(self, res, host):
    inventory_manager = inventory.Inventory()
    result = inventory_manager.get_variables(host)
    print result['ec2_tag_Name']
Exemplo n.º 23
0
'''
Test bundled filters
'''

import os.path
import unittest, tempfile, shutil
from ansible import playbook, inventory, callbacks
import ansible.runner.filter_plugins.core
import ansible.runner.filter_plugins.mathstuff

INVENTORY = inventory.Inventory(['localhost'])

BOOK = '''
- hosts: localhost
  vars:
    var: { a: [1,2,3] }
  tasks:
  - template: src=%s dest=%s
'''

SRC = '''
-
{{ var|to_json }}
-
{{ var|to_nice_json }}
-
{{ var|to_yaml }}
-
{{ var|to_nice_yaml }}
'''
Exemplo n.º 24
0
from ansible import inventory
from ansible import callbacks
from ansible import utils

utils.VERBOSITY = 3

ANSIBLE_HOSTS = 'local'
INVENTORY_FILE = 'servers.yml'
PLAYBOOK = 'playbook.yml'
CONFIG_RECIPES_FILE = 'script-config.yml'

playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
stats = callbacks.AggregateStats()
runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)

inventory = inventory.Inventory(INVENTORY_FILE)
inventory.subset(ANSIBLE_HOSTS)

inventory.set_playbook_basedir(os.path.dirname(PLAYBOOK))
hosts = inventory.list_hosts(ANSIBLE_HOSTS)

if len(hosts) == 0:
    raise Exception(
        'Could not find any host to match "%s" in the inventory "%s" file' %
        (ANSIBLE_HOSTS, INVENTORY_FILE))

stream = open(CONFIG_RECIPES_FILE, 'r')
configurations = yaml.load(stream)

for recipe in configurations:
    print recipe['only_tags']