def listfunction(llist):
    """This function does all the work and lists groups or hosts"""
    variable_manager = VariableManager()
    loader = DataLoader()
    if not os.path.isfile(inventory_file):
        print "%s is not a file - halting. Consider using the '--inventory $path/to/ansible_inventory file' parameter" % inventory_file
        sys.exit(1)
    else:
        inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=inventory_file)

    if chosen_group and single:
        def traverse(agroup, hostset):
            """Recursive depth-first traversal"""
            for child in agroup.child_groups:
                traverse(child, hostset)
            if len(agroup.hosts) > 0:
                hostset.add(agroup.hosts[0].name.encode('utf8'))
        single_hosts = set()
        traverse(inventory.groups[chosen_group], single_hosts)
        return {chosen_group:list(single_hosts)}

    if chosen_group:
        thegroup = inventory.groups[chosen_group]
        newhosts = []
        for h in thegroup.get_hosts():
            newhosts.append(h.name.encode('utf8'))
        return {chosen_group:newhosts}
    else:
        all_groups = {}
        for g in inventory.groups:
            newhosts = []
            for h in inventory.get_group(g).get_hosts():
                newhosts.append(h.name.encode('utf8'))
            all_groups[g] = newhosts
        return all_groups
class InventoryWrapper:
    def __init__(self, host_list):
        self.loader = DataLoader()
        # this code is a bit ugly, because Ansible 2.4 switched the order
        # of the object and the pattern (InventoryManager is a argument to
        # VariableManager, and vice-versa on version <2.3)
        if ANSIBLE_24:
            self.im = InventoryManager(loader=self.loader,
                                       sources=[host_list, ])
            self.vm = VariableManager(loader=self.loader, inventory=self.im)
        else:
            self.vm = VariableManager()
            self.im = Inventory(self.loader, self.vm, host_list)

    def get_loader(self):
        return self.loader

    def get_variable_manager(self):
        return VariableManagerWrapper(self.vm)

    def get_groups(self):
        if ANSIBLE_24:
            return self.im.groups
        return self.im.get_groups()

    def get_hosts(self, group):
        if ANSIBLE_24:
            return self.im.get_hosts(pattern=group)
        return self.im.get_hosts(group)

    def get_host(self, host):
        return self.im.get_host(host)

    def refresh_inventory(self):
        return self.im.refresh_inventory()
Example #3
0
 def __init__(self, ctrl, vault_password=None):
     from ploy_ansible import get_playbooks_directory
     kwargs = dict(host_list=[])
     if vault_password is not None:
         kwargs['vault_password'] = vault_password
     BaseInventory.__init__(self, **kwargs)
     self.ctrl = ctrl
     self.set_playbook_basedir(get_playbooks_directory(ctrl.config))
     groups = {}
     groups['all'] = self.get_group('all')
     seen = set()
     for instance in self.ctrl.instances.values():
         if instance.uid in seen:
             continue
         seen.add(instance.uid)
         h = Host(instance.uid)
         add_to = ['all', '%ss' % instance.sectiongroupname]
         if hasattr(instance, 'master'):
             master = instance.master
             if instance == getattr(master, 'instance', None):
                 add_to.append('masters')
             else:
                 add_to.append('%s-instances' % master.id)
         for group in add_to:
             g = groups.get(group)
             if g is None:
                 g = self.get_group(group)
                 if g is None:
                     g = Group(group)
                     self.add_group(g)
                 groups[group] = g
             g.add_host(h)
     self._vars_plugins = [x for x in utils.plugins.vars_loader.all(self)]
Example #4
0
def run_ansible_in_python(task):
    stats = callbacks.AggregateStats()
    ## everything between this comment
    ## and the "end" should hopefully not be necessary
    ## after Ansible 1.4.6 or 1.5 is released, since there
    ## will be an implicit sys.executable interpreter for the localhost
    ## host. This will almost certainly need tweaking anyway for dynamic inventory
    ## in ec2. I'm not sure yet how this would work.
    localhost = Host('localhost')
    localhost.set_variable('ansible_python_interpreter', sys.executable)
    all_group = Group('all')
    all_group.add_host(localhost)
    inventory = Inventory(None)
    inventory.add_group(all_group)
    os.putenv('EC2_INI_PATH', 'lib/glue/ec2-external.ini')
    ec2_inventory = InventoryScript(filename='lib/glue/ec2.py')
    inventory.parser = ec2_inventory
    [inventory.add_group(group) for group in ec2_inventory.groups.values()]
    ## end

    pb = playbook.PlayBook(playbook=task.inputs[0].abspath(),
                           inventory=inventory,
                           stats=stats,
                           callbacks=callbacks.PlaybookCallbacks(verbose=3),
                           runner_callbacks=callbacks.PlaybookRunnerCallbacks(stats, verbose=3)
                           )
    pb.run()
Example #5
0
class Inventory(object):
    def __init__(self, inventory_path):
        """
        The Inventory class provides methods to extract information from the
        specified ansible inventory file.

        :param inventory_path: The path to the inventory file
        :type inventory_path: String
        """
        from ansible.inventory import Inventory as AnsibleInventory
        from ansible.parsing.dataloader import DataLoader
        from ansible.vars import VariableManager

        self._inventory = AnsibleInventory(loader=DataLoader(),
                                           variable_manager=VariableManager(),
                                           host_list=inventory_path)

    def get_ansible_inventory(self):
        return self._inventory

    def get_hosts(self, pattern='all'):
        return self._inventory.get_hosts(pattern)

    def get_groups(self):
        return self._inventory.get_groups()
def run_installer(user_list, package_list, sudo_password):
    """
    Runs the playbook `installer.yml` with the supplied parameters
    """

    # Create the inventory
    controller = Host(name="localhost")
    controller.set_variable("users", user_list)
    controller.set_variable("apt_packages", package_list)
    local_inventory = Inventory([])
    local_inventory.get_group("all").add_host(controller)

    # Boilerplate for callbacks setup
    utils.VERBOSITY = 0
    # Output callbacks setup
    output_callbacks = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
    # API callbacks setup
    stats = callbacks.AggregateStats()
    api_callbacks = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)

    provision_playbook = PlayBook(
        playbook="installer.yml",
        stats=stats,
        callbacks=output_callbacks,
        runner_callbacks=api_callbacks,
        inventory=local_inventory,
        transport="local",
        become_pass=sudo_password,
    )
    playbook_result = provision_playbook.run()
    return playbook_result
Example #7
0
 def __init__(self, aws):
     BaseInventory.__init__(
         self,
         host_list=[])
     self.aws = aws
     ansible_config = aws.config.get('global', {}).get('ansible', {})
     if 'playbooks-directory' in ansible_config:
         self.set_playbook_basedir(ansible_config['playbooks-directory'])
     groups = {}
     groups['all'] = self.get_group('all')
     for instance in self.aws.instances.values():
         h = Host(instance.id)
         add_to = ['all', '%ss' % instance.sectiongroupname]
         if hasattr(instance, 'master'):
             master = instance.master
             if instance == getattr(master, 'instance', None):
                 add_to.append('masters')
             else:
                 add_to.append('%s-instances' % master.id)
         for group in add_to:
             g = groups.get(group)
             if g is None:
                 g = self.get_group(group)
                 if g is None:
                     g = Group(group)
                     self.add_group(g)
                 groups[group] = g
             g.add_host(h)
     self._vars_plugins = []
     self._vars_plugins.append(VarsModule(self))
Example #8
0
class NetworkTest(object):
    def __init__(self, playbook):
        self.inventory = Inventory(host_list=[])
        self.playbook = playbook
 
        self.stac_nodes_group = Group(name='stac_nodes')
        self.inventory.add_group(self.stac_nodes_group)
 
        self.inv_vars = dict()

    def set_inventory_vars(self, inv_vars):
        self.inv_vars.update(inv_vars)

    def add_stac_node(self, node):
        stac_node = Host(name=node)
        self.stac_nodes_group.add_host(stac_node)

    def run(self):
        stats = AggregateStats()
        playbook_cb = PlaybookCallbacks(verbose=utils.VERBOSITY)
        runner_cb = PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)

        pb = playbook.PlayBook(playbook=self.playbook,
                               stats=stats,
                               callbacks=playbook_cb,
                               runner_callbacks=runner_cb,
                               inventory=self.inventory,
                               extra_vars=self.inv_vars,
                               check=False)

        pr = pb.run()

        print json.dumps(pr, sort_keys=True, indent=4, separators=(',', ': '))
