Example #1
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

    from ansible.parsing.dataloader import DataLoader
    from ansible.inventory.manager import InventoryManager
    invent = InventoryManager(DataLoader(), sources=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
def get_ansible_host_ip():
    loader = DataLoader()
    inventory = InventoryManager(loader=loader, sources='hosts')
    variable_manager = VariableManager(loader=loader, inventory=inventory)
    hostnames = []
    for host in inventory.get_hosts():
        hostnames.append(variable_manager.get_vars(host=host))
    ip = ' '.join([str(i['ansible_host']) for i in hostnames])
    return str(ip)
Example #3
0
    def test_flush_cache(self):
        cli = PlaybookCLI(args=["ansible-playbook", "--flush-cache", "foobar.yml"])
        cli.parse()
        self.assertTrue(cli.options.flush_cache)

        variable_manager = VariableManager()
        fake_loader = DictDataLoader({'foobar.yml': ""})
        inventory = InventoryManager(loader=fake_loader, sources='testhost,')

        variable_manager.set_host_facts(inventory.get_host('testhost'), {'canary': True})
        self.assertTrue('testhost' in variable_manager._fact_cache)

        cli._flush_cache(inventory, variable_manager)
        self.assertFalse('testhost' in variable_manager._fact_cache)
Example #4
0
    def __init__(self, playbook):
        self.loader = DataLoader()
        self.inventory = InventoryManager(loader=self.loader, sources=None)
        self.variable_manager = VariableManager(self.loader,self.inventory)
        self.playbook = playbook

        self.sender_group = 'sender'
        self.inventory.add_group(self.sender_group)

        self.receiver_group = 'receiver'
        self.inventory.add_group(self.receiver_group)

        self.master_group = 'master'
        self.inventory.add_group(self.master_group)
Example #5
0
    def __init__(self):
        self.Options = namedtuple('Options',
                     ['connection',
                      'remote_user',
                      'ask_sudo_pass',
                      'verbosity',
                      'ack_pass',
                      'module_path',
                      'forks',
                      'become',
                      'become_method',
                      'become_user',
                      'check',
                      'listhosts',
                      'listtasks',
                      'listtags',
                      'syntax',
                      'sudo_user',
                      'sudo',
                      'diff'])
        self.options = self.Options(connection='smart',
                       remote_user=None,
                       ack_pass=None,
                       sudo_user=None,
                       forks=5,
                       sudo=None,
                       ask_sudo_pass=False,
                       verbosity=5,
                       module_path=None,
                       become=None,
                       become_method=None,
                       become_user=None,
                       check=False,
                       diff=False,
                       listhosts=None,
                       listtasks=None,
                       listtags=None,
                       syntax=None)

        self.loader = DataLoader()
        self.inventory = InventoryManager(loader=self.loader, sources=['/opt/myops/myansible/hosts'])
        self.variable_manager = VariableManager(loader=self.loader, inventory=self.inventory)
        self.callback = ModelResultsCollector()
        self.passwords = dict()
Example #6
0
])
options = Options(connection='smart',
                  module_path=[],
                  forks=10,
                  become=None,
                  become_method=None,
                  become_user=None,
                  check=False,
                  diff=False)

loader = DataLoader()
passwords = {}

results_callback = ResultCallback()

inventory = InventoryManager(loader=loader, sources='hosts')  # 剧本的位置

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

# ansible all -i etc/hosts -m setup
play_source = {
    'name': "collect server info",
    'hosts': 'all',  # 在哪些主机上执行
    'gather_facts': 'no',
    'tasks': [  # 执行的任务列表
        {
            'name': 'fact',  # 任务名称
            'setup': ''  # 执行任务模块
        }
    ]
}
Example #7
0
class MyInventory():
    '''
    resource = [{'hostid': '1231', 'hostname': 'h1', 'hostip': '1.1.1.1'},
                {'hostid': '2345', 'hostname': 'h2', 'hostip': '2.2.2.2'},
                ]
    resource = {'groupname1': {
        'hosts': [
            {'hostid': '1231', 'hostname': 'h1', 'hostip': '1.1.1.1'},
            {'hostid': '2231', 'hostname': 'h2', 'hostip': '1.1.1.2'},
                 ],
        'groupvars': {"k1":"v1"}
                              },
                'groupname2': {'hosts': [], 'groupvars': {}},
                              }
    '''

    # edit ori code  ansible/inventory/manage.pay line215  try if C.InventoryManager_PARSE_NOSOURCE:pass
    constants.InventoryManager_PARSE_NOSOURCE = True

    def __init__(self, resource):
        self.resource = resource
        self.loader = DataLoader()
        # self.inventory=InventoryManager(loader=self.loader,sources=['/etc/ansible/hosts'])
        self.inventory = InventoryManager(loader=self.loader)
        self.variable_manager = VariableManager(loader=self.loader,
                                                inventory=self.inventory)
        self._parse(self.resource)

    def _parse(self, resource):
        if isinstance(resource, list):
            self._addGroupHosts(self.resource)
        elif isinstance(resource, dict):
            # logging.info('parsing resuorce: %s'%(self.resource))
            for groupname, hosts_and_groupvars in self.resource.items():
                print("[1] groupname: %s |hostsandvars: %s" %
                      (groupname, hosts_and_groupvars))  # debug [1]
                self._addGroupHosts(hosts_and_groupvars.get('hosts'),
                                    groupname,
                                    hosts_and_groupvars.get('groupvars'))

        else:
            logging.error('resource error ,need dict or list')

    def _addGroupHosts(self, hosts, groupname='default', groupvars=None):
        self.inventory.add_group(group=groupname)
        group = Group(groupname)
        if groupvars:
            for k, v in groupvars.items():
                group.set_variable(k, v)

        # hosts=[{'hostid':'123','hostname':'h1','hostip':'192.168.188.20'}
        for host in hosts:
            hostid = host.get('hostid')
            hostname = host.get('hostname')
            hostip = host.get('hostip')
            username = host.get('username')
            password = host.get('password')
            port = host.get('port', 22)
            sshkey = host.get('sshkey')
            if hostname:
                self.inventory.add_host(
                    host=hostname, group=groupname
                )  # by default, indentify by hostname and need
                hostobj = self.inventory.get_host(
                    hostname=hostname)  # add host= , get hostname=

                self.variable_manager.set_host_variable(
                    host=hostobj, varname='ansible_ssh_host', value=hostip)
                self.variable_manager.set_host_variable(
                    host=hostobj, varname='ansible_ssh_port', value=port)
                self.variable_manager.set_host_variable(
                    host=hostobj, varname='ansible_ssh_user', value=username)
                self.variable_manager.set_host_variable(
                    host=hostobj, varname='ansible_ssh_pass', value=password)
                self.variable_manager.set_host_variable(
                    host=hostobj,
                    varname='ansible_ssh_private_key_file',
                    value=sshkey)

                # TODO: other vars such as become-method-user-pass
                #hostobj.set_variable('ansible_ssh_port',port)
                for k, v in host.items():
                    if k not in [
                            'hostip', 'port', 'username', 'password', 'sshkey'
                    ]:
                        hostobj.set_variable(k, v)
            else:
                logging.warning('resource error:cant get hostname from | %s' %
                                resource)

    def testcase(self):
        print(self.inventory.get_groups_dict())
        host = self.inventory.get_host(hostname='h1')
        print(self.variable_manager.get_vars(host=host))
