def _provision_node(self, node_name, ip_address, playbook_name):
        # playbook_path = '/etc/ansible/install_git.yml'
        user = None
        password = None
        playbook_path = playbook_name
        host_name = ip_address

        # if not os.path.exists(playbook_path):
        #     raise Exception('The playbook does not exist')

        variable_manager = VariableManager()
        loader = DataLoader()
        inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=[host_name])
        variable_manager.set_inventory(inventory)
        options = Options(connection='smart', become_user=user)
        passwords = {}

        host = inventory.get_host(host_name)
        variable_manager.set_host_variable(host, 'connection', 'ssh')
        variable_manager.set_host_variable(host, 'ansible_ssh_user', 'root')
        variable_manager.set_host_variable(host, 'ansible_ssh_pass', 'root')

        pbex = PlaybookExecutor(
            playbooks=[playbook_path],
            inventory=inventory,
            variable_manager=variable_manager,
            loader=loader,
            options=options,
            passwords=passwords)

        try:
            pbex.run()
        except Exception as ex:
            click.echo(ex.message)
Example #2
0
def execute_playbook(playbook, host, *args, **kwargs):
    playbook = os.path.join(playbook_store_path,playbook)
    Options = namedtuple('Options', ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'remote_user', 'listhosts', 'listtasks', 'listtags', 'syntax', 'verbose'])

    #needed objects
    variable_manager = VariableManager()
    loader = DataLoader()
    options = Options(connection='ssh', module_path='./modules', forks=100, become=None, become_method=None, become_user=None, check=False, remote_user='******', listhosts=None, listtasks=None, listtags=None, syntax=None, verbose=None)
    passwords = dict(vault_pass='******')


    #Create inventory and pass to manager
    inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=[host])
    variable_manager.set_inventory(inventory)
    hostobj = inventory.get_host(host)
    for varname, value in kwargs.iteritems():
        variable_manager.set_host_variable(hostobj, varname, value)


    #Should execute fine
    executor = PlaybookExecutor(playbooks=[ playbook ], inventory=inventory,
            variable_manager=variable_manager, loader=loader, options=options,
            passwords=passwords)

    #Forcing our callback object into the executor
    executor._tqm._stdout_callback = "json" 

    results = executor.run()
    pprint.pprint(results)
    report_tasks = executor._tqm._stdout_callback.results[0]['tasks']
    report = executor._tqm._stdout_callback.results
    report_stats = executor._tqm._stats.summarize(host)
    results_output = get_output(report_tasks, host)
    return dict(stats = report_stats, results = results_output)
Example #3
0
    def _run_play(self, play_source, host_vars):
        host_list = play_source['hosts']

        loader = dataloader.DataLoader()
        variable_manager = VariableManager()
        inventory_inst = inventory.Inventory(loader=loader,
                                             variable_manager=variable_manager,
                                             host_list=host_list)
        variable_manager.set_inventory(inventory_inst)

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

        storage = []
        callback = MyCallback(storage)

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

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

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

        return storage