Example #9
0
def get_ip():
     tag = "tag_Name_" + request.args.get('host')
     env = request.args.get('env')
     eip = request.args.get('eip').capitalize() if 'eip' in request.url else False
     inventory_file = ec2_inventory_path + env + "/ec2.py"

     if env not in envs:
        return make_response(json.dumps({'error': env + ' Environment not found'}),400)

     inv = Inventory(inventory_file)
     if len(inv.get_hosts(tag)) == 0:
        msg = "No Host match for {} in {}".format(request.args.get('host'), env)
        return make_response(json.dumps({'error': msg}),400) 

     if eip == False:
         #print "Checking IP of {} for Env: {}".format(tag,env)
         ipaddr = ','.join([ ip.name for ip in inv.get_hosts(tag)])
     else:
         #print "Checking EIP of {} for Env: {}".format(tag,env)
         data = ansible.runner.Runner(
                        module_name='debug', 
                        pattern=tag, 
                        inventory = inv, 
                        transport = 'local', 
                        module_args="var=hostvars['{{ inventory_hostname }}']['ec2_ip_address']"
         ).run()
         element = data['contacted'].keys()
         ipaddr = ','.join([ ''.join(data['contacted'][e]['var'].values()) for e in element])
         if len(ipaddr) == 0:
            msg = "No EIP Found for {} in {}".format(request.args.get('host'), env)
            return make_response(json.dumps({'error': msg}),400) 
 
     return  ipaddr
Example #10
0
 def add_group(self, group):
     if isinstance(group, basestring):
         group = Group(group)
     try:
         self.log.debug('add inventory group: %s' % group.name)
         AnsibleInventory.add_group(self, group)
     except AnsibleError, emsg:
         raise InventoryError(emsg)
Example #11
0
class Runner(object):
    def __init__(self, playbook, display, hosts=None, options={}, passwords={}, vault_pass=None):

        self.options = Options()
        for k, v in options.iteritems():
            setattr(self.options, k, v)

        self.display = display
        self.display.verbosity = self.options.verbosity
        # executor has its own verbosity setting
        playbook_executor.verbosity = self.options.verbosity

        # gets data from YAML/JSON files
        self.loader = DataLoader()
        if vault_pass is not None:
            self.loader.set_vault_password(vault_pass)
        elif 'VAULT_PASS' in os.environ:
            self.loader.set_vault_password(os.environ['VAULT_PASS'])

        # all the variables from all the various places
        self.variable_manager = VariableManager()
        if self.options.python_interpreter is not None:
            self.variable_manager.extra_vars = {
                'ansible_python_interpreter': self.options.python_interpreter
            }

        # set inventory, using most of above objects
        self.inventory = Inventory(
            loader=self.loader, variable_manager=self.variable_manager,
            host_list=hosts)

        if len(self.inventory.list_hosts()) == 0:
            self.display.error("Provided hosts list is empty.")
            sys.exit(1)

        self.inventory.subset(self.options.subset)

        if len(self.inventory.list_hosts()) == 0:
            self.display.error("Specified limit does not match any hosts.")
            sys.exit(1)

        self.variable_manager.set_inventory(self.inventory)

        # 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,
            options=self.options,
            passwords=passwords)

    def run(self):
        # run playbook and get stats
        self.pbex.run()
        stats = self.pbex._tqm._stats

        return stats
Example #12
0
    def test_leading_range(self):
        i = Inventory(os.path.join(self.test_dir, "inventory", "test_leading_range"))
        hosts = i.list_hosts("test")
        expected_hosts = ["1.host", "2.host", "A.host", "B.host"]
        assert sorted(hosts) == sorted(expected_hosts)

        hosts2 = i.list_hosts("test2")
        expected_hosts2 = ["1.host", "2.host", "3.host"]
        assert sorted(hosts2) == sorted(expected_hosts2)
Example #13
0
    def test_leading_range(self):
        i = Inventory(os.path.join(self.test_dir, 'inventory','test_leading_range'))
        hosts = i.list_hosts('test')
        expected_hosts=['1.host','2.host','A.host','B.host']
        assert sorted(hosts) == sorted(expected_hosts)

        hosts2 = i.list_hosts('test2')
        expected_hosts2=['1.host','2.host','3.host']
        assert sorted(hosts2) == sorted(expected_hosts2)
Example #14
0
class MyInventory(Inventory):
    """
    this is my ansible inventory object.
    """

    def __init__(self):
        self.inventory = Inventory(my_inventory)
        #self.gen_inventory()

    def my_add_group(self, hosts, groupname, groupvars=None):
        """
        add hosts to a group
        """
        my_group = Group(name=groupname)

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

        # add hosts to group
        for host in hosts:
            # 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)
            my_host.set_variable('ansible_ssh_host', hostip)
            my_host.set_variable('ansible_ssh_port', hostport)
            my_host.set_variable('ansible_ssh_user', username)
            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.iteritems():
                if key not in ["hostname", "port", "username", "password"]:
                    my_host.set_variable(key, value)
            # add to group
            my_group.add_host(my_host)

        self.inventory.add_group(my_group)

    def gen_inventory(self):
        """
        add hosts to inventory.
        """
        if isinstance(self.resource, list):
            self.my_add_group(self.resource, 'default_group')
        elif isinstance(self.resource, dict):
            for groupname, hosts_and_vars in self.resource.iteritems():
                self.my_add_group(
                    hosts_and_vars.get("hosts"), groupname,
                    hosts_and_vars.get("vars"))
def build_inventory(loader, variable_manager, group_names, playbook_basedir):
    inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=['localhost'])

    # because we just pass in a "host list" which isn't a real inventory file,
    # we explicitly have to add all of the desired groups to the inventory. By
    # default an "all" group is created whenever a new inventory is created
    for group_name in group_names:
        if not inventory.get_group(group_name):
            inventory.add_group(Group(group_name))

    # because we are explicitly adding groups, we also need to make sure that a
    # playbook basedir is set so that `group_vars` can be loaded from the
    # correct directory.
    inventory.set_playbook_basedir(playbook_basedir)
    inventory_hosts = inventory.get_hosts()

    # for each group specified, ensure that the inventory's host (localhost) is
    # explicitly in the group.
    for group_name in group_names:
        group = inventory.get_group(group_name)
        if group.get_hosts():
            continue

        for host in inventory.get_hosts():
            group.add_host(host)

    return inventory
Example #16
0
 def render_GET(self, request):
     i = Inventory()
     inv = []
     try:
         hosts = i.get_hosts()
         groups = i.get_groups()
     except:
         return self.error_page(request)
     inv.extend([{"name": x.name, "type":"host"} for x in sorted(hosts)])
     inv.extend([{"name": x.name, "type":"group"} for x in sorted(groups)])
     return jsonify(request, inv)
 def __init__(self, host_list):
     self.loader = DataLoader()
     # this code is a bit ugly, because Ansible 2.4 switched the order
     # of the object and the pattern (InventoryManager is a argument to
     # VariableManager, and vice-versa on version <2.3)
     if ANSIBLE_24:
         self.im = InventoryManager(loader=self.loader,
                                    sources=[host_list, ])
         self.vm = VariableManager(loader=self.loader, inventory=self.im)
     else:
         self.vm = VariableManager()
         self.im = Inventory(self.loader, self.vm, host_list)
Example #18
0
    def pytest_generate_tests(self, metafunc):
        log.debug("pytest_generate_tests() called")

        if 'ansible_host' in metafunc.fixturenames:
            # assert required --ansible-* parameters were used
            self.assert_required_ansible_parameters()
            # TODO: this doesn't support function/cls fixture overrides
            try:
                inventory_manager = Inventory(self.config.getvalue('ansible_inventory'))
            except ansible.errors.AnsibleError, e:
                raise pytest.UsageError(e)
            pattern = self.config.getvalue('ansible_host_pattern')
            metafunc.parametrize("ansible_host", inventory_manager.list_hosts(pattern))