Example #8
0
    def __init__(self, hosts, extra_vars=None, timeout=None):
        Options = namedtuple('Options', [
            'listtags',
            'listtasks',
            'listhosts',
            'syntax',
            'connection',
            'module_path',
            'forks',
            'remote_user',
            'private_key_file',
            'timeout',
            'ssh_common_args',
            'ssh_extra_args',
            'sftp_extra_args',
            'scp_extra_args',
            'become',
            'become_method',
            'become_user',
            'verbosity',
            'check',
            'extra_vars',
            'playbook_path',
            'passwords',
            'diff',
            'gathering',
            'remote_tmp',
        ])
        self.hosts = hosts
        self._validate()
        self.hosts_file = None
        self.timeout = 100 if not timeout else timeout
        self.options = Options(listtags=False,
                               listtasks=False,
                               listhosts=False,
                               syntax=False,
                               timeout=self.timeout,
                               connection='ssh',
                               module_path='',
                               forks=10,
                               remote_user='******',
                               private_key_file='/root/.ssh/id_rsa',
                               ssh_common_args="",
                               ssh_extra_args="",
                               sftp_extra_args="",
                               scp_extra_args="",
                               become=None,
                               become_method=None,
                               become_user=None,
                               verbosity=None,
                               extra_vars=[],
                               check=False,
                               playbook_path='',
                               passwords=None,
                               diff=False,
                               gathering='implicit',
                               remote_tmp='/tmp/.ansible')
        self.loader = DataLoader()
        self.inventory = InventoryManager(loader=self.loader,
                                          sources=[','.join(self.hosts) + ','])
        self.variable_manager = VariableManager(loader=self.loader,
                                                inventory=self.inventory)
        if extra_vars:
            self.variable_manager.extra_vars = extra_vars

        logger.info(self.options)
Example #9
0
                          become_method=None,
                          become_user=None,
                          check=False,
                          diff=False,
                          forks=2)

# initialize needed objects
loader = DataLoader(
)  # Takes care of finding and reading yaml, json and ini files
passwords = dict(vault_pass='******')

# Instantiate our ResultCallback for handling results as they come in. Ansible expects this to be one of its main display outlets
results_callback = ResultCallback()

# create inventory, use path to host config file as source or hosts in a comma separated string
inventory = InventoryManager(loader=loader, sources=['10.20.40.20'])

# variable manager takes care of merging all the different sources to give you a unifed view of variables available in each context
variable_manager = VariableManager(loader=loader, inventory=inventory)

# Create play object, playbook objects use .load instead of init or new methods,
# this will also automatically create the task objects from the info provided in play_source

playbook = PlaybookExecutor(
    playbooks=['/Users/lei/ops/github/cmdb/ansible_api/test1.yaml'],
    inventory=inventory,
    variable_manager=variable_manager,
    loader=loader,
    options=options,
    passwords=passwords)
playbook.run()
    def __execute_module(self,
                         module_name=None,
                         module_args=None,
                         persist_files=False,
                         delete_remote_tmp=None,
                         wrap_async=False,
                         run_on_ca_host=False,
                         ignore_changed=False,
                         ignore_failed=False):
        if run_on_ca_host:
            host = InventoryManager(DataLoader(),
                                    context.CLIARGS['inventory']).get_host(
                                        self._var("ca_host"))

            task = self._task.copy()

            task_data = self._var("ca_host_options")
            task_data.update({
                'name':
                "mgssl ca_host task: {0}".format(module_name),
                'action':
                module_name,
                'args':
                module_args,
                'delegate_to':
                self._var("ca_host")
            })

            task.load_data(task_data)

            task_vars = self._task_vars.copy()
            task_vars.update(self._task.get_variable_manager().get_vars(
                host=host, task=task))

            executor_result = TaskExecutor(host, task, task_vars,
                                           self._play_context, None,
                                           self._loader,
                                           self._shared_loader_obj, None)

            # Dirty fix for mitogen compatibility
            # Mitogen somehow puts a task global connection binding object in each connection that gets created
            # during the lifetime of a task. That usually happens on the beginning of a task, but here, we create
            # a new task executor within a task and that also creates a new connection for local running tasks.
            # After execution the connections get closed, but the close function also closes and removes the parent
            # tasks binding object. Now all future connections will fail.
            #
            # Solution: Overwrite the close method and only call the necessary close methods except the one that closes
            # the binding object
            if self._is_mitogen:
                get_connection_method = executor_result._get_connection

                def get_connection(cvars, templar):
                    c = get_connection_method(cvars, templar)
                    c.close = lambda: (c._put_connection(), None)
                    return c

                executor_result._get_connection = get_connection

            ret = executor_result.run()

            # Reset the close method
            if self._is_mitogen:
                executor_result._get_connection = get_connection_method

        else:
            if self._shared_loader_obj.action_loader.has_plugin(
                    module_name, None):
                task = self._task.copy()
                task.load_data({
                    'action': module_name,
                    'args': module_args,
                })
                handler = self._shared_loader_obj.action_loader.get(
                    module_name,
                    task=task,
                    connection=self._connection,
                    play_context=self._play_context.
                    set_task_and_variable_override(task, {}, self._templar),
                    loader=self._loader,
                    templar=self._templar,
                    shared_loader_obj=self._shared_loader_obj,
                    collection_list=None)

                ret = handler.run(None, self._task_vars)

            else:
                ret = self._execute_module(module_name, module_args, None,
                                           self._task_vars, persist_files,
                                           delete_remote_tmp, wrap_async)

        self._check_result(module_name, run_on_ca_host, ret, ignore_changed,
                           ignore_failed)
        return ret
Example #11
0
class AnsibleTest(object):
    def __init__(self):
        self.loader = None
        self.inventory = None
        self.vars_manager = None
        self.play = None
        self.role = None

    def load(self, inventory_dir, inventory_name, playbook_dir, playbook_name,
             play_name, role_name):
        self.loader = DataLoader()
        self.loader.set_basedir(playbook_dir)
        self.inventory = InventoryManager(loader=self.loader,
                                          sources=os.path.join(
                                              inventory_dir, inventory_name))
        self.vars_manager = VariableManager(loader=self.loader,
                                            inventory=self.inventory)
        self.play = self.get_play_by_name(playbook_name, play_name)
        self.role = self.get_role_by_name(self.play, role_name)

    def get_play_by_name(self, playbook_name, play_name):
        playbook = Playbook.load(os.path.join(self.loader.get_basedir(),
                                              playbook_name),
                                 variable_manager=self.vars_manager,
                                 loader=self.loader)
        plays = playbook.get_plays()

        play = None
        for p in plays:
            if p.get_name() == play_name:
                play = p
                break

        if play is None:
            raise Exception("no play with name %s found in plays: %s" %
                            (play_name, plays))

        return play

    def get_role_by_name(self, play, role_name):
        roles = play.get_roles()
        for r in roles:
            if r.get_name() == role_name:
                return r
        raise Exception("no role with name %s found in roles: %s" %
                        (role_name, roles))

    def read_role_template(self, template_name):
        path = os.path.join(self.role._role_path, "templates", template_name)
        with open(path, 'r') as f:
            return f.read()

    def get_host_vars(self, host_name):
        all_vars = self.vars_manager.get_vars(
            play=self.play, host=self.inventory.get_host(host_name))
        host_vars = HostVars(
            inventory=self.inventory,
            variable_manager=self.vars_manager,
            loader=self.loader,
        )
        all_vars.update(dict(hostvars=host_vars))
        return all_vars
Example #12
0
def main(args):
    host_list = []
    for inv in args.get("INVENTORY"):
        if not os.path.exists(inv):
            logger.error("Inventory file(s) not exist.")
            sys.exit(1)
        if os.path.isdir(inv):
            host_list.append(inv + "/*")
        else:
            host_list.append(inv)

    logger.debug(host_list)
    subprocess.call("cat %s > %s" % (" ".join(host_list), tmp_path),
                    shell=True)

    header_path = args.get("--header")
    if os.path.exists(header_path):
        header = open(header_path, "r").read()
    else:
        header = None
        logger.warn("No custom header file found.")

    indent = " " * int(args.get("--indent"))

    logger.debug(header)
    output = "" + header if header else ""
    loader = DataLoader()
    inventory = InventoryManager(loader=loader, sources=tmp_path)
    variable_manager = VariableManager(loader=loader, inventory=inventory)

    for host in inventory.get_hosts():
        host_all_vars = host.get_vars()

        for group in host.get_groups():
            host_all_vars.update(group.get_vars())

        output += "Host %s\n" % host.get_name()
        output += "%sHostname %s\n" % (indent, host_all_vars["ansible_host"])
        custom_user = False

        for user in ("ansible_ssh_user", "ansible_user"):
            if user in host_all_vars:
                output += "%sUser %s\n" % (indent, host_all_vars[user])
                custom_user = True
        if not custom_user:
            output += "%sUser root\n" % indent

        for port in ("ansible_ssh_port", "ansible_port"):
            if port in host_all_vars:
                output += "%sPort %d\n" % (indent, host_all_vars[port])

        ssh_key = "ansible_ssh_private_key_file"
        if ssh_key in host_all_vars:
            output += "%sIdentityFile %s\n" % (indent, host_all_vars[ssh_key])

        output += "\n"

    if args.get("--output"):
        with open(args.get("--output"), "w") as f:
            f.writelines(output)
    else:
        print(output)