Example #4
0
    def run(self):
        # insert node
        for ip in self.hosts:
            self._node_map[ip] = Service.new_node(self.task_id, ip)

        variable_manager = VariableManager()

        Logger.debug("start write ssh_key for task: {} global_id : {}".format(
            self.task_id, self.global_id))

        key_files = []

        group = Group(self.task_id)

        for h in self.hosts:

            # get ssh_key content
            key_content = _get_ssh_key(h)

            Logger.debug("read ssh_key for host: {} global_id: {}".format(
                h, self.global_id))

            # write ssh private key
            key_path = _write_ssh_key(h, key_content)

            #key_path="./tmp/97"
            Logger.debug("write ssh_key for host: {} global_id: {}".format(
                h, self.global_id))

            host_vars = dict(ansible_port=22,
                             ansible_user=self.user,
                             ansible_ssh_private_key_file="./" + key_path)

            Logger.debug("key_path: {} global_id: {}".format(
                key_path, self.global_id))

            key_files.append(key_path)

            host = Host(h)

            host.vars = host_vars

            group.add_host(host)

        # add params to each host
        if self.params is not None and isinstance(self.params, dict):
            for h in group.hosts:
                for key in self.params.keys():
                    variable_manager.set_host_variable(h, key,
                                                       self.params[key])

        Logger.debug("success write ssh_key for task: {} global_id: {}".format(
            self.task_id, self.global_id))

        # other options
        ssh_args = '-oControlMaster=auto -oControlPersist=60s -oStrictHostKeyChecking=no'
        options = _Options(connection='ssh',
                           module_path='./ansible/library',
                           forks=self.forks,
                           timeout=10,
                           remote_user=None,
                           private_key_file=None,
                           ssh_common_args=ssh_args,
                           ssh_extra_args=None,
                           sftp_extra_args=None,
                           scp_extra_args=None,
                           become=None,
                           become_method=None,
                           become_user=None,
                           verbosity=None,
                           check=False)

        if self.tasktype == "ansible_task":
            Logger.debug(
                "ansible tasks set*******************  global_id: {}".format(
                    self.global_id))
            play_source = dict(name=self.task_id,
                               hosts=self.task_id,
                               gather_facts='yes',
                               tasks=self.tasks)
        else:

            Logger.debug(
                "ansible role set******************* global_id: {}".format(
                    self.global_id))
            play_source = dict(name=self.task_id,
                               hosts=self.task_id,
                               gather_facts='yes',
                               roles=self.tasks)

        Logger.debug("start load play for task: {} global_id: {}".format(
            self.task_id, self.global_id))

        # make playbook
        playbook = Play().load(play_source,
                               variable_manager=variable_manager,
                               loader=_Loader)

        inventory = Inventory(loader=_Loader,
                              variable_manager=variable_manager)

        inventory.add_group(group)

        call_back = SyncCallbackModule(debug=True,
                                       step_callback=self._step_callback,
                                       global_id=self.global_id,
                                       source=self.source,
                                       tag_hosts=self.hosts)

        Logger.debug("success load play for task: {} global_id: {}".format(
            self.task_id, self.global_id))

        # task queue
        tqm = TaskQueueManager(inventory=inventory,
                               variable_manager=variable_manager,
                               loader=_Loader,
                               options=options,
                               passwords=None,
                               stdout_callback=call_back)

        try:
            back = tqm.run(playbook)

            Logger.info("back: {} global_id : {}".format(
                str(back), self.global_id))

            if back != 0:
                raise Exception("playbook run failed")

            return back
        finally:
            if tqm is not None:
                tqm.cleanup()
                _rm_tmp_key(key_files)
if variables['openslides_instance_projector_logo'] is None:
    print("NONE LOGO!!")
    variables['openslides_instance_projector_logo'] = ''

# check if instance if already created
instance_path = path.join(options.instances_dir, variables['openslides_instance_id'])

role = options.ansible_role
is_add = role == 'openslides-add-instance'
if path.exists(instance_path) and is_add and not options.force:
    raise Exception("instance already created")

variables['openslides_instance_path'] = instance_path

for key, value in variables.items():
    variable_manager.set_host_variable(Host(name='localhost'), key, value)

loader = DataLoader()
playoptions = Options(connection='local', module_path='/path/to/mymodules', forks=100, become=None,
                      become_method='sudo',
                      become_user=None, check=False)
passwords = dict(vault_pass='******')

# Instantiate our ResultCallback for handling results as they come in
results_callback = ResultCallback(path.join(options.instances_dir,
                                            'ansible.{}.log.json'.format(variables['openslides_instance_id'])))