Example #19
0
class MyInventory(Inventory):
    def __init__(self, resource):
        """
        resource的数据格式是一个列表字典,比如
            {
                "group1": {
                    "hosts": [{"hostname": "10.10.10.10", "port": "22", "username": "******", "password": "******"}, ...],
                    "vars": {"var1": value1, "var2": value2, ...}
                }
            }

        如果你只传入1个列表,这默认该列表内的所有主机属于my_group组,比如
            [{"hostname": "10.10.10.10", "port": "22", "username": "******", "password": "******"}, ...]
        """
        self.resource = resource
        self.inventory = Inventory(host_list=[])
        self.gen_inventory()
        
    def my_add_group(self, hosts, groupname, groupvars=None):
        my_group = Group(name=groupname)
        if groupvars:
            for key, value in groupvars.iteritems():
                my_group.set_variable(key, value)

        for host in hosts:
            hostname = host.get('hostname')
            hostip = host.get('ip', hostname)
            hostport = host.get('port', 22)
            username = host.get('username', 'root')
            password = host.get('password')
            ssh_key = host.get("ssh_key")
            my_host = Host(name=hostname, port=hostport)
            my_host.set_variable('ansible_ssh_host', hostip)
            my_host.set_variable('ansible_ssh_port', hostport)
            my_host.set_variable('ansible_ssh_user', username)
            my_host.set_variable('ansible_ssh_pass', password)
            my_host.set_variable('ansible_ssh_private_key_file', ssh_key)

            for key, value in host.iteritems():
                if key not in ['hostname', 'port', 'username', 'password']:
                    my_host.set_variable(key, value)
            my_group.add_host(my_host)
        self.inventory.add_group(my_group)
        

    def gen_inventory(self):
        if isinstance(self.resource, list):
            self.my_add_group(self.resource, 'default_group')
        elif isinstance(self.resource, dict):
            for groupname, hosts_and_vars in self.resource.iteritems():
                self.my_add_group(hosts_and_vars.get('hosts'), groupname, hosts_and_vars.get('vars'))
def notifyCanary():

    inven = Inventory(host_list=path["hosts"]);
    groups = inven.get_groups();
    for group in groups:
        # print group.get_variables()
        if group.name == "canary":
            hosts = inven.get_hosts(group.name)
            for host in hosts:
                host_ip = host.get_variables()['ansible_ssh_host']
                url = 'http://'+proxy_server+':'+proxy_port+"/canary"
                payload = {'host': host_ip}
                headers = {'content-type': 'application/json'}
                response = requests.post(url, data=payload, headers=headers)
    def configure_hosts(self, reservation_obj, playbook):
        inven = Inventory(host_list=reservation_obj.keys())
        for host in inven.get_hosts():
            for key, value in reservation_obj[host.name].items():
                host.set_variable(key, value)

        stats = callbacks.AggregateStats()
        playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
        runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY)
        pb = PlayBook(inventory=inven,
                      playbook=playbook,
                      stats=stats,
                      callbacks=playbook_cb,
                      runner_callbacks=runner_cb)
        pb.run()
Example #22
0
    def run(self):

        results = None

        super(InventoryCLI, self).run()

        # Initialize needed objects
        if getattr(self, '_play_prereqs', False):
            self.loader, self.inventory, self.vm = self._play_prereqs(self.options)
        else:
            # fallback to pre 2.4 way of initialzing
            from ansible.vars import VariableManager
            from ansible.inventory import Inventory

            self._new_api = False
            self.loader = DataLoader()
            self.vm = VariableManager()

            # use vault if needed
            if self.options.vault_password_file:
                vault_pass = CLI.read_vault_password_file(self.options.vault_password_file, loader=self.loader)
            elif self.options.ask_vault_pass:
                vault_pass = self.ask_vault_passwords()
            else:
                vault_pass = None

            if vault_pass:
                self.loader.set_vault_password(vault_pass)
                # actually get inventory and vars

            self.inventory = Inventory(loader=self.loader, variable_manager=self.vm, host_list=self.options.inventory)
            self.vm.set_inventory(self.inventory)

        if self.options.host:
            hosts = self.inventory.get_hosts(self.options.host)
            if len(hosts) != 1:
                raise AnsibleOptionsError("You must pass a single valid host to --hosts parameter")

            myvars = self._get_host_variables(host=hosts[0])
            self._remove_internal(myvars)

            # FIXME: should we template first?
            results = self.dump(myvars)

        elif self.options.graph:
            results = self.inventory_graph()
        elif self.options.list:
            top = self._get_group('all')
            if self.options.yaml:
                results = self.yaml_inventory(top)
            else:
                results = self.json_inventory(top)
            results = self.dump(results)

        if results:
            # FIXME: pager?
            display.display(results)
            exit(0)

        exit(1)
Example #23
0
    def __init__(self, playbook):
        self.inventory = Inventory(host_list=[])
        self.playbook = playbook
 
        self.stac_nodes_group = Group(name='stac_nodes')
        self.inventory.add_group(self.stac_nodes_group)
 
        self.inv_vars = dict()
Example #24
0
 def get_variables(self, hostname, **kwargs):
     result = BaseInventory.get_variables(self, hostname, **kwargs)
     if hasattr(self, 'get_host_variables'):
         result = utils.combine_vars(
             result,
             self.get_host_variables(
                 hostname, vault_password=self._vault_password))
     return PloyInventoryDict(result)
Example #25
0
def find_host_in_inventory(base, host_name):
  loader = DataLoader()
  var_manager = VariableManager()
  host_vars = None
  host_info = []

  for i in get_inventory_files(base):
    try:
      inv = Inventory(host_list='%s/%s' %(base, i), loader=loader, variable_manager=var_manager)
      host_vars = inv.get_vars(host_name)
    except AnsibleError as err:
#      print '%s in inventory %s' %(err, i)
      inv.host_list = None
      inv.refresh_inventory()
      continue

  if host_vars:
    ssh_host = host_vars.get('ansible_ssh_host', None)
    ssh_user = host_vars.get('ansible_ssh_user', None)
    ssh_key  = host_vars.get('ansible_ssh_private_key_file', None)
    ssh_port = host_vars.get('ansible_ssh_port', 22)
  else:
    raise Exception('Host %s is not present in any inventory %s' %(host_name, ' '.join(get_inventory_files(base))))

  if not ssh_host or not ssh_user:
    raise Exception('ansible_ssh_host and ansible_ssh_user required.')

  host_info.append(str('%s@%s' %(ssh_user, ssh_host)))

  if ssh_key:
    ssh_key = os.path.expanduser(ssh_key)

    if not os.path.isabs(ssh_key):
      ssh_key = os.path.abspath(ssh_key)

    if os.path.exists(ssh_key):
      host_info += ['-i', str(ssh_key)]
    else:
      print ("SSH key %s doesn't exists please check path." %(ssh_key))

  if ssh_port:
    if not int(ssh_port) == min(max(int(ssh_port), 0), 65535):
      raise Exception('Incorrect port number given')
    host_info += ['-p', str(ssh_port)]

  return host_info
Example #26
0
    def run(experimentid, playbook_path, host_list=C.DEFAULT_HOST_LIST):
        # print self.file_name
        # print self.path
        # print self.file.read()
        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', 'listhosts', 'listtasks', 'listtags', 'syntax', 'host_key_checking'])
        variable_manager = VariableManager()

        loader = DataLoader()
        options = Options(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=None,
                      become_method=None, become_user='******', verbosity=None, check=False, listhosts=None, listtasks=None, listtags=None, syntax=None, host_key_checking=False)
        passwords = dict(vault_pass='******')

        # create inventory and pass to var manager
        inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=host_list)

        #print host_list
        #print inventory.split_host_pattern(host_list)
        #inventory.parse_inventory(host_list)
        #print inventory.host_list
        #print inventory.get_hosts()
        variable_manager.set_inventory(inventory)

        # create the playbook executor, which manages running the plays via a task queue manager
        # playbook_path = self.file_name

        pbex = PlaybookExecutor(playbooks=[playbook_path], inventory=inventory, variable_manager=variable_manager,
                            loader=loader, options=options, passwords=passwords)
        # pbex._tqm._callback_plugins.append(C.DEFAULT_STDOUT_CALLBACK)
        # pbex._tqm._callback_plugins.append('default')
        # pbex._tqm._callback_plugins.append('default')
        # pbex._tqm._callback_plugins.append('default')

        pbex._tqm._callback_plugins.append(AnsibleCallbackModule(experimentid))
        # pbex._tqm._callback_plugins.append(TreeCallbackModule())

        pbex._playbooks
        results = pbex.run()

        # dont forget to clear host cache after finish, otherwise will raise exception when host_list changes in the new round
        inventory.clear_pattern_cache()
    def run(self, terms, variables=None, **kwargs):

        host_list = []

        for term in terms:
            patterns = Inventory.order_patterns(Inventory.split_host_pattern(term))

            for p in patterns:
                that = self.get_hosts(variables, p)
                if p.startswith("!"):
                    host_list = [ h for h in host_list if h not in that]
                elif p.startswith("&"):
                    host_list = [ h for h in host_list if h in that ]
                else:
                    host_list.extend(that)

        # return unique list
        return list(set(host_list))
    def __init__(self, inventory_file, module_name, module_args):
        loader = DataLoader()
        variable_manager = VariableManager()

        inventory = Inventory(loader=loader,
                              variable_manager=variable_manager,
                              host_list=inventory_file)
        variable_manager.set_inventory(inventory)

        hosts = [x.name for x in inventory.get_hosts()]

        play_source = {
            "name": "Ansible Play",
            "hosts": hosts,
            "gather_facts": "no",
            "tasks": [{
                "action": {
                    "module": module_name,
                    "args": module_args
                }
            }]
        }
        logging.info(play_source)
        play = Play().load(play_source,
                           variable_manager=variable_manager,
                           loader=loader)

        Options = namedtuple('Options', ['connection', 'module_path', 'forks',
                                         'become', 'become_method',
                                         'become_user', 'check'])
        options = Options(connection='local',
                          module_path='',
                          forks=100,
                          become=None,
                          become_method=None,
                          become_user=None,
                          check=False)

        self.inventory = inventory
        self.variable_manager = variable_manager
        self.loader = loader
        self.play = play
        self.options = options
        self.passwords = {"vault_pass": '******'}