Example #13
0
class NetworkTest(object):
    def __init__(self, playbook):
        self.loader = DataLoader()
        self.inventory = InventoryManager(loader=self.loader, sources=None)
        self.variable_manager = VariableManager(self.loader,self.inventory)
        self.playbook = playbook

        self.sender_group = 'sender'
        self.inventory.add_group(self.sender_group)

        self.receiver_group = 'receiver'
        self.inventory.add_group(self.receiver_group)

        self.master_group = 'master'
        self.inventory.add_group(self.master_group)

    def set_inventory_vars(self, inv_vars):
        self.variable_manager.extra_vars = inv_vars


    def add_sender(self, sender):
        sender_host =  sender
        self.inventory.add_host(sender_host, self.sender_group)


    def add_receiver(self, receiver):
        receiver_host = receiver
        self.inventory.add_host(receiver_host, self.receiver_group)


    def add_master(self, master):
        master_host = master
        self.inventory.add_host(master_host, self.master_group)


    def run(self):

        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, connection='ssh', module_path=None,
                          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=True, become_method=None,
                          become_user='******', verbosity=None, check=False, diff=False)
        #OPTION_FLAGS = ('connection', 'remote_user', 'private_key_file', 'verbosity', 'force_handlers', 'step', 'start_at_task', 'diff',
        #        'ssh_common_args', 'docker_extra_args', 'sftp_extra_args', 'scp_extra_args', 'ssh_extra_args')
        passwords = {}

        pbex = PlaybookExecutor(playbooks=[self.playbook],
                                inventory=self.inventory,
                                variable_manager=self.variable_manager,
                                loader=self.loader,
                                options=options,
                                passwords=passwords)
        result = pbex.run()
        return result
    def test_variable_manager_precedence(self):
        # FIXME: this needs to be redone as dataloader is not the automatic source of data anymore
        return

        # pylint: disable=unreachable
        '''
        Tests complex variations and combinations of get_vars() with different
        objects to modify the context under which variables are merged.
        '''
        # FIXME: BCS makethiswork
        # return True

        mock_inventory = MagicMock()

        inventory1_filedata = """
            [group2:children]
            group1

            [group1]
            host1 host_var=host_var_from_inventory_host1

            [group1:vars]
            group_var = group_var_from_inventory_group1

            [group2:vars]
            group_var = group_var_from_inventory_group2
            """

        fake_loader = DictDataLoader({
            # inventory1
            '/etc/ansible/inventory1': inventory1_filedata,
            # role defaults_only1
            '/etc/ansible/roles/defaults_only1/defaults/main.yml': """
            default_var: "default_var_from_defaults_only1"
            host_var: "host_var_from_defaults_only1"
            group_var: "group_var_from_defaults_only1"
            group_var_all: "group_var_all_from_defaults_only1"
            extra_var: "extra_var_from_defaults_only1"
            """,
            '/etc/ansible/roles/defaults_only1/tasks/main.yml': """
            - debug: msg="here i am"
            """,

            # role defaults_only2
            '/etc/ansible/roles/defaults_only2/defaults/main.yml': """
            default_var: "default_var_from_defaults_only2"
            host_var: "host_var_from_defaults_only2"
            group_var: "group_var_from_defaults_only2"
            group_var_all: "group_var_all_from_defaults_only2"
            extra_var: "extra_var_from_defaults_only2"
            """,
        })

        inv1 = InventoryManager(loader=fake_loader, sources=['/etc/ansible/inventory1'])
        v = VariableManager(inventory=mock_inventory, loader=fake_loader)
        v._fact_cache = defaultdict(dict)

        play1 = Play.load(dict(
            hosts=['all'],
            roles=['defaults_only1', 'defaults_only2'],
        ), loader=fake_loader, variable_manager=v)

        # first we assert that the defaults as viewed as a whole are the merged results
        # of the defaults from each role, with the last role defined "winning" when
        # there is a variable naming conflict
        res = v.get_vars(play=play1)
        self.assertEqual(res['default_var'], 'default_var_from_defaults_only2')

        # next, we assert that when vars are viewed from the context of a task within a
        # role, that task will see its own role defaults before any other role's
        blocks = play1.compile()
        task = blocks[1].block[0]
        res = v.get_vars(play=play1, task=task)
        self.assertEqual(res['default_var'], 'default_var_from_defaults_only1')

        # next we assert the precedence of inventory variables
        v.set_inventory(inv1)
        h1 = inv1.get_host('host1')

        res = v.get_vars(play=play1, host=h1)
        self.assertEqual(res['group_var'], 'group_var_from_inventory_group1')
        self.assertEqual(res['host_var'], 'host_var_from_inventory_host1')

        # next we test with group_vars/ files loaded
        fake_loader.push("/etc/ansible/group_vars/all", """
        group_var_all: group_var_all_from_group_vars_all
        """)
        fake_loader.push("/etc/ansible/group_vars/group1", """
        group_var: group_var_from_group_vars_group1
        """)
        fake_loader.push("/etc/ansible/group_vars/group3", """
        # this is a dummy, which should not be used anywhere
        group_var: group_var_from_group_vars_group3
        """)
        fake_loader.push("/etc/ansible/host_vars/host1", """
        host_var: host_var_from_host_vars_host1
        """)
        fake_loader.push("group_vars/group1", """
        playbook_group_var: playbook_group_var
        """)
        fake_loader.push("host_vars/host1", """
        playbook_host_var: playbook_host_var
        """)

        res = v.get_vars(play=play1, host=h1)
        # self.assertEqual(res['group_var'], 'group_var_from_group_vars_group1')
        # self.assertEqual(res['group_var_all'], 'group_var_all_from_group_vars_all')
        # self.assertEqual(res['playbook_group_var'], 'playbook_group_var')
        # self.assertEqual(res['host_var'], 'host_var_from_host_vars_host1')
        # self.assertEqual(res['playbook_host_var'], 'playbook_host_var')

        # add in the fact cache
        v._fact_cache['host1'] = dict(fact_cache_var="fact_cache_var_from_fact_cache")

        res = v.get_vars(play=play1, host=h1)
        self.assertEqual(res['fact_cache_var'], 'fact_cache_var_from_fact_cache')