# create inventory and pass to var manager
inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='localhost')
variable_manager.set_inventory(inventory)
Example #6
0
    def deploy(self):
        templatedb = {}
        if self._templatename:
            with open('/var/www/webvirtmgr/ansiblefunc/playbook/template',
                      'r') as template:
                lines = template.readlines()
                for line in lines:
                    templatedb[line.split(',')[0]] = line.split(',')[1]
                with open(
                        '/var/www/webvirtmgr/ansiblefunc/playbook/deploy_templ.yml',
                        'r') as source:
                    lines = source.readlines()
                    with open(
                            '/var/www/webvirtmgr/ansiblefunc/playbook/deploy.yml.do',
                            'w') as dest:
                        for line in lines:
                            dest.write(
                                line.replace('!host!', self._supervisorip))
        else:
            with open('/var/www/webvirtmgr/ansiblefunc/playbook/deploy.yml',
                      'r') as source:
                lines = source.readlines()
                with open(
                        '/var/www/webvirtmgr/ansiblefunc/playbook/deploy.yml.do',
                        'w') as dest:
                    for line in lines:
                        dest.write(line.replace('!host!', self._supervisorip))
        variable_manager = VariableManager()
        loader = DataLoader()
        supervisor = host.Host(name=self._supervisorip, port=None)
        variable_manager.set_host_variable(supervisor, 'vmname', self._vmname)
        variable_manager.set_host_variable(supervisor, 'rootpass',
                                           self._rootpass)
        variable_manager.set_host_variable(supervisor, 'memory', self._memory)
        variable_manager.set_host_variable(supervisor, 'cpu', self._cpu)
        variable_manager.set_host_variable(supervisor, 'rootimage',
                                           self._rootimage)
        variable_manager.set_host_variable(supervisor, 'network',
                                           self._network)
        variable_manager.set_host_variable(supervisor, 'inittype',
                                           self._inittype)
        variable_manager.set_host_variable(supervisor, 'images', self._images)
        if not self._templatename:
            variable_manager.set_host_variable(supervisor, 'installedpackage',
                                               self._installedpackage)
        else:
            variable_manager.set_host_variable(
                supervisor, 'template', templatedb[self._templatename].strip())
        inventory = Inventory(
            loader=loader,
            variable_manager=variable_manager,
            host_list='/var/www/webvirtmgr/ansiblefunc/playbook/hosts')

        if not os.path.exists(self._playbook):
            print 'playbook not found'
            sys.exit()
        options_tuple = 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'
        ])
        options = options_tuple(listtags=False,
                                listtasks=False,
                                listhosts=False,
                                syntax=False,
                                connection='ssh',
                                module_path=None,
                                forks=100,
                                remote_user='******',
                                private_key_file=self._keyfile,
                                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)
        variable_manager.extra_vars = {'hosts': self._supervisorip}
        pbex = PlaybookExecutor(playbooks=[self._playbook],
                                inventory=inventory,
                                variable_manager=variable_manager,
                                loader=loader,
                                options=options,
                                passwords={})
        results = pbex.run()
        print results
Example #7
0
    def _get_playbook_executor(self, variables, verbosity):
        Options = namedtuple('Options', [
            'connection', 'module_path', 'forks', 'become', 'become_method',
            'become_user', 'check', 'listhosts', 'listtasks', 'listtags',
            'syntax', 'diff'
        ])

        # -v given to us enables ansibles non-debug output.
        # So -vv should become ansibles -v.
        __main__.display.verbosity = max(0, verbosity - 1)

        # make sure ansible does not output warnings for our paternoster pseudo-play
        __main__._real_warning = __main__.display.warning

        def display_warning(msg, *args, **kwargs):
            if not msg.startswith('Could not match supplied host pattern'):
                __main__._real_warning(msg, *args, **kwargs)

        __main__.display.warning = display_warning

        loader = DataLoader()
        if ANSIBLE_VERSION < LooseVersion('2.4.0'):
            from ansible.inventory import Inventory
            variable_manager = VariableManager()
            inventory = Inventory(loader=loader,
                                  variable_manager=variable_manager,
                                  host_list='localhost,')
            variable_manager.set_inventory(inventory)
        else:
            from ansible.inventory.manager import InventoryManager
            inventory = InventoryManager(loader=loader, sources='localhost,')
            variable_manager = VariableManager(loader=loader,
                                               inventory=inventory)
        # force ansible to use the current python executable. Otherwise
        # it can end up choosing a python3 one (named python) or a different
        # python 2 version
        variable_manager.set_host_variable(inventory.localhost,
                                           'ansible_python_interpreter',
                                           sys.executable)

        for name, value in variables:
            variable_manager.set_host_variable(inventory.localhost, name,
                                               value)

        pexec = PlaybookExecutor(
            playbooks=[self._playbook],
            inventory=inventory,
            variable_manager=variable_manager,
            loader=loader,
            options=Options(
                connection='local',
                module_path=None,
                forks=1,
                listhosts=False,
                listtasks=False,
                listtags=False,
                syntax=False,
                become=None,
                become_method=None,
                become_user=None,
                check=False,
                diff=False,
            ),
            passwords={},
        )

        ansible.constants.RETRY_FILES_ENABLED = False

        if not verbosity:
            # ansible doesn't provide a proper API to overwrite this,
            # if you're using PlaybookExecutor instead of initializing
            # the TaskQueueManager (_tqm) yourself, like in the offical
            # example.
            pexec._tqm._stdout_callback = MinimalAnsibleCallback()

        return pexec