Example #29
0
def playbook(**kwargs):
    group_vars = kwargs['group_vars']
    groups = kwargs['groups']
    host_list = kwargs['host_list']
    playbook_basedir = os.sep.join([ANSIBLE_PLAYBOOK_PATH, kwargs['playbook_basedir']])
    playbooks = []
    for pb in kwargs['playbooks']:
        playbooks.append(os.sep.join([playbook_basedir, pb]))

    job_id = kwargs['job_id']
    loader = DataLoader()
    vars = VariableManager()
    # 指定inventory为一个目录,设置所有主机,包含group和host
    invertory = Inventory(loader, vars,
                          host_list=host_list)

    invertory.set_playbook_basedir(playbook_basedir)
    for group_name, hosts in groups.items():
        t_group = Group(group_name)
        for host in hosts:
            t_host = Host(host)
            t_group.add_host(t_host)
        invertory.add_group(t_group)

    vars.set_inventory(invertory)
    display = LogDisplay(logname=job_id)
    callback = CALLBACKMODULE[CALLBACK](display=display)
    Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'timeout', 'remote_user',
                                     'ask_pass', 'private_key_file', 'ssh_common_args', 'ssh_extra_args',
                                     'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user',
                                     'ask_value_pass', 'verbosity', 'check', 'listhosts',
                                     'listtags', 'listtasks', 'syntax'])

    options = Options(connection='smart', module_path='/usr/share/ansible', forks=100, timeout=10,
                      remote_user='******', ask_pass=False, private_key_file=None, ssh_common_args=None,
                      ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=None,
                      become_method=None, become_user='******', ask_value_pass=False, verbosity=None,
                      check=False, listhosts=None, listtags=None, listtasks=None, syntax=None)

    passwords = dict()
    pb_executor = PlaybookExecutor(playbooks, invertory, vars, loader, options, passwords)
    pb_executor._tqm._stdout_callback = callback
    pb_executor.run()
    return display.get_log_json()
Example #30
0
class MyInventory(Inventory):
    """inventory"""

    def __init__(self, resource, loader, variable_manager):
        self.resource = resource
        self.inventory = Inventory(
            loader=loader, variable_manager=variable_manager, host_list=[])
        self.dynamic_inventory()

    def add_dynamic_group(self, hosts, groupname, groupvars=None):
        """dynamic inventory group"""
        my_group = Group(name=groupname)
        if groupvars:
            for key, value in groupvars.items():
                my_group.set_variable(key, value)
        for host in hosts:
            # 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)
            my_host.set_variable('ansible_ssh_host', hostip)
            my_host.set_variable('ansible_ssh_port', hostport)
            my_host.set_variable('ansible_ssh_user', username)
            my_host.set_variable('ansible_ssh_pass', password)
            my_host.set_variable('ansible_ssh_private_key_file', ssh_key)
            for key, value in host.items():
                if key not in ["hostname", "port", "username", "password"]:
                    my_host.set_variable(key, value)
            my_group.add_host(my_host)

        self.inventory.add_group(my_group)

    def dynamic_inventory(self):
        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 #31
0
 def simple_inventory(self):
     return Inventory(self.inventory_file)
Example #32
0
 def test_simple_string_ipv6_port(self):
     inventory = Inventory('[FE80:EF45::12:1]:2222,192.168.1.1')
     hosts = inventory.list_hosts()
     self.assertEqual(sorted(hosts),
                      sorted(['FE80:EF45::12:1', '192.168.1.1']))
Example #33
0
 def testinvalid_entry(self):
     Inventory('1234')
Example #34
0
 def test_incorrect_format(self):
     Inventory(
         os.path.join(self.test_dir, 'inventory', 'test_incorrect_format'))
Example #35
0
 def test_no_src(self):
     inventory = Inventory('127.0.0.1,')
     self.assertEqual(inventory.src(), None)