Example #15
0
def main():

    my_dir = os.path.dirname(sys.argv[0])
    zhmc_module_dir = os.path.join(my_dir, 'zhmc_ansible_modules')
    zhmc_playbooks_dir = os.path.join(my_dir, 'playbooks')
    inventory_file = '/etc/ansible/hosts'

    options = Options(connection='local',
                      module_path=[zhmc_module_dir],
                      forks=100,
                      become=None,
                      become_method=None,
                      become_user=None,
                      check=False,
                      diff=False)
    passwords = dict(vault_pass=None)
    results_callback = ResultCallback()
    loader = DataLoader()
    inventory = InventoryManager(loader=loader, sources=[inventory_file])
    variable_manager = VariableManager(loader=loader, inventory=inventory)

    # The playbook source
    play_source = dict(
        name="Get facts for a Z partition",
        hosts='localhost',
        gather_facts='no',
        vars_files=[
            os.path.join(zhmc_playbooks_dir, 'vars.yml'),
            os.path.join(zhmc_playbooks_dir, 'vault.yml'),
        ],
        tasks=[
            dict(
                name="Get partition facts",
                action=dict(
                    module='zhmc_partition',
                    args=dict(
                        hmc_host="{{hmc_host}}",
                        hmc_auth="{{hmc_auth}}",
                        cpc_name="{{cpc_name}}",
                        name="{{partition_name}}",
                        state='facts',
                    ),
                ),
                register='part1_result',
            ),
            dict(action=dict(
                module='debug',
                args=dict(msg="Gathered facts for partition "
                          "'{{part1_result.partition.name}}': "
                          "status='{{part1_result.partition.status}}'", ),
            ), ),
        ],
    )

    play = Play().load(play_source,
                       variable_manager=variable_manager,
                       loader=loader)

    tqm = None
    try:

        tqm = TaskQueueManager(
            inventory=inventory,
            variable_manager=variable_manager,
            loader=loader,
            options=options,
            passwords=passwords,
            stdout_callback=results_callback,
        )

        try:
            rc = tqm.run(play)
        except AnsibleError as exc:
            print("Error: AnsibleError: %s" % exc)
            return 2

        if rc == TaskQueueManager.RUN_OK:
            return 0
        elif rc & TaskQueueManager.RUN_FAILED_HOSTS:
            status_list = results_callback.status_failed
            assert len(status_list) == 1
            status = status_list[0]
            host_name = status['host_name']
            task_name = status['task_name']
            result = status['result']
            try:
                msg = result['msg']
            except Exception:
                print("Internal error: Unexpected format of result: %r" %
                      result)
                return 2
            print("Error: Task '%s' failed on host '%s': %s" %
                  (task_name, host_name, msg))
            return 1
        elif rc & TaskQueueManager.RUN_UNREACHABLE_HOSTS:
            status_list = results_callback.status_unreachable
            assert len(status_list) == 1
            status = status_list[0]
            host_name = status['host_name']
            task_name = status['task_name']
            result = status['result']
            try:
                msg = result['msg']
            except Exception:
                print("Internal error: Unexpected format of result: %r" %
                      result)
                return 2
            print("Error: Task '%s' failed because host '%s' is unreachable: "
                  "%s" % (task_name, host_name, msg))
            return 1
        else:
            print("Internal error: Unexpected rc=%s: %s" % (rc, rc_msg(rc)))
            return 2

    finally:
        if tqm is not None:
            tqm.cleanup()

    return 0
#!/usr/bin/env python
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

#inventory = Inventory(loader=loader, variable_manager=variable_manager,host_list='/home/felixc/ansible/hosts')
loader = DataLoader()
inventory = InventoryManager(loader=loader, sources=['./hostslist'])
variable_manager = VariableManager(loader=loader, inventory=inventory)

passwords = dict(vault_pass='******')

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

options = Options(connection='smart',
                  remote_user=None,
                  ack_pass=None,
                  sudo_user=None,
                  forks=5,
                  sudo=None,
                  ask_sudo_pass=False,
                  verbosity=5,
                  module_path=None,
                  become=None,
                  become_method=None,
Example #17
0
class AnsibleApi(object):
    def __init__(self, hostinfo, taskinfo=None):
        self.resultinfo = []
        self.taskinfo = taskinfo
        self.hostinfo = hostinfo
        self.host_list = [i.get("host", None) for i in self.hostinfo]
        self.sources = ",".join(self.host_list)
        if len(self.host_list) == 1:
            self.sources += ","
        self.passwords = dict()
        self.callback = None
        self.__initializeData()

    def __initializeData(self):
        Options = namedtuple("Options", [
            "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"
        ])
        self.options = Options(
            connection='smart',
            module_path=['/usr/share/ansible'],
            forks=100,
            remote_user=None,
            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=None,
            verbosity=None,
            check=False,
            diff=False,
        )
        self.loader = DataLoader()

        # 设置本次调用的host_list
        self.inventory = InventoryManager(loader=self.loader,
                                          sources=self.sources)
        # 加载之前的变量
        self.variable_manager = VariableManager(loader=self.loader,
                                                inventory=self.inventory)

        self.__set_hostinfo()

    def __set_hostinfo(self):
        # 设置调用主机认证信息
        for host in self.hostinfo:
            self.inventory.add_host(host.get("host"), port=host.get("port"))
            hostname = self.inventory.get_host(hostname=host.get("host"))
            self.variable_manager.set_host_variable(host=hostname,
                                                    varname='ansible_ssh_pass',
                                                    value=host.get('password'))
            self.variable_manager.set_host_variable(host=hostname,
                                                    varname='ansible_ssh_user',
                                                    value=host.get('user'))
            self.variable_manager.set_host_variable(host=hostname,
                                                    varname='ansible_ssh_port',
                                                    value=host.get('port'))
            if host.get("sudo_pass"):
                self.variable_manager.set_host_variable(
                    host=hostname,
                    varname="ansible_become_user",
                    value=host.get("sudo_user"))
                self.variable_manager.set_host_variable(
                    host=hostname,
                    varname="ansible_become_pass",
                    value=host.get("sudo_pass"))
                self.variable_manager.set_host_variable(
                    host=hostname, varname="ansible_become", value=True)
            if not host.get('password') or host.get('password') == "None":
                self.variable_manager.set_host_variable(
                    host=hostname,
                    varname="ansible_ssh_private_key_file",
                    value=host.get("ansible_ssh_private_key_file"))

    def run_model(self):
        for task in self.taskinfo:
            play_source = dict(name="andible_api_play",
                               hosts=self.host_list,
                               gather_facts="no",
                               tasks=[
                                   dict(action=dict(module=task.get("module"),
                                                    args=task.get("args")))
                               ])
            play = Play().load(play_source,
                               variable_manager=self.variable_manager,
                               loader=self.loader)
            tqm = None
            self.callback = ModelResultsCollector()
            try:
                tqm = TaskQueueManager(
                    inventory=self.inventory,
                    variable_manager=self.variable_manager,
                    loader=self.loader,
                    options=self.options,
                    passwords=self.passwords,
                    stdout_callback="minimal",
                )
                tqm._stdout_callback = self.callback
                result = tqm.run(play)
            except Exception as err:
                import traceback
                print(traceback.print_exc())
            finally:
                if tqm is not None:
                    tqm.cleanup()

    @property
    def get_model_result(self):
        for host, result in self.callback.host_ok.items():
            self.resultinfo.append(
                {host: {
                    "message": result._result,
                    "code": 0
                }})
        for host, result in self.callback.host_unreachable.items():
            self.resultinfo.append(
                {host: {
                    "message": result._result,
                    "code": -1
                }})
        for host, result in self.callback.host_failed.items():
            self.resultinfo.append(
                {host: {
                    "message": result._result,
                    "code": 1
                }})
        return self.resultinfo

    def run_playbook(self, PlayBookPath):
        try:
            self.callback = PlayBookResultsCollector()
            pbex = PlaybookExecutor(playbooks=[PlayBookPath],
                                    inventory=self.inventory,
                                    variable_manager=self.variable_manager,
                                    loader=self.loader,
                                    option=self.options,
                                    passwords=self.passwords)
            pbex._tqm._stdout_callback = self.callback
            pbex.run()
        except Exception as err:
            import traceback
            print(traceback.print_exc())
            return False

    def get_playbook_result(self):
        for host, result in self.callback.host_ok.items():
            self.resultinfo.append(
                {host: {
                    "message": result._result,
                    "code": 0
                }})
        for host, result in self.callback.host_unreachable.items():
            self.resultinfo.append(
                {host: {
                    "message": result._result,
                    "code": -1
                }})
        for host, result in self.callback.host_failed.items():
            self.resultinfo.append(
                {host: {
                    "message": result._result,
                    "code": 1
                }})
        for host, result in self.callback.task_status.items():
            self.resultinfo.append(
                {host: {
                    "message": result._result,
                    "code": 2
                }})

        for host, result in self.callback.task_skipped.items():
            self.resultinfo.append(
                {host: {
                    "message": result._result,
                    "code": 3
                }})
        return self.resultinfo
Example #18
0
 def __init__(self, filename):
     self._loader = DataLoader()
     self._inventory = InventoryManager(loader=self._loader,
                                        sources=filename)
Example #19
0
def give_inventory_and_data_loader(
) -> Tuple[InventoryManager, dataloader.DataLoader, VariableManager]:
    data_loader = dataloader.DataLoader()
    # create the inventory, and filter it based on the subset specified (if any)
    inventory = InventoryManager(loader=data_loader, sources="localhost")
    # inventory.add_host("spire_server",group="all")
    # inventory.add_host("spire_agent",group="all")
    inventory.add_group("all")
    inventory.add_group("ungrouped")
    inventory.add_host("localhost", group="ungrouped")
    inventory.add_host("spire_server", group="ungrouped")
    inventory.add_host("spire_agent", group="ungrouped")

    # create the variable manager, which will be shared throughout
    # the code, ensuring a consistent view of global variables
    var_manager = VariableManager(loader=data_loader,
                                  inventory=inventory,
                                  version_info=None)
    var_manager.set_host_variable("spire_server", "ansible_connection",
                                  "local")
    var_manager.set_host_variable("spire_agent", "ansible_connection", "local")
    var_manager.set_host_variable("spire_server", "ansible_host", "localhost")
    var_manager.set_host_variable("spire_agent", "ansible_host", "localhost")
    python_path = sys.executable
    var_manager.set_host_variable("spire_server", "ansible_python_interpreter",
                                  python_path)
    var_manager.set_host_variable("spire_agent", "ansible_python_interpreter",
                                  python_path)
    var_manager.set_host_variable("localhost", "ansible_python_interpreter",
                                  python_path)

    # spire_agent_host_vars = variable_manager.get_vars(host=inventory.get_host("localhost"))

    return inventory, data_loader, var_manager
Example #20
0
                  become_method=None,
                  become_user=None,
                  check=False,
                  diff=False)