Example #8
0
class Runner(object):
    """Ansible Playbook runner."""
    def __init__(self,
                 username,
                 playbook,
                 private_key_file,
                 inventory_data,
                 extra_vars,
                 become_pass,
                 verbosity=0,
                 search_filter=None):
        """
    Args:
      username: string, username of user running the playbook
      playbook: string, full playbook path eg. /tmp/my_pb.yml
      private_key_file: string, private key file
      inventory_data: dict, inventory data
      extra_vars: dict, Ansible extra vars, key = variable name
      become_pass: string, become password
      verbosity: integer, verbosity level
      search_filter: string, hosts/groups to match
    """

        self.playbook = playbook
        self.username = username
        self.inventory_data = inventory_data
        self.extra_vars = extra_vars
        self.search_filter = search_filter

        self.options = Options()
        self.options.private_key_file = private_key_file
        self.options.verbosity = verbosity
        self.options.connection = 'ssh'  # Need a connection type "smart" or "ssh"
        self.options.become = True
        self.options.become_method = 'sudo'
        self.options.become_user = '******'

        # Set global verbosity
        self.display = Display()
        self.display.verbosity = self.options.verbosity
        # Executor appears to have it's own verbosity object/setting as well
        playbook_executor.verbosity = self.options.verbosity

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

        # Gets data from YAML/JSON files
        self.loader = DataLoader()

        # ORIGNAL on line 1
        #self.loader.set_vault_password(os.environ['VAULT_PASS'])
        self.loader.set_vault_password('secret')

        # All the variables from all the various places
        self.variable_manager = VariableManager()

        # Set of hosts
        hosts = set()

        # Load group variable
        for group in self.inventory_data:
            if group != '_meta':
                for host in self.inventory_data[group]['hosts']:
                    host_obj = Host(host)
                    hosts.add(host)
                    for var in self.inventory_data[group]['vars']:
                        self.variable_manager.set_host_variable(
                            host_obj, var,
                            self.inventory_data[group]['vars'][var])

        # Load host variables
        for host in self.inventory_data['_meta']['hostvars']:
            for var in self.inventory_data['_meta']['hostvars'][host]:
                host_obj = Host(host)
                self.variable_manager.set_host_variable(
                    host_obj, var,
                    self.inventory_data['_meta']['hostvars'][host][var])

        self.variable_manager.extra_vars = self.extra_vars

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

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

    def run(self):
        """Run playbook."""

        # Results of PlaybookExecutor
        stats = None
        play_recap = {}

        try:
            self.pbex.run()
            stats = self.pbex._tqm._stats
        except AnsibleError as error:
            print 'Error', error
            self.pbex._tqm.send_callback('record_logs',
                                         username=self.username,
                                         success=False,
                                         extra_vars=self.extra_vars,
                                         playbook=self.playbook,
                                         search_filter=self.search_filter)
            raise AnsibleError
        finally:
            if stats:
                self.pbex._tqm.cleanup()

                # Test if success for record_logs
                run_success = True
                hosts = sorted(stats.processed.keys())
                for host in hosts:
                    play_recap = stats.summarize(host)
                    if play_recap['unreachable'] > 0 or play_recap[
                            'failures'] > 0:
                        run_success = False

        # Dirty hack to send callback to save logs with data we want
        # Note that function "record_logs" is one I created and put into
        # the playbook callback file
        self.pbex._tqm.send_callback('record_logs',
                                     username=self.username,
                                     success=run_success,
                                     extra_vars=self.extra_vars,
                                     playbook=self.playbook,
                                     search_filter=self.search_filter)

        return play_recap