Example #36
0
class ConsoleCLI(CLI, cmd.Cmd):

    modules = []

    def __init__(self, args):

        super(ConsoleCLI, self).__init__(args)

        self.intro = 'Welcome to the ansible console.\nType help or ? to list commands.\n'

        self.groups = []
        self.hosts = []
        self.pattern = None
        self.variable_manager = None
        self.loader = None
        self.passwords = dict()

        self.modules = None
        cmd.Cmd.__init__(self)

    def parse(self):
        self.parser = CLI.base_parser(
            usage='%prog <host-pattern> [options]',
            runas_opts=True,
            inventory_opts=True,
            connect_opts=True,
            check_opts=True,
            vault_opts=True,
            fork_opts=True,
            module_opts=True,
        )

        # options unique to shell
        self.parser.add_option(
            '--step',
            dest='step',
            action='store_true',
            help="one-step-at-a-time: confirm each task before running")

        self.parser.set_defaults(cwd='*')

        super(ConsoleCLI, self).parse()

        display.verbosity = self.options.verbosity
        self.validate_conflicts(runas_opts=True,
                                vault_opts=True,
                                fork_opts=True)

    def get_names(self):
        return dir(self)

    def cmdloop(self):
        try:
            cmd.Cmd.cmdloop(self)
        except KeyboardInterrupt:
            self.do_exit(self)

    def set_prompt(self):
        login_user = self.options.remote_user or getpass.getuser()
        self.selected = self.inventory.list_hosts(self.options.cwd)
        prompt = "%s@%s (%d)[f:%s]" % (login_user, self.options.cwd,
                                       len(self.selected), self.options.forks)
        if self.options.become and self.options.become_user in [None, 'root']:
            prompt += "# "
            color = C.COLOR_ERROR
        else:
            prompt += "$ "
            color = C.COLOR_HIGHLIGHT
        self.prompt = stringc(prompt, color)

    def list_modules(self):
        modules = set()
        if self.options.module_path is not None:
            for i in self.options.module_path.split(os.pathsep):
                module_loader.add_directory(i)

        module_paths = module_loader._get_paths()
        for path in module_paths:
            if path is not None:
                modules.update(self._find_modules_in_path(path))
        return modules

    def _find_modules_in_path(self, path):

        if os.path.isdir(path):
            for module in os.listdir(path):
                if module.startswith('.'):
                    continue
                elif os.path.isdir(module):
                    self._find_modules_in_path(module)
                elif module.startswith('__'):
                    continue
                elif any(module.endswith(x) for x in C.BLACKLIST_EXTS):
                    continue
                elif module in C.IGNORE_FILES:
                    continue
                elif module.startswith('_'):
                    fullpath = '/'.join([path, module])
                    if os.path.islink(fullpath):  # avoids aliases
                        continue
                    module = module.replace('_', '', 1)

                module = os.path.splitext(module)[0]  # removes the extension
                yield module

    def default(self, arg, forceshell=False):
        """ actually runs modules """
        if arg.startswith("#"):
            return False

        if not self.options.cwd:
            display.error("No host found")
            return False

        if arg.split()[0] in self.modules:
            module = arg.split()[0]
            module_args = ' '.join(arg.split()[1:])
        else:
            module = 'shell'
            module_args = arg

        if forceshell is True:
            module = 'shell'
            module_args = arg

        self.options.module_name = module

        result = None
        try:
            check_raw = self.options.module_name in ('command', 'shell',
                                                     'script', 'raw')
            play_ds = dict(
                name="Ansible Shell",
                hosts=self.options.cwd,
                gather_facts='no',
                tasks=[
                    dict(action=dict(module=module,
                                     args=parse_kv(module_args,
                                                   check_raw=check_raw)))
                ])
            play = Play().load(play_ds,
                               variable_manager=self.variable_manager,
                               loader=self.loader)
        except Exception as e:
            display.error(u"Unable to build command: %s" % to_text(e))
            return False

        try:
            cb = 'minimal'  # FIXME: make callbacks configurable
            # now create a task queue manager to execute the play
            self._tqm = None
            try:
                self._tqm = TaskQueueManager(
                    inventory=self.inventory,
                    variable_manager=self.variable_manager,
                    loader=self.loader,
                    options=self.options,
                    passwords=self.passwords,
                    stdout_callback=cb,
                    run_additional_callbacks=C.DEFAULT_LOAD_CALLBACK_PLUGINS,
                    run_tree=False,
                )

                result = self._tqm.run(play)
            finally:
                if self._tqm:
                    self._tqm.cleanup()
                if self.loader:
                    self.loader.cleanup_all_tmp_files()

            if result is None:
                display.error("No hosts found")
                return False
        except KeyboardInterrupt:
            display.error('User interrupted execution')
            return False
        except Exception as e:
            display.error(to_text(e))
            # FIXME: add traceback in very very verbose mode
            return False

    def emptyline(self):
        return

    def do_shell(self, arg):
        """
        You can run shell commands through the shell module.

        eg.:
        shell ps uax | grep java | wc -l
        shell killall python
        shell halt -n

        You can use the ! to force the shell module. eg.:
        !ps aux | grep java | wc -l
        """
        self.default(arg, True)

    def do_forks(self, arg):
        """Set the number of forks"""
        if not arg:
            display.display('Usage: forks <number>')
            return
        self.options.forks = int(arg)
        self.set_prompt()

    do_serial = do_forks

    def do_verbosity(self, arg):
        """Set verbosity level"""
        if not arg:
            display.display('Usage: verbosity <number>')
        else:
            display.verbosity = int(arg)
            display.v('verbosity level set to %s' % arg)

    def do_cd(self, arg):
        """
            Change active host/group. You can use hosts patterns as well eg.:
            cd webservers
            cd webservers:dbservers
            cd webservers:!phoenix
            cd webservers:&staging
            cd webservers:dbservers:&staging:!phoenix
        """
        if not arg:
            self.options.cwd = '*'
        elif arg == '..':
            try:
                self.options.cwd = self.inventory.groups_for_host(
                    self.options.cwd)[1].name
            except Exception:
                self.options.cwd = ''
        elif arg in '/*':
            self.options.cwd = 'all'
        elif self.inventory.get_hosts(arg):
            self.options.cwd = arg
        else:
            display.display("no host matched")

        self.set_prompt()

    def do_list(self, arg):
        """List the hosts in the current group"""
        if arg == 'groups':
            for group in self.groups:
                display.display(group)
        else:
            for host in self.selected:
                display.display(host.name)

    def do_become(self, arg):
        """Toggle whether plays run with become"""
        if arg:
            self.options.become = C.mk_boolean(arg)
            display.v("become changed to %s" % self.options.become)
            self.set_prompt()
        else:
            display.display("Please specify become value, e.g. `become yes`")

    def do_remote_user(self, arg):
        """Given a username, set the remote user plays are run by"""
        if arg:
            self.options.remote_user = arg
            self.set_prompt()
        else:
            display.display(
                "Please specify a remote user, e.g. `remote_user root`")

    def do_become_user(self, arg):
        """Given a username, set the user that plays are run by when using become"""
        if arg:
            self.options.become_user = arg
        else:
            display.display(
                "Please specify a user, e.g. `become_user jenkins`")
            display.v("Current user is %s" % self.options.become_user)
        self.set_prompt()

    def do_become_method(self, arg):
        """Given a become_method, set the privilege escalation method when using become"""
        if arg:
            self.options.become_method = arg
            display.v("become_method changed to %s" %
                      self.options.become_method)
        else:
            display.display(
                "Please specify a become_method, e.g. `become_method su`")

    def do_check(self, arg):
        """Toggle whether plays run with check mode"""
        if arg:
            self.options.check = C.mk_boolean(arg)
            display.v("check mode changed to %s" % self.options.check)
        else:
            display.display(
                "Please specify check mode value, e.g. `check yes`")

    def do_diff(self, arg):
        """Toggle whether plays run with diff"""
        if arg:
            self.options.diff = C.mk_boolean(arg)
            display.v("diff mode changed to %s" % self.options.diff)
        else:
            display.display("Please specify a diff value , e.g. `diff yes`")

    def do_exit(self, args):
        """Exits from the console"""
        sys.stdout.write('\n')
        return -1

    do_EOF = do_exit

    def helpdefault(self, module_name):
        if module_name in self.modules:
            in_path = module_loader.find_plugin(module_name)
            if in_path:
                oc, a, _, _ = plugin_docs.get_docstring(in_path)
                if oc:
                    display.display(oc['short_description'])
                    display.display('Parameters:')
                    for opt in oc['options'].keys():
                        display.display('  ' +
                                        stringc(opt, C.COLOR_HIGHLIGHT) + ' ' +
                                        oc['options'][opt]['description'][0])
                else:
                    display.error('No documentation found for %s.' %
                                  module_name)
            else:
                display.error(
                    '%s is not a valid command, use ? to list all valid commands.'
                    % module_name)

    def complete_cd(self, text, line, begidx, endidx):
        mline = line.partition(' ')[2]
        offs = len(mline) - len(text)

        if self.options.cwd in ('all', '*', '\\'):
            completions = self.hosts + self.groups
        else:
            completions = [
                x.name for x in self.inventory.list_hosts(self.options.cwd)
            ]

        return [
            to_native(s)[offs:] for s in completions
            if to_native(s).startswith(to_native(mline))
        ]

    def completedefault(self, text, line, begidx, endidx):
        if line.split()[0] in self.modules:
            mline = line.split(' ')[-1]
            offs = len(mline) - len(text)
            completions = self.module_args(line.split()[0])

            return [s[offs:] + '=' for s in completions if s.startswith(mline)]

    def module_args(self, module_name):
        in_path = module_loader.find_plugin(module_name)
        oc, a, _, _ = plugin_docs.get_docstring(in_path)
        return list(oc['options'].keys())

    def run(self):

        super(ConsoleCLI, self).run()

        sshpass = None
        becomepass = None
        vault_pass = None

        # hosts
        if len(self.args) != 1:
            self.pattern = 'all'
        else:
            self.pattern = self.args[0]
        self.options.cwd = self.pattern

        # dynamically add modules as commands
        self.modules = self.list_modules()
        for module in self.modules:
            setattr(
                self,
                'do_' + module,
                lambda arg, module=module: self.default(module + ' ' + arg))
            setattr(self,
                    'help_' + module,
                    lambda module=module: self.helpdefault(module))

        self.normalize_become_options()
        (sshpass, becomepass) = self.ask_passwords()
        self.passwords = {'conn_pass': sshpass, 'become_pass': becomepass}

        self.loader = DataLoader()

        if self.options.vault_password_file:
            # read vault_pass from a file
            vault_pass = CLI.read_vault_password_file(
                self.options.vault_password_file, loader=self.loader)
            self.loader.set_vault_password(vault_pass)
        elif self.options.ask_vault_pass:
            vault_pass = self.ask_vault_passwords()
            self.loader.set_vault_password(vault_pass)

        self.variable_manager = VariableManager()
        self.inventory = Inventory(loader=self.loader,
                                   variable_manager=self.variable_manager,
                                   host_list=self.options.inventory)
        self.variable_manager.set_inventory(self.inventory)

        no_hosts = False
        if len(self.inventory.list_hosts()) == 0:
            # Empty inventory
            no_hosts = True
            display.warning(
                "provided hosts list is empty, only localhost is available")

        self.inventory.subset(self.options.subset)
        hosts = self.inventory.list_hosts(self.pattern)
        if len(hosts) == 0 and not no_hosts:
            raise AnsibleError(
                "Specified hosts and/or --limit does not match any hosts")

        self.groups = self.inventory.list_groups()
        self.hosts = [x.name for x in hosts]

        # This hack is to work around readline issues on a mac:
        #  http://stackoverflow.com/a/7116997/541202
        if 'libedit' in readline.__doc__:
            readline.parse_and_bind("bind ^I rl_complete")
        else:
            readline.parse_and_bind("tab: complete")

        histfile = os.path.join(os.path.expanduser("~"),
                                ".ansible-console_history")
        try:
            readline.read_history_file(histfile)
        except IOError:
            pass

        atexit.register(readline.write_history_file, histfile)
        self.set_prompt()
        self.cmdloop()