# initialize needed objects
loader = DataLoader(
)  # Takes care of finding and reading yaml, json and ini files
passwords = dict()

# Instantiate our ResultCallback for handling results as they come in. Ansible expects this to be one of its main display outlets
# results_callback = ResultCallback()

# create inventory, use path to host config file as source or hosts in a comma separated string
# inventory = InventoryManager(loader=loader, sources='localhost,')
inventory = InventoryManager(loader=loader, sources=['/root/myansi/hosts'])

# variable manager takes care of merging all the different sources to give you a unifed view of variables available in each context
variable_manager = VariableManager(loader=loader, inventory=inventory)

# create datastructure that represents our play, including tasks, this is basically what our YAML loader does internally.
play_source = dict(
    name="Ansible Play",
    hosts='webservers',
    gather_facts='no',
    tasks=[
        # dict(action=dict(module='shell', args='ls'), register='shell_out'),
        dict(action=dict(module='yum', args='name=vsftpd state=latest'),
             register='shell_out'),
        # dict(action=dict(module='shell', args='id zhangsan'), register='shell_out'),
        # dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}')))
    def __init__(self,
                 hostnames,
                 action,
                 playbook,
                 private_key_file,
                 run_data,
                 internal_data,
                 location,
                 become_pass,
                 request_id,
                 started_at,
                 config,
                 dbsession,
                 tr,
                 verbosity=5):

        self.logger = app.logger
        self.logger.debug('initializing ansible runbook executor')

        self.action = action
        self.config = config
        self.dbsession = dbsession
        self.transition_request = tr

        self.location = location
        self.run_data = run_data
        self.internal_data = internal_data

        self.run_variables = {}
        self.run_variables.update(self.run_data)
        self.run_variables.update(self.location)
        self.run_variables.update(self.internal_data)

        self.logger.debug(str(self.location))
        self.logger.debug(str(self.run_data))
        self.logger.debug(str(self.run_variables))

        self.request_id = request_id
        self.started_at = started_at
        self.finished_at = None
        self.resInstance = {}

        # NEW 2.9
        # handles options now
        context.CLIARGS = ImmutableDict(
            connection='ssh',
            module_path=['/var/alm_ansible_rm/library'],
            forks=20,
            become=None,
            become_method='sudo',
            become_user='******',
            check=False,
            diff=False,
            ansible_python_interpreter='/usr/bin/python3',
            host_key_checking=False,
            vault_password_file='/etc/ansible/tslvault.txt',
            private_key_file=private_key_file,
            listhosts=None,
            listtasks=None,
            listtags=None,
            syntax=None,
            start_at_task=None)

        # Gets data from YAML/JSON files
        self.loader = DataLoader()
        self.loader.set_vault_secrets([
            ('default', VaultSecret(_bytes=to_bytes('TSLDem0')))
        ])

        # create temporary inventory file
        self.hosts = NamedTemporaryFile(delete=False)
        self.hosts.write(b'[run_hosts]\n')
        self.hosts.write(
            b'localhost ansible_connection=local ansible_python_interpreter="/usr/bin/env python3" host_key_checking=False'
        )
        self.hosts.close()

        # set Inventory
        self.inventory = InventoryManager(loader=self.loader,
                                          sources=self.hosts.name)

        # All the variables from all the various places
        self.variable_manager = VariableManager(loader=self.loader,
                                                inventory=self.inventory)
        # MOD 2.9 - has been renamed, not sure this is the proposed way to treat extra_vars, but it works
        self.variable_manager._extra_vars = self.run_variables

        # Become Pass Needed if not logging in as user root
        passwords = {'become_pass': become_pass}

        # Setup playbook executor, but don't run until run() called
        self.pbex = playbook_executor.PlaybookExecutor(
            playbooks=[playbook],
            inventory=self.inventory,
            variable_manager=self.variable_manager,
            loader=self.loader,
            passwords=passwords)

        if (self.transition_request) and (isinstance(self.transition_request,
                                                     TransitionRequest)):
            # log only if transition request, not for netowrk/image scans)
            self.logger.debug('transition request ' +
                              str(self.transition_request))
            self.log_request_status('PENDING', 'playbook initialized', '', '')

        self.logger.debug('ansible runbook executor instantiated for ' +
                          str(playbook))

        self.callback = OutputCallback(self.action, self.logger)
        self.pbex._tqm._stdout_callback = self.callback
Example #22
0
    def v2_runner_on_ok(self, result, *args, **kwargs):
        """Print a json representation of the result.

        Also, store the result in an instance attribute for retrieval later
        """
        host = result._host
        self.host_ok[host.get_name()] = result
        print(json.dumps({host.name: result._result}, indent=4))

    def v2_runner_on_failed(self, result, *args, **kwargs):
        host = result._host
        self.host_failed[host.get_name()] = result
        print("aaaa")

results_callback = ResultCallback()

context.CLIARGS = ImmutableDict(tags={}, listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh',
                    module_path=None, 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=True,
                    become_method='Sudo', become_user='******', verbosity=True, check=False, start_at_task=None, stdout_callback=results_callback)

inventory = InventoryManager(loader=loader, sources=('10.0.0.27,'))

variable_manager = VariableManager(loader=loader, inventory=inventory, version_info=CLI.version_info(gitinfo=False))

pbex = PlaybookExecutor(playbooks=['./static/ansible/ls.yml'], inventory=inventory, variable_manager=variable_manager, loader=loader, passwords={})

results = pbex.run()

print(results)
Example #23
0
                                private_key_file=None,
                                ssh_common_args=None,
                                ssh_extra_args=None,
                                sftp_extra_args=None,
                                scp_extra_args=None,
                                become=True,
                                become_method='sudo',
                                become_user='******',
                                verbosity=20,
                                check=False)

loader = DataLoader()

passwords = dict(vault_pass='******')

inventory = InventoryManager(loader=loader, sources='/etc/ansible/hosts')

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

play_source = dict(
    name='Ansible Play',
    hosts='pool',
    gather_facts='no',
    tasks=[
        dict(action=dict(module='shell', args='systemctl status indy-node'),
             register='shell_out'),
        dict(action=dict(module='debug', args=dict(
            msg='{{shell_out.stdout}}')))
    ])

play = Play().load(play_source,
Example #24
0
def adhoc(sources, hosts, module, args):
    # Options是在执行ansible临时命令时,提供的选项,需要了解的选项有
    # connection是连接方式,local表示在本机执行,ssh表示ssh执行,smart表示自动选择
    # forks指的是一次同时向多少台主机发送指令
    Options = namedtuple('Options', [
        'connection', 'module_path', 'forks', 'become', 'become_method',
        'become_user', 'check', 'diff'
    ])
    options = Options(connection='ssh',
                      module_path=['/to/mymodules'],
                      forks=10,
                      become=None,
                      become_method=None,
                      become_user=None,
                      check=False,
                      diff=False)

    # DataLoader用于解析json/ini/yaml等文件,将其转换成python的数据类型
    loader = DataLoader()
    # 设置密码
    passwords = dict(vault_pass='******')

    # 主机清单文件,表示方式有两种
    # 一种是将各个主机用冒号分隔,成为一个字符串
    # 另外一种方式是使用主机路径列表
    # inventory = InventoryManager(loader=loader, sources='localhost,')
    inventory = InventoryManager(loader=loader, sources=sources)

    # 变量管理
    variable_manager = VariableManager(loader=loader, inventory=inventory)

    # 创建play源
    play_source = dict(
        name="Ansible Play",
        hosts=hosts,  # 在哪台主机上执行任务
        gather_facts='no',
        tasks=[
            dict(action=dict(module=module, args=args), register='shell_out'),
            # dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}')))
        ])

    # 创建play对象
    play = Play().load(play_source,
                       variable_manager=variable_manager,
                       loader=loader)

    # 通过任务队列管理器执行play
    tqm = None
    try:
        tqm = TaskQueueManager(
            inventory=inventory,
            variable_manager=variable_manager,
            loader=loader,
            options=options,
            passwords=passwords,
        )
        result = tqm.run(play)
    finally:
        if tqm is not None:
            tqm.cleanup()

        shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
Example #25
0
    def execude(self):
        # since API is constructed for CLI it expects certain options to always be set, named tuple 'fakes' the args parsing options object
        Options = namedtuple('Options', [
            'connection',
            'module_path',
            'forks',
            'become',
            'become_method',
            'become_user',
            'check',
            'diff',
            'private_key_file',
        ])
        options = Options(
            connection='smart',
            module_path=['/to/mymodules'],
            forks=10,
            become=None,
            become_method=None,
            become_user='******',
            check=False,
            diff=False,
            private_key_file='~/.ssh/id_rsa',
        )

        # initialize needed objects
        loader = DataLoader(
        )  # Takes care of finding and reading yaml, json and ini files
        passwords = dict(vault_pass='******')

        # Instantiate our ResultCallback for handling results as they come in. Ansible expects this to be one of its main display outlets
        results_callback = ResultCallback()

        # create inventory, use path to host config file as source or hosts in a comma separated string
        inventory = InventoryManager(loader=loader,
                                     sources='/etc/ansible/hosts')
        # inventory.add_host(host='172.20.51.22',port=22,group='test')

        # variable manager takes care of merging all the different sources to give you a unified view of variables available in each context
        variable_manager = VariableManager(loader=loader, inventory=inventory)

        # create data structure that represents our play, including tasks, this is basically what our YAML loader does internally.
        play_source = dict(
            name="Ansible Play",
            hosts=self.hosts,
            gather_facts='no',
            tasks=[
                dict(
                    action=dict(module='shell', args='ps -ef | grep VSM'),
                    register='shell_out',
                ),
                # dict(action=dict(module='setup', ), register='shell_out'),
                # dict(action=dict(module='debug', args=dict(msg='{{shell_out.stdout}}')))
            ])

        play = Play().load(
            play_source,
            variable_manager=variable_manager,
            loader=loader,
        )
        tqm = None
        try:
            tqm = TaskQueueManager(
                inventory=inventory,
                variable_manager=variable_manager,
                loader=loader,
                options=options,
                passwords=None,
                stdout_callback=results_callback,
                # Use our custom callback instead of the ``default`` callback plugin, which prints to stdout
            )
            # atest = tqm
            # atest.run(play)
            rs = tqm.run(
                play,
            )  # most interesting data for a play is actually sent to the callback's methods
            print('---')
            print()
            # if tqm._stdout_callback.consquence:
            #     for key in tqm._stdout_callback.consquence['172.20.51.22']['stderr']:
            #         print(key,)
        except BaseException:
            print(str(BaseException))
        finally:
            # we always need to cleanup child procs and the structures we use to communicate with them
            if tqm is not None:
                tqm.cleanup()

            # Remove ansible tmpdir
            shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)
Example #26
0
class MyInventory():
    """
    this is IOPS ansible inventory object.
    """
    def __init__(self, resource, loader, variable_manager):
        self.resource = resource
        self.loader = DataLoader()
        self.inventory = InventoryManager(
            loader=self.loader, sources=[settings.FD_ANSIBLE_HOSTS_FILE])

        # self.variable_manager.set_inventory(self.inventory)
        self.variable_manager = VariableManager(loader=self.loader,
                                                inventory=self.inventory)

        self.dynamic_inventory()

    def add_dynamic_group(self, hosts, groupname, groupvars=None):
        """
            add hosts to a group
        """
        self.inventory.add_group(groupname)
        my_group = Group(name=groupname)

        # if group variables exists, add them to group
        if groupvars:
            for key, value in groupvars.items():
                my_group.set_variable(key, value)

        # add hosts to group
        for host in hosts:
            #print (host)
            # set connection variables
            hostname = host.get("hostname")
            hostip = host.get('ip', hostname)
            hostport = host.get("port")
            username = host.get("username")
            password = host.get("password")
            ssh_key = host.get("ssh_key")
            my_host = Host(name=hostname, port=hostport)
            print(my_host)
            self.variable_manager.set_host_variable(host=my_host,
                                                    varname='ansible_ssh_host',
                                                    value=hostip)
            self.variable_manager.set_host_variable(host=my_host,
                                                    varname='ansible_ssh_pass',
                                                    value=password)
            self.variable_manager.set_host_variable(host=my_host,
                                                    varname='ansible_ssh_port',
                                                    value=hostport)
            self.variable_manager.set_host_variable(host=my_host,
                                                    varname='ansible_ssh_user',
                                                    value=username)
            self.variable_manager.set_host_variable(
                host=my_host,
                varname='ansible_ssh_private_key_file',
                value=ssh_key)
            # my_host.set_variable('ansible_ssh_pass', password)
            # my_host.set_variable('ansible_ssh_private_key_file', ssh_key)
            # set other variables
            for key, value in host.items():
                if key not in ["hostname", "port", "username", "password"]:
                    self.variable_manager.set_host_variable(host=my_host,
                                                            varname=key,
                                                            value=value)

            # add to group

            self.inventory.add_host(host=hostname,
                                    group=groupname,
                                    port=hostport)
            ghost = Host(name="192.168.8.119")

    def dynamic_inventory(self):
        """
            add hosts to inventory.
        """
        if isinstance(self.resource, list):
            self.add_dynamic_group(self.resource, 'default_group')
        elif isinstance(self.resource, dict):
            for groupname, hosts_and_vars in self.resource.items():
                self.add_dynamic_group(hosts_and_vars.get("hosts"), groupname,
                                       hosts_and_vars.get("vars"))
Example #27
0
class ResultCallback(CallbackBase):

    def v2_runner_on_ok(self, result, **kwargs):
        print(json.dumps({result._host.name: result._result}))


if __name__ == '__main__':
    Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'diff'])
    options = Options(connection='smart', module_path=[], forks=6, become=None, become_method=None, become_user=None, check=False, diff=False)

    loader = DataLoader()
    passwords = {}

    callback = ResultCallback()

    inventory = InventoryManager(loader=loader, sources='hosts.py')

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

    source = {
        'hosts' : 'mytest',
        'gather_facts' : 'False',
        'tasks' : [
            {
                'name' : 'shell',
                'shell' : 'ls /',
                'register' : 'result',
            },
            {
                'debug' : {
                    'msg' : ' {{ result.stdout }}',
Example #28
0
 def inventory_manager(self):
     return InventoryManager(loader=self._ansible_inventory_data_loader,
                             sources=self.paths.inventory_ini)
Example #29
0
                  check=False,
                  diff=False)

# initialize needed objects
#ansible会用到各种各样的文件, 如 json, yaml, ini等, 这些文件的内容需要转成python的数据类型,DataLoader自动进行转换
loader = DataLoader(
)  # Takes care of finding and reading yaml, json and ini files
#各种密码 ,如果有利用ansible-vault 加密,尝试使用vault_pass 所指定的密码来解密
passwords = dict(vault_pass='******')

# Instantiate our ResultCallback for handling results as they come in. Ansible expects this to be one of its main display outlets
# results_callback = ResultCallback()

# create inventory, use path to host config file as source or hosts in a comma separated string
#inventory = InventoryManager(loader=loader, sources='localhost,')
inventory = InventoryManager(loader=loader, sources=["/root/myansible/hosts"])

# variable manager takes care of merging all the different sources to give you a unifed view of variables available in each context
#变量管理器
variable_manager = VariableManager(loader=loader, inventory=inventory)

# create datastructure that represents our play, including tasks, this is basically what our YAML loader does internally.

play_source = dict(
    name="Ansible Play",
    hosts='webservers',
    gather_facts='no',
    tasks=[
        dict(action=dict(module='shell', args='ls'), register='shell_out'),
        dict(action=dict(module='debug', args=dict(
            msg='{{shell_out.stdout}}')))
Example #30
0
                  module_path='/usr/local/lib/python2.7/dist-packages/ansible',
                  forks=100,
                  become=None,
                  become_method=None,
                  become_user=None,
                  check=False,
                  diff=False)
passwords = dict(vault_pass='******')

# Instantiate our ResultCallback for handling results as they come in
results_callback = ResultCallback()
results_callback.nodeid = 'my_id_node'
list = get_template()

# create inventory and pass to var manager
inventory = InventoryManager(loader=loader, sources=['my_inventory.txt'])
variable_manager = VariableManager(loader=loader, inventory=inventory)

# create play with tasks

play_source = loader.load_from_file(
    '/mnt/extra-addons/bibind/create_site_ovh.yml')[0]
play = Play().load(play_source,
                   variable_manager=variable_manager,
                   loader=loader)

# actually run it
tqm = None
try:
    tqm = TaskQueueManager(
        inventory=inventory,
Example #31
0
from ansible.executor.task_queue_manager import TaskQueueManager
from ansible.plugins.callback import CallbackBase

from ansible import context
from ansible.module_utils.common.collections import ImmutableDict

###############################
# InventoryManager 类
###############################
# 读取资产清单信息
loader = DataLoader()
"""
sources 可以是绝对和相对路径
"""

inventory_manager = InventoryManager(loader=loader, sources=['my_hots.txt'])

###############################
# VariableManager 类
###############################
variable_manager = VariableManager(loader=loader, inventory=inventory_manager)

# 方法2
context.CLIARGS = ImmutableDict(connection='smart',
                                module_path=None,
                                verbosity=5,
                                forks=10,
                                become=None,
                                become_method=None,
                                become_user=None,
                                check=False,
Example #32
0
def main():
    # Set default
    base_dir = '.'
    discovered_devices = {}
    inventory_file = None
    username = None
    password = None

    # Reading options
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'di:u:p:')
    except getopt.GetoptError as err:
        logging.error(err)
        usage()
        sys.exit(255)

    for opt, arg in opts:
        if opt == '-d':
            logging.basicConfig(level=logging.DEBUG)
        elif opt == '-i':
            inventory_file = arg
        elif opt == '-u':
            username = arg
        elif opt == '-p':
            password = arg
        else:
            logging.error('unhandled option ({})'.format(opt))
            usage()
            sys.exit(255)

    # Checking options and environment
    if not os.path.isdir(base_dir):
        logging.error('base directory does not exist ({})'.format(base_dir))
        sys.exit(255)
    if not inventory_file:
        logging.error('inventory file not specified'.format(base_dir))
        sys.exit(255)
    if not os.path.isfile(inventory_file):
        logging.error(
            'inventory file does not exist ({})'.format(inventory_file))
        sys.exit(255)
    if not username or not password:
        logging.error('username and/or password not set'.format(base_dir))
        sys.exit(255)

    # Loading Ansible inventory
    ansible_loader = DataLoader()
    try:
        ansible_inventory = InventoryManager(loader=ansible_loader,
                                             sources=inventory_file)
    except:
        logging.error('cannot read inventory file ({})'.format(inventory_file))
        sys.exit(255)
    variable_manager = VariableManager(loader=ansible_loader,
                                       inventory=ansible_inventory)

    # Discover each host
    for host in ansible_inventory.get_hosts():
        try:
            os.makedirs('devices/{}'.format(host), exist_ok=True)
        except:
            logging.error('cannot create directory (devices/{})'.format(host))

        facts = getFacts(username=host.vars['snmp_username'],
                         password=host.vars['snmp_password'],
                         host=host.vars['ansible_host'])
        cdp_neighbors = getCDPNeighbors(username=host.vars['snmp_username'],
                                        password=host.vars['snmp_password'],
                                        host=host.vars['ansible_host'])

        try:
            facts_output = open('devices/{}/facts.json'.format(host), 'w+')
            facts_output.write(json.dumps(facts))
            facts_output.close()
        except:
            logging.error(
                'cannot write facts (devices/{}/facts.json)'.format(host))
            continue

        try:
            cdp_neighbors_output = open(
                'devices/{}/cdp_neighbors.json'.format(host), 'w+')
            cdp_neighbors_output.write(json.dumps(cdp_neighbors))
            cdp_neighbors_output.close()
        except:
            logging.error(
                'cannot write facts (devices/{}/cdp_neighbors.json)'.format(
                    host))
            continue
Example #33
0
 def __init__(self, module=None):
     self.module = module
     self.tasks = []
     self.results = dict(tasks_run=[],
                         tasks_failed=[],
                         tasks_unreachable=[],
                         tasks_result=dict())
     # counter
     # self._JS = 0
     self.config = Config()
     _options = namedtuple(
         'Options',
         [
             'connection',  # connection types: smart,ssh,paramiko...
             'module_path',  # remote python module path
             'forks',  # forks numbers
             'become',  # True or False
             'become_method',  # sudo or other
             'become_user',  # who sudo
             'check',  # TODO: unknown
             'diff',  # TODO: unknown
             'remote_user',  # remote user name
             'ssh_common_args',  # add ssh args
             'private_key_file',  # where private key
             'verbosity',  # more information show
             'host_key_checking',  # next version support for close host checking
             'listhosts',  # support PlaybookExecutor
             'listtasks',  # support PlaybookExecutor
             'listtags',  # support PlaybookExecutor
             'syntax',  # support PlaybookExecutor
         ])
     # defaults args
     _defaults = dict(
         connection='smart',
         module_path=None,
         forks=100,
         become=True,
         become_method='sudo',
         become_user='******',
         check=False,
         diff=False,
         remote_user='******',
         ssh_common_args=None,
         private_key_file=None,
         verbosity=4,
         host_key_checking=False,
     )
     # load from config file
     for i, j in self.config.items():
         _defaults[i] = j
     self.options = _options(
         connection=_defaults['connection'],
         remote_user=_defaults['remote_user'],
         module_path=_defaults['module_path'],
         forks=_defaults['forks'],
         become=_defaults['become'],
         become_method=_defaults['become_method'],
         become_user=_defaults['become_user'],
         check=_defaults['check'],
         diff=_defaults['diff'],
         ssh_common_args=_defaults['ssh_common_args'],
         private_key_file=_defaults['private_key_file'],
         verbosity=_defaults['verbosity'],
         host_key_checking=_defaults['host_key_checking'],
         listhosts=None,
         listtasks=None,
         listtags=None,
         syntax=None,
     )
     self.loader = DataLoader()
     self.inventory = InventoryManager(loader=self.loader,
                                       sources=LC['HOSTS_FILES'])
     self.variable_manager = VariableManager(loader=self.loader,
                                             inventory=self.inventory)
     self.play_source = dict(name='',
                             hosts='',
                             gather_facts='no',
                             tasks=self.tasks)
     self.results_callback = ResultCallback(self.results)
Example #34
0
                                become=None,
                                become_method=None,
                                become_user=None,
                                check=False,
                                diff=False)

# 初始化所需对象
loader = DataLoader()  # 负责查找和读取yaml、json和ini文件
passwords = dict(vault_pass='******')

# 实例化resultcallback,以便在结果传入时处理它们。
# Ansible希望这是其主要的展示渠道之一。
results_callback = ResultCallback()

# 创建清单,使用作为源的主机配置文件路径,或使用逗号分隔的字符串作为主机
inventory = InventoryManager(loader=loader, sources='localhost,')

# 变量管理器负责合并所有不同的源,以便为您提供每个上下文中可用变量的统一视图。
variable_manager = VariableManager(loader=loader, inventory=inventory)

# 创建代表我们的游戏(包括任务)的数据结构,这基本上是我们的yaml加载程序在内部所做的。
play_source = dict(
    name="Ansible Play",
    hosts='localhost',
    gather_facts='no',
    tasks=[
        dict(action=dict(module='shell', args='ls'), register='shell_out'),
        dict(action=dict(module='debug', args=dict(
            msg='{{shell_out.stdout}}')))
    ])
# 创建play object,playbook objects使用。
Example #35
0
    def _run_ansible(self, args):
        """Actually build an run an ansible play and return the results"""
        zclass = args.pop('zbx_class')

        # The leadup to the TaskQueueManager() call below is
        # copy pasted from Ansible's example:
        # https://docs.ansible.com/ansible/developing_api.html#python-api-2-0
        # pylint: disable=invalid-name
        Options = namedtuple('Options', [
            'connection', 'module_path', 'forks', 'become', 'become_method',
            'become_user', 'check', 'diff'
        ])

        loader = DataLoader()
        options = Options(connection='local',
                          module_path=None,
                          forks=1,
                          become=None,
                          become_method=None,
                          become_user=None,
                          check=False,
                          diff=False)
        passwords = dict(vault_pass='******')

        results_callback = ResultsCallback()

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

        play_source = dict(name="Ansible Play",
                           hosts=self.pattern,
                           gather_facts='no',
                           tasks=[
                               dict(action=dict(module=zclass, args=args)),
                           ])

        play = Play().load(play_source,
                           variable_manager=variable_manager,
                           loader=loader)

        tqm = None
        try:
            tqm = TaskQueueManager(inventory=inventory,
                                   variable_manager=variable_manager,
                                   loader=loader,
                                   options=options,
                                   passwords=passwords,
                                   stdout_callback=results_callback)
            return_code = tqm.run(play)
        finally:
            if tqm is not None:
                tqm.cleanup()

            # Remove ansible tmpdir
            shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)

        if return_code != 0:
            raise ResultsException(
                "Ansible module run failed, no results given.")

        if results_callback.result.is_unreachable():
            message = "Ansible module run failed: module output:\n%s" % \
                      json.dumps(results_callback.raw_result, indent=4)
            raise ResultsException(message)

        if results_callback.result.is_failed():
            raise ResultsException(results_callback.raw_result)

        return results_callback.raw_result
Example #36
0
def ansible_adhoc(module, args, host):
    class ResultCallback(CallbackBase):
        def v2_runner_on_ok(self, result, **kwargs):
            host = result._host
            self.data = {host.name: result._result}
            return self.data

    Options = namedtuple('Options', [
        'connection', 'module_path', 'forks', 'become', 'become_method',
        'become_user', 'check', 'diff'
    ])

    # initialize needed objects
    loader = DataLoader()
    options = Options(
        connection='smart',
        module_path=
        '/ops-data/zp/haha/mysite-11/lib/python3.6/site-packages/ansible',
        forks=100,
        become=None,
        become_method=None,
        become_user=None,
        check=False,
        diff=False)
    passwords = dict(vault_pass='******')

    # Instantiate our ResultCallback for handling results as they come in
    results_callback = ResultCallback()

    # create inventory and pass to var manager
    inventory = InventoryManager(loader=loader, sources=['/etc/ansible/hosts'])
    variable_manager = VariableManager(loader=loader, inventory=inventory)

    # create play with tasks
    play_source = dict(name="Ansible Play",
                       hosts=host,
                       gather_facts='no',
                       tasks=[
                           dict(action=dict(module=module, args=args),
                                register='shell_out')
                       ])
    play = Play().load(play_source,
                       variable_manager=variable_manager,
                       loader=loader)

    # actually run it
    tqm = None
    try:
        tqm = TaskQueueManager(
            inventory=inventory,
            variable_manager=variable_manager,
            loader=loader,
            options=options,
            passwords=passwords,
            stdout_callback=results_callback,
        )
        result = tqm.run(play)
    finally:
        if tqm is not None:
            tqm.cleanup()
        return results_callback.data
Example #37
0
class AnsibleApi(object):
    def __init__(self):
        self.Options = namedtuple('Options',
                     ['connection',
                      'remote_user',
                      'ask_sudo_pass',
                      'verbosity',
                      'ack_pass',
                      'module_path',
                      'forks',
                      'become',
                      'become_method',
                      'become_user',
                      'check',
                      'listhosts',
                      'listtasks',
                      'listtags',
                      'syntax',
                      'sudo_user',
                      'sudo',
                      'diff'])
        self.options = self.Options(connection='smart',
                       remote_user=None,
                       ack_pass=None,
                       sudo_user=None,
                       forks=5,
                       sudo=None,
                       ask_sudo_pass=False,
                       verbosity=5,
                       module_path=None,
                       become=None,
                       become_method=None,
                       become_user=None,
                       check=False,
                       diff=False,
                       listhosts=None,
                       listtasks=None,
                       listtags=None,
                       syntax=None)

        self.loader = DataLoader()
        self.inventory = InventoryManager(loader=self.loader, sources=['/opt/myops/myansible/hosts'])
        self.variable_manager = VariableManager(loader=self.loader, inventory=self.inventory)
        self.callback = ModelResultsCollector()
        self.passwords = dict()

    def display_hosts(self):
        groups = self.inventory.get_groups_dict()
        groups = json.dumps(groups, sort_keys=False, indent=4)
        return groups

    def runansible(self,host_list, task_list):

        play_source =  dict(
            name = "Ansible Play",
            hosts = host_list,
            gather_facts = 'no',
            tasks = task_list
        )
    
        play = Play().load(play_source, variable_manager=self.variable_manager, loader=self.loader)    
        tqm = TaskQueueManager(
           inventory=self.inventory,
           variable_manager=self.variable_manager,
           loader=self.loader,
           options=self.options,
           passwords=self.passwords,
           stdout_callback=self.callback,
           )

        result = tqm.run(play)
        result_raw = {'success':{},'failed':{},'unreachable':{}}
        for host,result in self.callback.host_ok.items():
            result_raw['success'][host] = result._result
        for host,result in self.callback.host_failed.items():
            result_raw['failed'][host] = result._result
        for host,result in self.callback.host_unreachable.items():
            result_raw['unreachable'][host] = result._result

        js = json.dumps(result_raw, sort_keys=False, indent=4)
        
        return js