Example #37
0
class AnsibleTask(object):
    def __init__(self, targetHost, user, password, os, port):

        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'
        ])
        self.options = Options(
            listtags=False,
            listtasks=False,
            listhosts=False,
            syntax=False,
            connection='ssh',
            module_path='/usr/lib/python2.7/site-packages/ansible/modules',
            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=False,
            become_method=None,
            become_user=None,
            verbosity=0,
            check=False)

        # initialize needed objects
        self.variable_manager = VariableManager()

        # self.options = Options(
        #                   listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh',
        #                   module_path='/usr/lib/python2.7/site-packages/ansible/modules', forks=100,
        #                   remote_user='******', private_key_file=None, ssh_common_args=None, ssh_extra_args=None,
        #                   sftp_extra_args=None, scp_extra_args=None, become=False, become_method=None, become_user='******',
        #                   verbosity=None, check=False
        #               )
        self.passwords = {}
        self.loader = DataLoader()
        hosts = '''{host} ansible_ssh_user="******"  ansible_ssh_pass="******" ansible_ssh_port={port}'''.format(
            host=targetHost, user=user, password=password, port=port)
        if os == "windows":
            hosts = hosts + """ ansible_connection="winrm" ansible_winrm_server_cert_validation=ignore"""
        # create inventory and pass to var manager
        self.hostsFile = NamedTemporaryFile(delete=False)
        # self.hostsFile.write()
        self.hostsFile.write("""[run_hosts]
        %s
        """ % hosts)
        self.hostsFile.close()
        self.inventory = Inventory(loader=self.loader,
                                   variable_manager=self.variable_manager,
                                   host_list=self.hostsFile.name)
        self.variable_manager.set_inventory(self.inventory)

    def ansiblePlay(self, module, args):
        # create play with tasks
        # args = "ls /"
        play_source = dict(name="Ansible Play",
                           hosts='all',
                           gather_facts='no',
                           tasks=[
                               dict(action=dict(module=module, args=args),
                                    register='shell_out'),
                           ])
        print "Ansible:"
        print play_source
        play = Play().load(play_source,
                           variable_manager=self.variable_manager,
                           loader=self.loader)

        # run it
        results_callback = ResultCallback()
        tqm = None
        try:
            tqm = TaskQueueManager(
                inventory=self.inventory,
                variable_manager=self.variable_manager,
                loader=self.loader,
                options=self.options,
                passwords=self.passwords,
                stdout_callback=results_callback,
            )
            result = tqm.run(play)
            if results_callback.error_msg:
                results_callback.result = {
                    'msg': "地址或用户名错误,主机连接失败",
                    'flag': False
                }
            else:
                if results_callback.result == None:
                    data = {'msg': "执行命令有问题或执行不成功", 'flag': False}
                    return data
                else:
                    results_callback.result["flag"] = True
            return results_callback.result
        except AnsibleParserError:
            code = 1001
            results = {'msg': "地址或用户名错误,主机连接失败", 'flag': False}
            return code, results
        finally:
            if tqm is not None:
                tqm.cleanup()
                os.remove(self.hostsFile.name)
                self.inventory.clear_pattern_cache()
Example #38
0
def process_playbooks(playbooks_to_run, bitbucket_user, target_ip, hostname, ansible_ssh_pass):
   playbooks = set(playbooks_to_run)
   bitbucket = "https://" + bitbucket_user + "@stash.veritas.com/scm/iac/"
   ansible_user = "******"
   directory = "/home/" + getpass.getuser() + '/runPlaybooks' + str(os.getpid())
   inventory = "jenkins.inv"

   run_command = "ansible-playbook -i " + inventory + " local.yml"
   pull_command = "ansible-galaxy install -r requirements.yml"
   clone_command = "git clone "

   pattern_mint = "playbook-mint"
   prog = re.compile(pattern_mint)

   create a folder to run multiple playbooks
   try:
      os.system("mkdir " + directory)
      os.chdir(directory)
   except Exception as e:
      raise Exception("Failed to create diretory: %s" % str(e))
      sys.exit()

   # pull playbooks
   try:
      for play in playbooks:
         clone_playbook = bitbucket + play + ".git"
         os.system(clone_command + clone_playbook)
   except Exception as e:
      raise Exception("Failed to clone playbooks: %s" % str(e))
      sys.exit()

   # pull roles for playbooks
   try:
      for play in playbooks:
        os.chdir(play)
        print("in dir: " + play)
        os.system(pull_command)
        os.chdir(directory)
   except Exception as e:
      raise Exception("Failed to pull roles: %s" % str(e))
      sys.exit()
   # invertory and run playbooks
   Options = namedtuple('Options', ['connection',  'forks', 'become', 'become_method',
    'become_user', 'check', 'listhosts', 'listtasks', 'listtags', 'syntax', 'module_path'])

   variable_manager = VariableManager()
   loader = DataLoader()
   options = Options(connection='local', forks=100, become=None, become_method=None, become_user=None,
       check=False, listhosts=False, listtasks=False, listtags=False, syntax=False, module_path="")
   passwords = ansible_ssh_pass #dict(vault_pass='******')

   inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=target_ip)#'localhost')
   variable_manager.set_inventory(inventory)
   playbooks = playbooks_to_run #["./test.yaml"]

   executor = PlaybookExecutor(
              playbooks=playbooks,
              inventory=inventory,
              variable_manager=variable_manager,
              loader=loader,
              options=options,
              passwords=passwords)

   executor.run()
def exec_playbook(file_name):
    exec_result = {'success': [], 'failed': [], 'unreachable': []}
    host_list = ['localhost', 'www.example.com', 'www.google.com']
    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'
    ])

    # initialize needed objects
    variable_manager = VariableManager()
    loader = DataLoader()
    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=None,
                      become_user=None,
                      verbosity=None,
                      check=False)

    passwords = dict()

    # create inventory and pass to var manager
    inventory = Inventory(loader=loader,
                          variable_manager=variable_manager,
                          host_list=host_list)
    variable_manager.set_inventory(inventory)

    # create play with tasks

    playbook = Playbook(loader).load(file_name,
                                     variable_manager=variable_manager,
                                     loader=loader)
    plays = playbook.get_plays()
    # actually run it
    tqm = None
    callback = ResultsCollector()
    try:
        tqm = TaskQueueManager(
            inventory=inventory,
            variable_manager=variable_manager,
            loader=loader,
            options=options,
            passwords=passwords,
        )
        tqm._stdout_callback = callback
        for play in plays:
            result = tqm.run(play)
    finally:
        if tqm is not None:
            tqm.cleanup()

    print "UP ***********"
    for host, result in callback.host_ok.items():
        exec_result['success'].append(dict(host=host, result=result._result))

    print "FAILED *******"
    for host, result in callback.host_failed.items():
        exec_result['failed'].append(dict(host=host,
                                          msg=result._result['msg']))

    print "DOWN *********"
    for host, result in callback.host_unreachable.items():
        exec_result['unreachable'].append(
            dict(host=host, msg=result._result['msg']))
    return exec_result
Example #40
0
 def dir_inventory(self):
     return Inventory(self.inventory_dir)
Example #41
0
 def complex_inventory(self):
     return Inventory(self.complex_inventory_file)
Example #42
0
 def script_inventory(self):
     return Inventory(self.inventory_script)
Example #43
0
 def test_combined_range(self):
     i = Inventory(
         os.path.join(self.test_dir, 'inventory', 'test_combined_range'))
     hosts = i.list_hosts('test')
     expected_hosts = ['host1A', 'host2A', 'host1B', 'host2B']
     assert sorted(hosts) == sorted(expected_hosts)
Example #44
0
def exec_ansible(module, args, host, redisKey):
    class ResultCallback(CallbackBase):
        def v2_runner_on_ok(self, result, **kwargs):
            host = result._host
            self.data = json.dumps({host.name: result._result}, indent=4)
            print(self.data)

    Options = namedtuple('Options', [
        'connection', 'module_path', 'forks', 'become', 'become_method',
        'become_user', 'check'
    ])
    # initialize needed objects
    variable_manager = VariableManager()
    loader = DataLoader()
    #module_path参数指定本地ansible模块包的路径
    options = Options(
        connection='smart',
        module_path='/usr/local/lib/python3.5/site-packages/ansible/modules/',
        forks=100,
        become=None,
        become_method=None,
        become_user='******',
        check=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
    #host_list指定本地ansible的hosts文件
    inventory = Inventory(loader=loader,
                          variable_manager=variable_manager,
                          host_list='/etc/ansible/hosts')
    variable_manager.set_inventory(inventory)

    # create play with tasks
    play_source = dict(
        name="Ansible Play",
        #hosts可以指定inventory中的一组主机,也可指定单台主机
        hosts=host,
        gather_facts='no',
        #task执行列表,如果想一次执行多个任务,可以在列表中添加任务
        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)
    except Exception as err:
        if redisKey: DsRedis.OpsAnsibleModel.lpush(redisKey, data=err)
    finally:
        if tqm is not None:
            tqm.cleanup
        msg = json.loads(results_callback.data)
        new_msg = {}
        for k in msg:
            v = msg[k]
            if 'stdout' in v:
                v = v['stdout']
            new_msg[k] = v
        new_msg = str(new_msg).replace('\\r\\n', '\<br \>')
        #new_msg=str(msg).replace('\\r\\n','\<br \/\>')
        DsRedis.OpsAnsibleModel.lpush(redisKey, data=new_msg)
        #DsRedis.OpsAnsibleModel.lpush(redisKey, data=msg)
        return msg
Example #45
0
    def run(self):

        super(ConsoleCLI, self).run()

        sshpass = None
        becomepass = None
        vault_pass = None

        # hosts
        if len(self.args) != 1:
            self.pattern = 'all'
        else:
            self.pattern = self.args[0]
        self.options.cwd = self.pattern

        # dynamically add modules as commands
        self.modules = self.list_modules()
        for module in self.modules:
            setattr(
                self,
                'do_' + module,
                lambda arg, module=module: self.default(module + ' ' + arg))
            setattr(self,
                    'help_' + module,
                    lambda module=module: self.helpdefault(module))

        self.normalize_become_options()
        (sshpass, becomepass) = self.ask_passwords()
        self.passwords = {'conn_pass': sshpass, 'become_pass': becomepass}

        self.loader = DataLoader()

        if self.options.vault_password_file:
            # read vault_pass from a file
            vault_pass = CLI.read_vault_password_file(
                self.options.vault_password_file, loader=self.loader)
            self.loader.set_vault_password(vault_pass)
        elif self.options.ask_vault_pass:
            vault_pass = self.ask_vault_passwords()
            self.loader.set_vault_password(vault_pass)

        self.variable_manager = VariableManager()
        self.inventory = Inventory(loader=self.loader,
                                   variable_manager=self.variable_manager,
                                   host_list=self.options.inventory)
        self.variable_manager.set_inventory(self.inventory)

        no_hosts = False
        if len(self.inventory.list_hosts()) == 0:
            # Empty inventory
            no_hosts = True
            display.warning(
                "provided hosts list is empty, only localhost is available")

        self.inventory.subset(self.options.subset)
        hosts = self.inventory.list_hosts(self.pattern)
        if len(hosts) == 0 and not no_hosts:
            raise AnsibleError(
                "Specified hosts and/or --limit does not match any hosts")

        self.groups = self.inventory.list_groups()
        self.hosts = [x.name for x in hosts]

        # This hack is to work around readline issues on a mac:
        #  http://stackoverflow.com/a/7116997/541202
        if 'libedit' in readline.__doc__:
            readline.parse_and_bind("bind ^I rl_complete")
        else:
            readline.parse_and_bind("tab: complete")

        histfile = os.path.join(os.path.expanduser("~"),
                                ".ansible-console_history")
        try:
            readline.read_history_file(histfile)
        except IOError:
            pass

        atexit.register(readline.write_history_file, histfile)
        self.set_prompt()
        self.cmdloop()
Example #46
0
class TestInventory(unittest.TestCase):

    patterns = {
        'a': ['a'],
        'a, b': ['a', 'b'],
        'a , b': ['a', 'b'],
        ' a,b ,c[1:2] ': ['a', 'b', 'c[1:2]'],
        '9a01:7f8:191:7701::9': ['9a01:7f8:191:7701::9'],
        '9a01:7f8:191:7701::9,9a01:7f8:191:7701::9':
        ['9a01:7f8:191:7701::9', '9a01:7f8:191:7701::9'],
        '9a01:7f8:191:7701::9,9a01:7f8:191:7701::9,foo':
        ['9a01:7f8:191:7701::9', '9a01:7f8:191:7701::9', 'foo'],
        'foo[1:2]': ['foo[1:2]'],
        'a::b': ['a::b'],
        'a:b': ['a', 'b'],
        ' a : b ': ['a', 'b'],
        'foo:bar:baz[1:2]': ['foo', 'bar', 'baz[1:2]'],
    }

    pattern_lists = [[['a'], ['a']], [['a', 'b'], ['a', 'b']],
                     [['a, b'], ['a', 'b']],
                     [['9a01:7f8:191:7701::9', '9a01:7f8:191:7701::9,foo'],
                      ['9a01:7f8:191:7701::9', '9a01:7f8:191:7701::9', 'foo']]]

    # pattern_string: [ ('base_pattern', (a,b)), ['x','y','z'] ]
    # a,b are the bounds of the subscript; x..z are the results of the subscript
    # when applied to string.ascii_letters.

    subscripts = {
        'a': [('a', None), list(string.ascii_letters)],
        'a[0]': [('a', (0, None)), ['a']],
        'a[1]': [('a', (1, None)), ['b']],
        'a[2:3]': [('a', (2, 3)), ['c', 'd']],
        'a[-1]': [('a', (-1, None)), ['Z']],
        'a[-2]': [('a', (-2, None)), ['Y']],
        'a[48:]': [('a', (48, -1)), ['W', 'X', 'Y', 'Z']],
        'a[49:]': [('a', (49, -1)), ['X', 'Y', 'Z']],
        'a[1:]': [('a', (1, -1)),
                  list(string.ascii_letters[1:])],
    }

    ranges_to_expand = {
        'a[1:2]': ['a1', 'a2'],
        'a[1:10:2]': ['a1', 'a3', 'a5', 'a7', 'a9'],
        'a[a:b]': ['aa', 'ab'],
        'a[a:i:3]': ['aa', 'ad', 'ag'],
        'a[a:b][c:d]': ['aac', 'aad', 'abc', 'abd'],
        'a[0:1][2:3]': ['a02', 'a03', 'a12', 'a13'],
        'a[a:b][2:3]': ['aa2', 'aa3', 'ab2', 'ab3'],
    }

    def setUp(self):
        v = VariableManager()
        fake_loader = DictDataLoader({})

        self.i = Inventory(loader=fake_loader,
                           variable_manager=v,
                           host_list='')

    def test_split_patterns(self):

        for p in self.patterns:
            r = self.patterns[p]
            self.assertEqual(r, self.i.split_host_pattern(p))

        for p, r in self.pattern_lists:
            self.assertEqual(r, self.i.split_host_pattern(p))

    def test_ranges(self):

        for s in self.subscripts:
            r = self.subscripts[s]
            self.assertEqual(r[0], self.i._split_subscript(s))
            self.assertEqual(
                r[1],
                self.i._apply_subscript(list(string.ascii_letters), r[0][1]))

    def test_expand_hostname_range(self):

        for e in self.ranges_to_expand:
            r = self.ranges_to_expand[e]
            self.assertEqual(r, expand_hostname_range(e))
Example #47
0
 def test_get_hosts(self):
     inventory = Inventory('127.0.0.1,192.168.1.1')
     hosts = inventory.get_hosts('!10.0.0.1')
     hosts_all = inventory.get_hosts('all')
     self.assertEqual(sorted(hosts), sorted(hosts_all))
Example #48
0
 def test_alpha_end_before_beg(self):
     Inventory(
         os.path.join(self.test_dir, 'inventory',
                      'test_alpha_end_before_beg'))
Example #49
0
 def test_simple_string_ipv4(self):
     inventory = Inventory('127.0.0.1,192.168.1.1')
     hosts = inventory.list_hosts()
     self.assertEqual(sorted(hosts), sorted(['127.0.0.1', '192.168.1.1']))
#initialize needed objects
variable_manager = VariableManager()
loader = DataLoader()
options = Options(connection='local',
                  module_path='',
                  forks=100,
                  become=True,
                  become_method='sudo',
                  become_user='******',
                  check=False)
passwords = dict(vault_pass='******')

#create inventory and pass to var manager
inventory = Inventory(loader=loader,
                      variable_manager=variable_manager,
                      host_list='/etc/ansible/hosts')
variable_manager.set_inventory(inventory)

#create play with tasks
play_src = dict(
    name="server list",
    hosts="192.168.10.238",
    gather_facts="no",
    become="true",
    tasks=[
        dict(name="show facts about all servers",
             action=dict(module="os_server_facts",
                         auth=dict(auth_url="http://192.168.10.238:5000/v2.0",
                                   username="******",
                                   password="******",
Example #51
0
 def test_missing_end(self):
     Inventory(os.path.join(self.test_dir, 'inventory', 'test_missing_end'))
Example #52
0
 def __init__(self, resource, loader, variable_manager):
     self.resource = resource
     # 创建主机配置清单对象
     self.inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=[])
     self.dynamic_inventory()
Example #53
0
 def test_invalid_range(self):
     Inventory(
         os.path.join(self.test_dir, 'inventory', 'test_incorrect_range'))
Example #54
0
    def run_playbook(self,
                     playbook_file,
                     playbook_variables=None,
                     option_overrides=None):
        """
        Run a playbook from file with the variables provided.

        :param playbook_file: the location of the playbook
        :param playbook_variables: extra variables for the playbook
        """
        # on the first run, the playbook that initializes the
        # inventory will not have yet run, so we should ensure
        # that the directory for it exists, at the least, so
        # ansible doesn't complain
        if not exists(self.host_list):
            mkdir(self.host_list)

        variable_manager = VariableManager()
        data_loader = DataLoader()
        inventory = Inventory(loader=data_loader,
                              variable_manager=variable_manager,
                              host_list=self.host_list)
        variable_manager.set_inventory(inventory)
        variable_manager.extra_vars = playbook_variables

        # until Ansible's display logic is less hack-ey we need
        # to mutate their global in __main__
        options = self.generate_playbook_options(playbook_file)
        display.verbosity = options.verbosity

        # we want to log everything so we can parse output
        # nicely later from files and don't miss output due
        # to the pretty printer, if it's on
        from ..oct import __file__ as root_dir
        callback_loader.add_directory(
            join(dirname(root_dir), 'ansible', 'oct', 'callback_plugins'))
        constants.DEFAULT_CALLBACK_WHITELIST = [
            'log_results', 'generate_junit'
        ]
        environ['ANSIBLE_LOG_ROOT_PATH'] = self.log_directory

        if options.verbosity == 1:
            # if the user has not asked for verbose output
            # we will use our pretty printer for progress
            # on the TTY
            constants.DEFAULT_STDOUT_CALLBACK = 'pretty_progress'

            # we really don't want output in std{err,out}
            # that we didn't put there, but some code in
            # Ansible calls directly to the Display, not
            # through a callback, so we need to ensure
            # that those raw calls don't go to stdout
            display.display = partial(display.display, log_only=True)
        else:
            # if the user asks for verbose output, we want
            # to give them nicer output than the default
            # plugin, anyway
            constants.DEFAULT_STDOUT_CALLBACK = 'default_with_output_lists'

        if option_overrides:
            for key in option_overrides:
                if hasattr(options, key):
                    setattr(options, key, option_overrides[key])

        result = PlaybookExecutor(playbooks=[playbook_file],
                                  inventory=inventory,
                                  variable_manager=variable_manager,
                                  loader=data_loader,
                                  options=options,
                                  passwords=None).run()

        if result != TaskQueueManager.RUN_OK:
            # TODO: this seems bad, but can we discover the thread here to join() it?
            sleep(0.2)
            raise ClickException('Playbook execution failed with code ' +
                                 str(result))
Example #55
0
 def test_simple_string_ipv6_vars(self):
     inventory = Inventory('[FE80:EF45::12:1]:2222,192.168.1.1')
     var = inventory.get_variables('FE80:EF45::12:1')
     self.assertEqual(var['ansible_ssh_port'], 2222)
Example #56
0
class AnsibleTask(object):
    def __init__(self, targetHost):
        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'
        ])

        # initialize needed objects
        self.variable_manager = VariableManager()

        self.options = Options(
            listtags=False,
            listtasks=False,
            listhosts=False,
            syntax=False,
            connection='smart',
            module_path='/usr/lib/python2.7/site-packages/ansible/modules',
            forks=100,
            remote_user='******',
            private_key_file=None,
            ssh_common_args=None,
            ssh_extra_args=None,
            sftp_extra_args=None,
            scp_extra_args=None,
            become=False,
            become_method=None,
            become_user='******',
            verbosity=None,
            check=False)
        self.passwords = dict(vault_pass='******')
        self.loader = DataLoader()

        # create inventory and pass to var manager
        self.hostsFile = NamedTemporaryFile(delete=False)
        self.hostsFile.write(targetHost)
        self.hostsFile.close()
        self.inventory = Inventory(loader=self.loader,
                                   variable_manager=self.variable_manager,
                                   host_list=self.hostsFile.name)
        self.variable_manager.set_inventory(self.inventory)

    def ansiblePlay(self, action):
        # create play with tasks
        args = "ls /"
        play_source = dict(
            name="Ansible Play",
            hosts='all',
            gather_facts='no',
            tasks=[
                dict(action=dict(module='shell', args=args),
                     register='shell_out'),
                dict(action=dict(module='debug',
                                 args=dict(msg='{{shell_out.stdout}}')))
            ])
        play = Play().load(play_source,
                           variable_manager=self.variable_manager,
                           loader=self.loader)

        # run it
        tqm = None
        try:
            tqm = TaskQueueManager(
                inventory=self.inventory,
                variable_manager=self.variable_manager,
                loader=self.loader,
                options=self.options,
                passwords=self.passwords,
                stdout_callback='default',
            )
            result = tqm.run(play)
        finally:
            # print result
            if tqm is not None:
                tqm.cleanup()
                os.remove(self.hostsFile.name)
                self.inventory.clear_pattern_cache()
            return result
Example #57
0
class MyInventory(Inventory):
    """ 
    this is my ansible inventory object. 
    """
    def __init__(self, resource, loader, variable_manager):
        """ 
        resource的数据格式是一个列表字典,比如 
            { 
                "group1": { 
                    "hosts": [{"hostname": "10.0.0.0", "port": "22", "username": "******", "password": "******"}, ...], 
                    "vars": {"var1": value1, "var2": value2, ...} 
                } 
            } 
 
                     如果你只传入1个列表,这默认该列表内的所有主机属于my_group组,比如 
            [{"hostname": "10.0.0.0", "port": "22", "username": "******", "password": "******"}, ...] 
        """
        self.resource = resource
        self.inventory = Inventory(loader=loader,
                                   variable_manager=variable_manager,
                                   host_list=[])
        self.dynamic_inventory()

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

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

        # add hosts to group
        for host in hosts:
            # 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)
            my_host.set_variable('ansible_ssh_host', hostip)
            my_host.set_variable('ansible_ssh_port', hostport)
            my_host.set_variable('ansible_ssh_user', username)
            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.iteritems():
                if key not in ["hostname", "port", "username", "password"]:
                    my_host.set_variable(key, value)
            # add to group
            my_group.add_host(my_host)

        self.inventory.add_group(my_group)

    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.iteritems():
                self.add_dynamic_group(hosts_and_vars.get("hosts"), groupname,
                                       hosts_and_vars.get("vars"))
Example #58
0
 def empty_inventory(self):
     return Inventory(None)
Example #59
0
 def large_range_inventory(self):
     return Inventory(self.large_range_inventory_file)
                   become_user=None,
                   check=True,
                   remote_user=None,
                   private_key_file=None,
                   ssh_common_args=None,
                   sftp_extra_args=None,
                   scp_extra_args=None,
                   ssh_extra_args=None,
                   verbosity=3))
passwords = dict(vault_pass='')

aws_region = 'us-west-2'

# create inventory and pass to var manager
inventory = Inventory(loader=loader,
                      variable_manager=variable_manager,
                      host_list='localhost')
variable_manager.set_inventory(inventory)


def run(play):
    tqm = None
    results = None
    try:
        tqm = TaskQueueManager(
            inventory=inventory,
            variable_manager=variable_manager,
            loader=loader,
            options=options,
            passwords=passwords,
            stdout_callback='default',