Exemplo n.º 1
0
def pbexe(sid,war,mode):
  variable_manager = VariableManager()
  loader = DataLoader()

#  inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=[])
  inventory = Inventory(loader=loader, variable_manager=variable_manager)
  playbook_path = '/etc/ansible/yml/'+sid+'.yml'

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

  Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection','module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check'])
  options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=100, remote_user='******', private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=True, become_method=None, become_user='******', verbosity=None, check=False)

  variable_manager.extra_vars = {'war': war,'mode': mode} # This can accomodate various other command line arguments.`

  passwords = {}

  #cb = ResultCallback()
#  cb = default.CallbackModule()
  pbex = PlaybookExecutor(playbooks=[playbook_path],  inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords)
#  pbex._tqm._stdout_callback = cb
  op=sys.stdout

  filename='/var/log/ansible/'+time.strftime('%Y%m%d%H%M%S')+'.log'
  opf=open(filename,'w')
  sys.stdout=opf
  results = pbex.run()
  sys.stdout=op
  opf.close
  print open(filename,'r').read()
  return filename
Exemplo n.º 2
0
class PlayBook(object):
    def __init__(self, playbook, inventory="inventory.py", extra_vars=None):
        basedir = os.path.abspath(os.path.dirname(__file__))
        self.playbook = os.path.join(basedir, playbook)
        self.options = Options(
            inventory=inventory,
            sudo=False, su=False, sudo_user=None, su_user=None,
            listtags=False, listtasks=False, listhosts=False,
            syntax=False, check=False,
            ask_pass=False,ask_su_pass=False,ask_sudo_pass=False,ask_vault_pass=False,
            connection='smart', timeout=10, remote_user=None, private_key_file=None,
            ssh_common_args=None, ssh_extra_args=None,
            sftp_extra_args=None, scp_extra_args=None,
            module_path=None, forks=5,
            become=False, become_method='sudo', become_user=None,
            verbosity=0)
        self.loader = DataLoader()
        self.variable_manager = VariableManager()
        self.variable_manager.extra_vars = extra_vars
        self.inventory = Inventory(loader=self.loader,
                                   variable_manager=self.variable_manager,
                                   host_list=inventory)
        self.pbex = PlaybookExecutor(playbooks=[self.playbook],
                                     inventory=self.inventory,
                                     loader=self.loader,
                                     variable_manager=self.variable_manager,
                                     options=self.options,
                                     passwords={'become_pass': None})

    def run(self):
        self.pbex.run()
        stats = self.pbex._tqm._stats
        self.pbex._tqm.send_callback('human_log')
        return stats
Exemplo n.º 3
0
    def run_playbook(self, hosts, playbook_path):
        """ 
        run ansible palybook 
        """
        global tasklist
        tasklist = {}
        for host in hosts:
            tasklist[host] = {}
            tasklist[host]['ok'] = []
            tasklist[host]['failed'] = []
            tasklist[host]['skppied'] = []

        self.results_callback = PlayBookResultCallback(tasklist)
        try:
            executor = PlaybookExecutor(
                playbooks=[playbook_path],
                inventory=self.inventory,
                variable_manager=self.variable_manager,
                loader=self.loader,
                options=self.options,
                passwords=self.passwords)
            executor._tqm._stdout_callback = self.results_callback
            executor.run()
        except Exception as e:
            print(e)
            return False
Exemplo n.º 4
0
    def __init__(self, **kwargs):
        super(Ansible_playbook, self).__init__(**kwargs)

        self.task_file = kwargs.get('task_file', None)
        self.sudo = kwargs.get('sudo', False)
        self.sudo_user = kwargs.get('sudo_user', False)
        self.sudo_password = kwargs.get('sudo_password', False)

        # check if parameters have been provided
        if self._is_parameters_ok():

            variable_manager = VariableManager()
            loader = DataLoader()
            options = self._get_options()
            passwords = {'become_pass': self.sudo_password}

            inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list="localhost")
            variable_manager.set_inventory(inventory)
            playbooks = [self.task_file]

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

            executor.run()
Exemplo n.º 5
0
    def run(self):

        Options = namedtuple('Options',
                             ['listtags', 'listtasks', 'listhosts',
                              'syntax', 'connection', 'module_path',
                              'forks', 'remote_user', 'private_key_file',
                              'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args',
                              'scp_extra_args', 'become', 'become_method',
                              'become_user', 'verbosity', 'check', 'diff'])
        options = Options(listtags=False, listtasks=False, listhosts=False,
                          syntax=False, connection='ssh', module_path=None,
                          forks=100, remote_user='******', private_key_file=None,
                          ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None,
                          scp_extra_args=None, become=True, become_method=None,
                          become_user='******', verbosity=None, check=False, diff=False)
        #OPTION_FLAGS = ('connection', 'remote_user', 'private_key_file', 'verbosity', 'force_handlers', 'step', 'start_at_task', 'diff',
        #        'ssh_common_args', 'docker_extra_args', 'sftp_extra_args', 'scp_extra_args', 'ssh_extra_args')
        passwords = {}

        pbex = PlaybookExecutor(playbooks=[self.playbook],
                                inventory=self.inventory,
                                variable_manager=self.variable_manager,
                                loader=self.loader,
                                options=options,
                                passwords=passwords)
        result = pbex.run()
        return result
Exemplo n.º 6
0
def call_ansible(yaml_file, become=False, tag=None):
  """Call Ansible with a playbook."""

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

  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',
                        'tags'])

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

  pbex = PlaybookExecutor(playbooks=[yaml_file],
                          inventory=inventory,
                          variable_manager=variable_manager,
                          loader=loader,
                          options=options,
                          passwords={})

  logger.debug("Calling Ansible with yaml file: {}".format(yaml_file))
  result = pbex.run()
  if result:
    logger.error("An error occured whilst executing the Ansible Playbook.")
Exemplo n.º 7
0
def run_playbook(playbook_path, cluster):
    playbook_file = '{playbook_path}/provision/site.yml'.format(
        playbook_path=playbook_path,
    )
    loader = DataLoader()
    variable_manager = VariableManager()
    inventory = Inventory(
        loader=loader,
        variable_manager=variable_manager,
        host_list=get_host_list(playbook_path, cluster),
    )
    file_handle, private_key_file = mkstemp(dir=playbook_path)
    with open(private_key_file, 'w') as key_file:
        key_file.write(cluster.sshKey)
    options = Options(
        inventory=inventory,
        remote_user=cluster.username,
        private_key_file=private_key_file,
    )
    executor = PlaybookExecutor(
        playbooks=[playbook_file],
        inventory=inventory,
        variable_manager=variable_manager,
        loader=loader,
        options=options,
        passwords={
            'become_pass': '******',
        },
    )
    return executor.run()
Exemplo n.º 8
0
    def run(self, playbooks, limit=''):
        """
        param: `playbooks`: type string or list, example: ['/etc/ansible/test.yml'], '/etc/ansible/test1.yml, /etc/ansible/test2.yml'
        param: `limit`:  eq --limit ''
        """
        self._prepare_run()

        results = 0

        if isinstance(playbooks, basestring):
            if ',' in playbooks:
                playbooks = [pl.strip() for pl in playbooks.split(',') if os.path.exists(pl)]
            else:
                playbooks = [playbooks.strip()]

        # --limit
        if limit:
            self.inventory.subset(limit)

        try:
            # create the playbook executor, which manages running the plays via a task queue manager
            pbex = PlaybookExecutor(playbooks=playbooks, inventory=self.inventory,
                                    variable_manager=self.variable_manager,
                                    loader=self.loader, options=self.options, passwords=self.passwords)

            results = pbex.run()
        except Exception, ex:
            results = str(ex)
Exemplo n.º 9
0
	def run(self,mission_id,role_name,exe_group):			
                '''
		 create play with tasks actually run it
		'''
		tqm = None
		try:
                        retry_path = '/etc/ansible/main.yml'
			inventory_path = [retry_path]
			self.results_callback = ResultCallback()
                        extra_vars = {}
			extra_vars['host_list'] = exe_group
			extra_vars['role_name'] = role_name
			extra_vars['run_id'] = mission_id
			self.variable_manager.extra_vars = extra_vars
    			pbex = PlaybookExecutor(
              			playbooks=inventory_path, 
              			inventory=self.inventory, 
              			variable_manager=self.variable_manager, 
              			loader=self.loader, 
              			options=self.options, 
              			passwords=self.passwords,
          		)

    			pbex._tqm._stdout_callback = self.results_callback
    			result = pbex.run()
		finally:
    			if tqm is not None:
        			tqm.cleanup()
Exemplo n.º 10
0
def run_playbook():
    playbooks = ['/Users/CYu/Code/Python/python-demo/demo_ansible/playbook.yml']  # 这里是一个列表, 因此可以运行多个playbook
    variable_manager.extra_vars = {"ansible_ssh_user": "******", "ansible_ssh_pass": "******"}  # 增加外部变量
    pb = PlaybookExecutor(playbooks=playbooks, inventory=inventory, variable_manager=variable_manager, loader=loader,
                          options=options, passwords=None)
    result = pb.run()
    print result
Exemplo n.º 11
0
  def runPlaybook(self):
    sys.stdout = open('Output-pythonAnsible','w')
    variable_manager = VariableManager()
    loader = DataLoader()

    inventory = Inventory(loader=loader, variable_manager=variable_manager,  host_list=self.host_list)
    #self.playbook_path
    #playbook_path = '/home/davis/Documents/Network-automation/cisco_xe.yml'

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

    Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection','module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check'])
    options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=100, remote_user='******', private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=True, become_method=None, become_user='******', verbosity=None, check=False)

    #variable_manager.extra_vars = {'hosts': 'mywebserver'} # This can accomodate various other command line arguments.`

    passwords = {}

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

    results = pbex.run()

    print "Output ::-",results
    return results
Exemplo n.º 12
0
 def playbook_api(self,yml_fp):
     loader = DataLoader()
     variable_manager = VariableManager()
     
     inventory = Inventory(
         loader=loader,
         variable_manager=variable_manager,
         host_list=self.ansible_host_list
     )
     
     variable_manager.set_inventory(inventory)
     
     playbooks = ["%s" % yml_fp]
     
     pbex = PlaybookExecutor(
                   playbooks=playbooks,
                   inventory=inventory,
                   variable_manager=variable_manager,
                   loader=loader,
                   options=self.options,
                   passwords=self.passwords
             )
                   
     callback = AnsiCallBack()
     pbex._tqm._stdout_callback = callback
     pbex.run()
     
     return self.evaluate_results(callback)
Exemplo n.º 13
0
def pbexe(userid,serial,host,module,apppath,yaml,url=None):
    variable_manager = VariableManager()
    loader = DataLoader()
    hostfile = '/ansible/hosts'
    inventory = Inventory(loader=loader, variable_manager=variable_manager,host_list='/ansible/hosts')
    playbook_path = yaml

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

    Options = namedtuple('Options', ['listtags', 'listtasks', 'listhosts', 'syntax', 'connection','module_path', 'forks', 'remote_user', 'private_key_file', 'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args', 'scp_extra_args', 'become', 'become_method', 'become_user', 'verbosity', 'check'])
    options = Options(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='ssh', module_path=None, forks=100, remote_user='******', private_key_file=None, ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None, become=True, become_method=None, become_user='******', verbosity=None, check=False)

    variable_manager.extra_vars = {
            'host': host,
            'module': module,
            'tomcat_root': apppath,
            'url': url
} # This can accomodate various other command line arguments.`
    passwords = {}
    cb = CallbackModule(serial)
#  cb = default.CallbackModule()
    pbex = PlaybookExecutor( playbooks=[playbook_path],  inventory=inventory, variable_manager=variable_manager, loader=loader, options=options, passwords=passwords)
    pbex._tqm._stdout_callback = cb
    results = pbex.run()
    return results
Exemplo n.º 14
0
    def run_playbook(self, host_list, role_name, role_uuid, temp_param):
        """
        run ansible palybook
        """
        try:
            self.callback = ResultsCollector()
            filenames = [BASE_DIR + '/handlers/ansible/v1_0/sudoers.yml']
            logger.info('ymal file path:%s'% filenames)
            template_file = TEMPLATE_DIR
            if not os.path.exists(template_file):
                logger.error('%s 路径不存在 '%template_file)
                sys.exit()

            extra_vars = {}
            host_list_str = ','.join([item for item in host_list])
            extra_vars['host_list'] = host_list_str
            extra_vars['username'] = role_name
            extra_vars['template_dir'] = template_file
            extra_vars['command_list'] = temp_param.get('cmdList')
            extra_vars['role_uuid'] = 'role-%s'%role_uuid
            self.variable_manager.extra_vars = extra_vars
            logger.info('playbook 额外参数:%s'%self.variable_manager.extra_vars)
            # actually run it
            executor = PlaybookExecutor(
                playbooks=filenames, inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader,
                options=self.options, passwords=self.passwords,
            )
            executor._tqm._stdout_callback = self.callback
            executor.run()
        except Exception as e:
            logger.error("run_playbook:%s"%e)
Exemplo n.º 15
0
 def test_init(self):
     """
     Initialises and runs sample echo playbook for testing
     """
     path = os.path.realpath(__file__).split("/")[0:-1]
     path = "/".join(path)
     playbook_path = path+'/playbooks/test_playbook.yml'
     options = self.Options(listtags=False,
                       listtasks=False,
                       listhosts=False,
                       syntax=False,
                       connection='ssh',
                       module_path=None,
                       forks=100,
                       remote_user='******',
                       private_key_file=None,
                       ssh_common_args=None,
                       ssh_extra_args=None,
                       sftp_extra_args=None,
                       scp_extra_args=None,
                       become=True,
                       become_method=None,
                       become_user='******',
                       verbosity=3,
                       check=False)
     self.variable_manager.extra_vars = {'hosts': 'mywebserver'}
     passwords = {}
     pbex = PlaybookExecutor(playbooks=[playbook_path],
                             inventory=self.inventory,
                             variable_manager=self.variable_manager,
                             loader=self.loader,
                             options=options,
                             passwords=passwords)
     results = pbex.run()
     assert_equal(results, 0)
Exemplo n.º 16
0
def ansible_task(playbooks):
    Options = namedtuple('Options', \
    ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'listhosts', 'listtasks', 'listtags', 'syntax'])

    options = Options(
        connection = 'ssh', 
        module_path = '/opt/ansible/modules',
        forks = 100, 
        become = True, 
        become_method = 'sudo', 
        become_user = '******', 
        check = False, 
        listhosts = None,
        listtasks = None,
        listtags = None,
        syntax = None
    )

    loader = DataLoader()
    variable_manager = VariableManager()
    inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list='/opt/ansible/inventory')
    variable_manager.set_inventory(inventory)

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

    stats = pb._tqm._stats
    ips = stats.processed.keys()
    return [ {ip : stats.summarize(ip)} for ip in ips ]
Exemplo n.º 17
0
 def run(self, log):
     if not self.playbook:
         code = 999
         simple = 'playbook must exist'
         return code, simple, None
     if log:
         log_file.append(log)
     if not os.path.exists(self.playbook):
         code = 1000
         results = 'not exists playbook: ' + self.playbook
         return code, results, None
     pbex = PlaybookExecutor(playbooks=[self.playbook],
                             inventory=self.inventory,
                             variable_manager=self.variable_manager,
                             loader=self.loader,
                             options=self.options,
                             passwords=self.passwords)
     try:
         code = pbex.run()
     except AnsibleParserError:
         code = 1001
         results = 'syntax problems in ' + self.playbook
         return  code, results, None
     stats = pbex._tqm._stats
     hosts = sorted(stats.processed.keys())
     results = [{h: stats.summarize(h)} for h in hosts]
     if not results:
         code = 1002
         results = 'no host executed in ' + self.playbook
         return  code, results, None
     complex = '\n'.join(log_add)
     return code, results, complex
Exemplo n.º 18
0
    def run(self, playbook,
            hosts,
            extra_vars={},
            log='',
            with_output=False,
            use_root=False):
        self.result['playbook'] = playbook
        if not os.path.exists(playbook):
            result = {
                'errno': -3,
                'msg': 'not exists playbook: ' + playbook
            }
        else:
            AT = Ansi_Template()
            hosts, host_file = AT.make_host_template(hosts, use_root)
            extra_vars['ansible_hosts'] = ':'.join(hosts)
            inventory = InventoryManager(loader=self.loader, sources=[host_file])
            variable_manager = VariableManager(loader=self.loader, inventory=inventory)
            variable_manager.extra_vars = extra_vars
            pbex = PlaybookExecutor(playbooks=[playbook],
                                    inventory=inventory,
                                    variable_manager=variable_manager,
                                    loader=self.loader,
                                    options=self.options,
                                    passwords={})

            new_display = New_Display(log_file=log, debug=self.debug)
            results_callback = ResultCallback(new_display=new_display)

            pbex._tqm._stdout_callback = results_callback
            try:
                errno = pbex.run()
                result = results_callback.tasks
                result['errno'] = errno
            except AnsibleParserError as e:
                msg = 'syntax problems: {0}'.format(str(e))
                result = {
                    'errno': -2,
                    'msg': msg
                }
                self.write_log(log, msg)
                print('syntax problems: {0}'.format(str(e)))
            if with_output:
                result['output'] = '\n'.join(new_display.log_add)
            if result['errno'] != -2 and not result['summary']:
                msg = 'no host executed'
                result = {
                    'errno': -1,
                    'msg': msg
                }
                self.write_log(log, msg)
            os.unlink(host_file)
        self.result.update(result)
        if self.result['errno'] != 0:
            self.result['msg'] = '无法配置完成,请联系管理员!'
        return self.result
Exemplo n.º 19
0
 def run_need_data(self):
     if not self.playbook:
         code = 999
         simple = 'playbook must exist'
         return code, simple, None
     if not os.path.exists(self.playbook):
         code = 1000
         complex = {'playbook': self.playbook, 
        'msg': self.playbook + ' playbook does not exist', 'flag': False}
         simple = 'playbook does not exist about ' + self.playbook
         return code, simple, complex
     pbex = PlaybookExecutor(playbooks=[self.playbook], 
                             inventory=self.inventory, 
                             variable_manager=self.variable_manager, 
                             loader=self.loader, 
                             options=self.options, 
                             passwords=self.passwords)
     results_callback = ResultCallback()
     pbex._tqm._stdout_callback = results_callback
     try:
         code = pbex.run()
     except AnsibleParserError:
         code = 1001
         simple = {'playbook': self.playbook,
                   'msg': 'syntax problems in ' + self.playbook, 'flag': False,
                   'msg_list': {'status': 'unknown', 'msg': 'syntax problems'}}
         complex = 'syntax problems in ' + self.playbook
         return code, simple, complex
     if results_callback.no_hosts:
         code = 1002
         complex = 'no hosts matched in ' + self.playbook
         simple = {'executed': False, 'flag': False, 'playbook': self.playbook,
                   'msg': 'no_hosts', 'msg_list': {'status': 'unknown', 'msg': 'no_hosts'}}
         return code, simple, complex
     else:
         msg_list = results_callback.msg_list
         ok = results_callback.ok
         fail = results_callback.fail
         unreachable = results_callback.unreachable
         ok_all = list(ok.keys())
         fail_all = list(fail.keys())
         unreachable_hosts = list(unreachable.keys())
         fail_hosts = list(set(fail_all) - set(unreachable_hosts))
         ok_hosts = list(set(ok_all) - set(unreachable_hosts) - set(fail_hosts))
         if code != 0:
             complex = {'playbook': results_callback.playbook, 'ok': ok,
              'fail': fail, 'unreachable': unreachable, 'flag': False}
             simple = {'executed': True, 'flag': False, 'playbook': self.playbook, 'msg_list': msg_list,
                      'msg': {'playbook': self.playbook, 'ok_hosts': ok_hosts, 'fail': fail_hosts, 'unreachable': unreachable_hosts}}
             return code, simple, complex
         else:
             complex = {'playbook': results_callback.playbook, 'ok': ok,
              'fail': fail, 'unreachable': unreachable, 'flag': True}
             simple = {'executed': True, 'flag': True, 'playbook': self.playbook, 'msg_list': msg_list,
                      'msg': {'playbook': self.playbook, 'ok_hosts': ok_hosts, 'fail': fail_hosts, 'unreachable': unreachable_hosts}}
             return code, simple, complex
    def test_playbook_executor__get_serialized_batches(self):
        fake_loader = DictDataLoader({
            'no_serial.yml': '''
            - hosts: all
              gather_facts: no
              tasks:
              - debug: var=inventory_hostname
            ''',
            'serial_int.yml': '''
            - hosts: all
              gather_facts: no
              serial: 2
              tasks:
              - debug: var=inventory_hostname
            ''',
            'serial_pct.yml': '''
            - hosts: all
              gather_facts: no
              serial: 20%
              tasks:
              - debug: var=inventory_hostname
            ''',
        })

        mock_inventory = MagicMock()
        mock_var_manager = MagicMock()

        # fake out options to use the syntax CLI switch, which will ensure
        # the PlaybookExecutor doesn't create a TaskQueueManager
        mock_options = MagicMock()
        mock_options.syntax.value = True

        pbe = PlaybookExecutor(
            playbooks=['no_serial.yml', 'serial_int.yml', 'serial_pct.yml'],
            inventory=mock_inventory,
            variable_manager=mock_var_manager,
            loader=fake_loader,
            options=mock_options,
            passwords=[],
        )

        playbook = Playbook.load(pbe._playbooks[0], variable_manager=mock_var_manager, loader=fake_loader)
        play = playbook.get_plays()[0]
        mock_inventory.get_hosts.return_value = ['host0','host1','host2','host3','host4','host5','host6','host7','host8','host9']
        self.assertEqual(pbe._get_serialized_batches(play), [['host0','host1','host2','host3','host4','host5','host6','host7','host8','host9']])

        playbook = Playbook.load(pbe._playbooks[1], variable_manager=mock_var_manager, loader=fake_loader)
        play = playbook.get_plays()[0]
        mock_inventory.get_hosts.return_value = ['host0','host1','host2','host3','host4','host5','host6','host7','host8','host9']
        self.assertEqual(pbe._get_serialized_batches(play), [['host0','host1'],['host2','host3'],['host4','host5'],['host6','host7'],['host8','host9']])

        playbook = Playbook.load(pbe._playbooks[2], variable_manager=mock_var_manager, loader=fake_loader)
        play = playbook.get_plays()[0]
        mock_inventory.get_hosts.return_value = ['host0','host1','host2','host3','host4','host5','host6','host7','host8','host9']
        self.assertEqual(pbe._get_serialized_batches(play), [['host0','host1'],['host2','host3'],['host4','host5'],['host6','host7'],['host8','host9']])
Exemplo n.º 21
0
def main(argv=sys.argv[1:]):
    #
    # initialize needed objects
    #
    variable_manager = VariableManager()
    loader = DataLoader()
    # https://pymotw.com/2/collections/namedtuple.html
    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'])

    options = Options(listtags=False, listtasks=False, listhosts=False,
                      syntax=False, connection='local', module_path=None,
                      forks=100, remote_user='******',
                      private_key_file=None, ssh_common_args=None,
                      ssh_extra_args=None, sftp_extra_args=None,
                      scp_extra_args=None, become=True, become_method=None,
                      become_user='******', verbosity=None, check=False)

    passwords = {}

    inventory = Inventory(loader=loader, variable_manager=variable_manager,
                          host_list='./ls_hosts')

    variable_manager.set_inventory(inventory)

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

    # This can accomodate various other command line arguments.`
    # variable_manager.extra_vars = {'hosts': 'mywebserver'}

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

    # cb = ResultAccumulator()
    # pbex._tqm._stdout_callback = cb

    results = pbex.run()
    if results != 0:
        print "ERROR"
Exemplo n.º 22
0
 def _run_playbook(self, playbook):
     self.options = options = self.Options(**self.options_args)
     vm = self.variable_manager
     vm.extra_vars = load_extra_vars(loader=self.loader, options=options)
     pbe = PlaybookExecutor(
         playbooks=[playbook],
         inventory=self.inventory,
         variable_manager=vm,
         loader=self.loader,
         options=options,
         passwords=dict(),
     )
     pbe.run()
     return vm.get_vars(self.loader)
Exemplo n.º 23
0
    def run(self):
        executor = PlaybookExecutor(
            playbooks=[self.playbook_path],
            inventory=self.inventory,
            variable_manager=self.variable_manager,
            loader=self.loader,
            passwords={"conn_pass": self.passwords}
        )
        context.CLIARGS = ImmutableDict(self.options)

        if executor._tqm:
            executor._tqm._stdout_callback = self.results_callback
        executor.run()
        executor._tqm.cleanup()
        return self.results_callback.output
Exemplo n.º 24
0
    def run(self):
        executor = PlaybookExecutor(
            playbooks=[self.playbook_path],
            inventory=self.inventory,
            variable_manager=self.variable_manager,
            loader=self.loader,
            options=self.options,
            passwords=self.passwords
        )

        if executor._tqm:
            executor._tqm._stdout_callback = self.results_callback
        executor.run()
        executor._tqm.cleanup()
        return self.results_callback.output
Exemplo n.º 25
0
 def run_playbook(self, playbook):
     options = self.Options(**self.options_args)
     vm = self.variable_manager
     vm.extra_vars = load_extra_vars(loader=self.loader, options=options)
     pbe = PlaybookExecutor(
         playbooks=[playbook],
         inventory=self.inventory,
         variable_manager=vm,
         loader=self.loader,
         options=options,
         passwords=dict(conn_pass='******'),
     )
     res = pbe.run()
     self._run_vars = vm.get_vars(self.loader)
     return res
Exemplo n.º 26
0
    def run_playbook(self, filenames, fork=5):
        '''
             :param filenames is list ,
             :param fork is interge, default 5
        '''
        display = LogDisplay(logname=self.job_id)
        callback = CALLBACKMODULE[CALLBACK](display=display)
        # actually run it
        executor = PlaybookExecutor(
            playbooks=filenames, inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader,
            options=self.options, passwords=self.passwords,
        )
        executor._tqm._stdout_callback = callback
        executor.run()

        return display.get_log_json()
Exemplo n.º 27
0
class PlayBook(object):
    def __init__(self, inventory='/etc/ansible/hosts', extra_vars=None, private_key_file=None):
        """
        :param playbook: playbook.yml
        :param inventory: inventory file or script
        :type param extra_vars: dict
        :param private_key_file: ssh private key
        """
        self.pbex = None
        self.options = Options(private_key_file=private_key_file, connection='smart', forks=10, timeout=10,
                               verbosity=0, check=False,
                               listtasks=False, listhosts=False, syntax=False,
                               subset=None, module_path=None, become=None, become_user=None, become_method='sudo')

        # initialize needed objects
        self.loader = DataLoader()
        self.variable_manager = VariableManager()
        self.variable_manager.extra_vars = extra_vars
        self.variable_manager.options_vars = {'ansible_check_mode': self.options.check}
        self.inventory = Inventory(loader=self.loader, variable_manager=self.variable_manager, host_list=inventory)
        self.variable_manager.set_inventory(self.inventory)
        # Limits inventory results to a subset of inventory that matches a given pattern
        self.inventory._subset = self.options.subset

    def run_playbook(self, playbook):
        self.pbex = PlaybookExecutor(playbooks=[playbook], inventory=self.inventory,
                                     variable_manager=self.variable_manager,
                                     loader=self.loader, options=self.options,
                                     passwords={'conn_pass': None, 'become_pass': None})
        self.pbex._tqm._stdout_callback = ResultCallback()
        return self.pbex.run()

    def run_play(self, play):
        pass
Exemplo n.º 28
0
 def run_playbook(self, playbook):
     self.pbex = PlaybookExecutor(playbooks=[playbook], inventory=self.inventory,
                                  variable_manager=self.variable_manager,
                                  loader=self.loader, options=self.options,
                                  passwords={'conn_pass': None, 'become_pass': None})
     self.pbex._tqm._stdout_callback = ResultCallback()
     return self.pbex.run()
Exemplo n.º 29
0
 def run_playbook(self, host_list, playbook_path,extra_vars=None): 
     """ 
     run ansible palybook 
     """         
     try: 
         if self.redisKey:self.callback = PlayBookResultsCollectorToSave(self.redisKey,self.logId)  
         else:self.callback = PlayBookResultsCollector()  
         if extra_vars:self.variable_manager.extra_vars = extra_vars 
         executor = PlaybookExecutor(  
             playbooks=[playbook_path], inventory=self.inventory, variable_manager=self.variable_manager, loader=self.loader,  
             options=self.options, passwords=self.passwords,  
         )  
         executor._tqm._stdout_callback = self.callback  
         executor.run()  
     except Exception as e: 
         return False
Exemplo n.º 30
0
 def run_need_data(self):
     if not os.path.exists(self.playbook):
         code = 1000
         complex = {'playbook': self.playbook, 
        'msg': self.playbook + ' playbook does not exist', 'flag': False}
         simple = 'playbook does not exist about ' + self.playbook
         return code, simple, complex
     pbex = PlaybookExecutor(playbooks=[self.playbook], 
                             inventory=self.inventory, 
                             variable_manager=self.variable_manager, 
                             loader=self.loader, 
                             options=self.options, 
                             passwords=self.passwords)
     results_callback = ResultCallback()
     pbex._tqm._stdout_callback = results_callback
     try:
         code = pbex.run()
     except AnsibleParserError:
         code = 1001
         complex = {'playbook': self.playbook, 
                   'msg': 'syntax problems in ' + self.playbook, 'flag': False}
         simple = 'syntax problems in ' + self.playbook
         return code, simple, complex
     if results_callback.no_hosts:
         code = 1002
         complex = 'no hosts matched in ' + self.playbook
         simple = {'executed': False, 'flag': False, 'playbook': self.playbook,
                   'msg': 'no_hosts'}
         return code, simple, complex
     else:
         ok = json.loads(results_callback.ok)
         fail = json.loads(results_callback.fail)
         unreachable = json.loads(results_callback.unreachable)
         if code != 0:
             complex = {'playbook': results_callback.playbook, 'ok': ok,
              'fail': fail, 'unreachable': unreachable, 'flag': False}
             simple = {'executed': True, 'flag': False, 'playbook': self.playbook,
                      'msg': {'playbook': self.playbook, 'ok_hosts': ok.keys(), 'fail': fail.keys(), 'unreachable': unreachable.keys()}}
             return code, simple, complex
         else:
             complex = {'playbook': results_callback.playbook, 'ok': ok,
              'fail': fail, 'unreachable': unreachable, 'flag': True}
             simple = {'executed': True, 'flag': True, 'playbook': self.playbook,
                       'msg': {'playbook': self.playbook, 'ok_hosts': ok.keys(), 'fail': fail.keys(), 'unreachable': unreachable.keys()}}
             return code, simple, complex
Exemplo n.º 31
0
    def __init__(self,
                 hosts=None,
                 playbook_path=None,
                 forks=C.DEFAULT_FORKS,
                 listtags=False,
                 listtasks=False,
                 listhosts=False,
                 syntax=False,
                 module_path=None,
                 remote_user='******',
                 timeout=C.DEFAULT_TIMEOUT,
                 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,
                 extra_vars=None,
                 connection_type="ssh",
                 passwords=None,
                 private_key_file=None,
                 check=False):

        C.RETRY_FILES_ENABLED = False
        self.callbackmodule = PlaybookResultCallBack()
        if playbook_path is None or not os.path.exists(playbook_path):
            raise AnsibleError("Not Found the playbook file: %s." %
                               playbook_path)
        self.playbook_path = playbook_path
        self.loader = DataLoader()
        self.variable_manager = VariableManager()
        self.passwords = passwords or {}
        self.inventory = JMSInventory(hosts)

        self.options = self.Options(listtags=listtags,
                                    listtasks=listtasks,
                                    listhosts=listhosts,
                                    syntax=syntax,
                                    timeout=timeout,
                                    connection=connection_type,
                                    module_path=module_path,
                                    forks=forks,
                                    remote_user=remote_user,
                                    private_key_file=private_key_file,
                                    ssh_common_args=ssh_common_args or "",
                                    ssh_extra_args=ssh_extra_args or "",
                                    sftp_extra_args=sftp_extra_args,
                                    scp_extra_args=scp_extra_args,
                                    become=become,
                                    become_method=become_method,
                                    become_user=become_user,
                                    verbosity=verbosity,
                                    extra_vars=extra_vars or [],
                                    check=check)

        self.variable_manager.extra_vars = load_extra_vars(
            loader=self.loader, options=self.options)
        self.variable_manager.options_vars = load_options_vars(self.options)

        self.variable_manager.set_inventory(self.inventory)

        # 初始化playbook的executor
        self.runner = PlaybookExecutor(playbooks=[self.playbook_path],
                                       inventory=self.inventory,
                                       variable_manager=self.variable_manager,
                                       loader=self.loader,
                                       options=self.options,
                                       passwords=self.passwords)

        if self.runner._tqm:
            self.runner._tqm._stdout_callback = self.callbackmodule
Exemplo n.º 32
0
    def run(self):

        super(PlaybookCLI, self).run()

        # Note: slightly wrong, this is written so that implicit localhost
        # Manage passwords
        sshpass = None
        becomepass = None
        vault_pass = None
        passwords = {}

        # don't deal with privilege escalation or passwords when we don't need to
        if not self.options.listhosts and not self.options.listtasks and not self.options.listtags and not self.options.syntax:
            self.normalize_become_options()
            (sshpass, becomepass) = self.ask_passwords()
            passwords = {'conn_pass': sshpass, 'become_pass': becomepass}

        if self.options.vault_password_file:
            # read vault_pass from a file
            vault_pass = CLI.read_vault_password_file(
                self.options.vault_password_file)
        elif self.options.ask_vault_pass:
            vault_pass = self.ask_vault_passwords(ask_vault_pass=True,
                                                  ask_new_vault_pass=False,
                                                  confirm_new=False)[0]

        loader = DataLoader(vault_password=vault_pass)

        # initial error check, to make sure all specified playbooks are accessible
        # before we start running anything through the playbook executor
        for playbook in self.args:
            if not os.path.exists(playbook):
                raise AnsibleError("the playbook: %s could not be found" %
                                   playbook)
            if not (os.path.isfile(playbook)
                    or stat.S_ISFIFO(os.stat(playbook).st_mode)):
                raise AnsibleError(
                    "the playbook: %s does not appear to be a file" % playbook)

        # create the variable manager, which will be shared throughout
        # the code, ensuring a consistent view of global variables
        variable_manager = VariableManager()
        variable_manager.extra_vars = load_extra_vars(loader=loader,
                                                      options=self.options)

        # create the inventory, and filter it based on the subset specified (if any)
        inventory = Inventory(loader=loader,
                              variable_manager=variable_manager,
                              host_list=self.options.inventory)
        variable_manager.set_inventory(inventory)

        # (which is not returned in list_hosts()) is taken into account for
        # warning if inventory is empty.  But it can't be taken into account for
        # checking if limit doesn't match any hosts.  Instead we don't worry about
        # limit if only implicit localhost was in inventory to start with.
        #
        # Fix this when we rewrite inventory by making localhost a real host (and thus show up in list_hosts())
        no_hosts = False
        if len(inventory.list_hosts()) == 0:
            # Empty inventory
            self.display.warning(
                "provided hosts list is empty, only localhost is available")
            no_hosts = True
        inventory.subset(self.options.subset)
        if len(inventory.list_hosts()) == 0 and no_hosts is False:
            # Invalid limit
            raise AnsibleError("Specified --limit does not match any hosts")

        # create the playbook executor, which manages running the plays via a task queue manager
        pbex = PlaybookExecutor(playbooks=self.args,
                                inventory=inventory,
                                variable_manager=variable_manager,
                                loader=loader,
                                display=self.display,
                                options=self.options,
                                passwords=passwords)

        results = pbex.run()

        if isinstance(results, list):
            for p in results:

                self.display.display('\nplaybook: %s' % p['playbook'])
                i = 1
                for play in p['plays']:
                    if play.name:
                        playname = play.name
                    else:
                        playname = '#' + str(i)

                    msg = "\n  PLAY: %s" % (playname)
                    mytags = set()
                    if self.options.listtags and play.tags:
                        mytags = mytags.union(set(play.tags))
                        msg += '    TAGS: [%s]' % (','.join(mytags))

                    if self.options.listhosts:
                        playhosts = set(inventory.get_hosts(play.hosts))
                        msg += "\n    pattern: %s\n    hosts (%d):" % (
                            play.hosts, len(playhosts))
                        for host in playhosts:
                            msg += "\n      %s" % host

                    self.display.display(msg)

                    if self.options.listtags or self.options.listtasks:
                        taskmsg = '    tasks:'

                        for block in play.compile():
                            if not block.has_tasks():
                                continue

                            j = 1
                            for task in block.block:
                                taskmsg += "\n      %s" % task
                                if self.options.listtags and task.tags:
                                    taskmsg += "    TAGS: [%s]" % ','.join(
                                        mytags.union(set(task.tags)))
                                j = j + 1

                        self.display.display(taskmsg)

                    i = i + 1
            return 0
        else:
            return results
Exemplo n.º 33
0
    def execute_playbook(self, playbook_info):
        output = None
        try:
            loader = DataLoader()
            inventory = InventoryManager(loader=loader, sources=['localhost'])
            variable_manager = VariableManager(loader=loader,
                                               inventory=inventory)

            Options = namedtuple('Options', [
                'listtags', 'listtasks', 'listhosts', 'syntax', 'connection',
                'module_path', 'forks', 'remote_user', 'private_key_file',
                'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args',
                'scp_extra_args', 'become', 'become_method', 'become_user',
                'verbosity', 'check', 'diff'
            ])
            options = Options(listtags=False,
                              listtasks=False,
                              listhosts=False,
                              syntax=False,
                              connection='ssh',
                              module_path=None,
                              forks=100,
                              remote_user=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,
                              diff=False)

            variable_manager.extra_vars = playbook_info['extra_vars']

            pbex = PlaybookExecutor(playbooks=[playbook_info['uri']],
                                    inventory=inventory,
                                    variable_manager=variable_manager,
                                    loader=loader,
                                    options=options,
                                    passwords=None)
            ret_val = pbex.run()

            output = self.get_plugin_output(pbex)

            if ret_val != 0:
                msg = MsgBundle.getMessage(
                    MsgBundle.PLAYBOOK_RETURN_WITH_ERROR)
                raise Exception(msg)

            if output is None or output.get('status') is None:
                msg = MsgBundle.getMessage(MsgBundle.PLAYBOOK_OUTPUT_MISSING)
                raise Exception(msg)

            if output.get('status').lower() == "failure":
                msg = MsgBundle.getMessage(MsgBundle.PLAYBOOK_STATUS_FAILED)
                raise Exception(msg)

            return output
        except Exception as exp:
            msg = MsgBundle.getMessage(MsgBundle.PLAYBOOK_EXECUTE_ERROR,
                                       playbook_uri=playbook_info['uri'],
                                       execution_id=playbook_info['extra_vars']
                                       ['playbook_input']['job_execution_id'],
                                       exc_msg=repr(exp))
            if exp.message:
                msg = msg + "\n" + exp.message

            JM_LOGGER.error(msg)

            # after handling exception, write an END
            # to stop listening to the file if created
            unique_pb_id = playbook_info['extra_vars']['playbook_input'][
                'unique_pb_id']
            exec_id = playbook_info['extra_vars']['playbook_input'][
                'job_execution_id']
            self._job_file_write.write_to_file(exec_id, unique_pb_id,
                                               JobFileWrite.PLAYBOOK_OUTPUT,
                                               json.dumps(output))
            with open("/tmp/" + exec_id, "a") as f:
                f.write(unique_pb_id + 'END' + PLAYBOOK_EOL_PATTERN)
            sys.exit(msg)
    def execute_playbook(self, playbook_info):
        try:
            loader = DataLoader()
            inventory = InventoryManager(loader=loader, sources=['localhost'])
            variable_manager = VariableManager(loader=loader,
                                               inventory=inventory)

            Options = namedtuple('Options', [
                'listtags', 'listtasks', 'listhosts', 'syntax', 'connection',
                'module_path', 'forks', 'remote_user', 'private_key_file',
                'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args',
                'scp_extra_args', 'become', 'become_method', 'become_user',
                'verbosity', 'check', 'diff'
            ])
            options = Options(listtags=False,
                              listtasks=False,
                              listhosts=False,
                              syntax=False,
                              connection='ssh',
                              module_path=None,
                              forks=100,
                              remote_user='******',
                              private_key_file=None,
                              ssh_common_args=None,
                              ssh_extra_args=None,
                              sftp_extra_args=None,
                              scp_extra_args=None,
                              become=None,
                              become_method=None,
                              become_user=None,
                              verbosity=None,
                              check=False,
                              diff=False)

            variable_manager.extra_vars = playbook_info['extra_vars']

            pbex = PlaybookExecutor(playbooks=[playbook_info['uri']],
                                    inventory=inventory,
                                    variable_manager=variable_manager,
                                    loader=loader,
                                    options=options,
                                    passwords=None)
            ret_val = pbex.run()
            if ret_val != 0:
                msg = MsgBundle.getMessage(
                    MsgBundle.PLAYBOOK_RETURN_WITH_ERROR)
                raise Exception(msg)
            output = self.get_plugin_output(pbex)
            if output is None or output.get('status') is None:
                msg = MsgBundle.getMessage(MsgBundle.PLAYBOOK_OUTPUT_MISSING)
                raise Exception(msg)

            if output.get('status').lower() == "failure":
                msg = MsgBundle.getMessage(MsgBundle.PLAYBOOK_STATUS_FAILED)
                raise Exception(msg)

            return output
        except Exception as e:
            msg = MsgBundle.getMessage(MsgBundle.PLAYBOOK_EXECUTE_ERROR,
                                       playbook_uri=playbook_info['uri'],
                                       execution_id=playbook_info['extra_vars']
                                       ['playbook_input']['job_execution_id'],
                                       exc_msg=repr(e))
            if e.message:
                msg = msg + "\n" + e.message

            JM_LOGGER.error(msg)
            sys.exit(msg)
                                become=True, become_method='sudo',
                                become_user='******', verbosity=4, check=False, diff=False, start_at_task=None)
"""

context.CLIARGS = ImmutableDict(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='local',
                                forks=100, private_key_file=None, become=True, become_method='sudo', become_user='******',
                                check=False, diff=False)



callback = ResultCallback1()

try:
    executor = PlaybookExecutor(
        playbooks=[git_playbook_path],
        inventory=inventory,
        variable_manager=variable_manager,
        loader=loader,        
        passwords=passwords)

    #executor._tqm._stdout_callback = callback
    print('------------------->',inventory.get_hosts())
    executor.run()

    print("UP ***********")

    for host, result in callback.host_failed.items():
        print('{0} >>> {1}'.format(host, result._result.keys()))
        for key in result._result.keys():
            print("Key: {0} Val : {1}".format(key, result._result[key]))
        
    print('<-----------------------------Execution completed------------------------>')
Exemplo n.º 36
0
    def run(self):

        super(PlaybookCLI, self).run()

        # Note: slightly wrong, this is written so that implicit localhost
        # manages passwords
        sshpass = None
        becomepass = None
        passwords = {}

        # initial error check, to make sure all specified playbooks are accessible
        # before we start running anything through the playbook executor

        b_playbook_dirs = []
        for playbook in context.CLIARGS['args']:
            if not os.path.exists(playbook):
                raise AnsibleError("the playbook: %s could not be found" % playbook)
            if not (os.path.isfile(playbook) or stat.S_ISFIFO(os.stat(playbook).st_mode)):
                raise AnsibleError("the playbook: %s does not appear to be a file" % playbook)

            b_playbook_dir = os.path.dirname(os.path.abspath(to_bytes(playbook, errors='surrogate_or_strict')))
            # load plugins from all playbooks in case they add callbacks/inventory/etc
            add_all_plugin_dirs(b_playbook_dir)

            b_playbook_dirs.append(b_playbook_dir)

        set_collection_playbook_paths(b_playbook_dirs)

        # don't deal with privilege escalation or passwords when we don't need to
        if not (context.CLIARGS['listhosts'] or context.CLIARGS['listtasks'] or
                context.CLIARGS['listtags'] or context.CLIARGS['syntax']):
            (sshpass, becomepass) = self.ask_passwords()
            passwords = {'conn_pass': sshpass, 'become_pass': becomepass}

        # create base objects
        loader, inventory, variable_manager = self._play_prereqs()

        # (which is not returned in list_hosts()) is taken into account for
        # warning if inventory is empty.  But it can't be taken into account for
        # checking if limit doesn't match any hosts.  Instead we don't worry about
        # limit if only implicit localhost was in inventory to start with.
        #
        # Fix this when we rewrite inventory by making localhost a real host (and thus show up in list_hosts())
        CLI.get_host_list(inventory, context.CLIARGS['subset'])

        # flush fact cache if requested
        if context.CLIARGS['flush_cache']:
            self._flush_cache(inventory, variable_manager)

        # create the playbook executor, which manages running the plays via a task queue manager
        pbex = PlaybookExecutor(playbooks=context.CLIARGS['args'], inventory=inventory,
                                variable_manager=variable_manager, loader=loader,
                                passwords=passwords, stdout_callback=self.callback)

        results = pbex.run()

        if isinstance(results, list):
            for p in results:

                display.display('\nplaybook: %s' % p['playbook'])
                for idx, play in enumerate(p['plays']):
                    if play._included_path is not None:
                        loader.set_basedir(play._included_path)
                    else:
                        pb_dir = os.path.realpath(os.path.dirname(p['playbook']))
                        loader.set_basedir(pb_dir)

                    msg = "\n  play #%d (%s): %s" % (idx + 1, ','.join(play.hosts), play.name)
                    mytags = set(play.tags)
                    msg += '\tTAGS: [%s]' % (','.join(mytags))

                    if context.CLIARGS['listhosts']:
                        playhosts = set(inventory.get_hosts(play.hosts))
                        msg += "\n    pattern: %s\n    hosts (%d):" % (play.hosts, len(playhosts))
                        for host in playhosts:
                            msg += "\n      %s" % host

                    display.display(msg)

                    all_tags = set()
                    if context.CLIARGS['listtags'] or context.CLIARGS['listtasks']:
                        taskmsg = ''
                        if context.CLIARGS['listtasks']:
                            taskmsg = '    tasks:\n'

                        def _process_block(b):
                            taskmsg = ''
                            for task in b.block:
                                if isinstance(task, Block):
                                    taskmsg += _process_block(task)
                                else:
                                    if task.action == 'meta':
                                        continue

                                    all_tags.update(task.tags)
                                    if context.CLIARGS['listtasks']:
                                        cur_tags = list(mytags.union(set(task.tags)))
                                        cur_tags.sort()
                                        if task.name:
                                            taskmsg += "      %s" % task.get_name()
                                        else:
                                            taskmsg += "      %s" % task.action
                                        taskmsg += "\tTAGS: [%s]\n" % ', '.join(cur_tags)

                            return taskmsg

                        all_vars = variable_manager.get_vars(play=play)
                        for block in play.compile():
                            block = block.filter_tagged_tasks(all_vars)
                            if not block.has_tasks():
                                continue
                            taskmsg += _process_block(block)

                        if context.CLIARGS['listtags']:
                            cur_tags = list(mytags.union(all_tags))
                            cur_tags.sort()
                            taskmsg += "      TASK TAGS: [%s]\n" % ', '.join(cur_tags)

                        display.display(taskmsg)

            return 0
        else:
            return results
Exemplo n.º 37
0
    verbosity=5,
    forks=10,
    become=None,
    become_method=None,
    become_user=None,
    check=False,
    diff=False,
    syntax=None,
    start_at_task=None,
    gather_facts='no',
)
passwords = dict()

play_book = PlaybookExecutor(playbooks=['testplaybook.yml'],
                             inventory=inventory_manager,
                             variable_manager=variable_manager,
                             loader=loader,
                             passwords=passwords)


# Create a callback plugin so we can capture the output
class ResultsCollectorJSONCallback(CallbackBase):
    """A sample callback plugin used for performing an action as results come in.

    If you want to collect all results into a single object for processing at
    the end of the execution, look into utilizing the ``json`` callback plugin
    or writing your own custom callback plugin.
    """
    def __init__(self, *args, **kwargs):
        super(ResultsCollectorJSONCallback, self).__init__(*args, **kwargs)
        self.host_ok = {}
Exemplo n.º 38
0
                  scp_extra_args=None,
                  ssh_extra_args=None)

# 定义默认的密码连接,主机未定义密码的时候才生效,conn_pass指连接远端的密码,become_pass指提升权限的密码
passwords = dict(conn_pass='******', become_pass='******')

# create inventory and pass to var manager
# 创建inventory、并带进去参数
inventory = InventoryManager(loader=loader, variable_manager=variable_manager, host_list='/root/ansible/playbooks/hosts')

# 把inventory传递给variable_manager管理
variable_manager.set_inventory(inventory)

# 多个yaml文件则以列表形式
playbook_path = ['/root/ansible/playbooks/mysql.yml',
                 '/root/ansible/playbooks/test_result.yml']
for playbook in playbook_path:
    if not os.path.exists(playbook):
        print('[INFO] The [%s] playbook does not exist') % playbook
        sys.exit()

playbook = PlaybookExecutor(playbooks=playbook_path,
                            inventory=inventory,
                            variable_manager=variable_manager,
                            loader=loader,
                            options=options,
                            passwords=passwords)
# 执行playbook
result = playbook.run()

print('执行结果: %s') % (result)
class ContrailAnsiblePlayBook(multiprocessing.Process):
    def validate_provision_params(self, inv, defaults):

        keys_to_check = [
            "ansible_playbook", "docker_insecure_registries",
            "docker_registry_insecure"
        ]

        params = inv.get("[all:vars]", None)
        if params == None:
            return ("[all:vars] not defined")

        for x in keys_to_check:
            if not x in params.keys():
                if x == "docker_insecure_registries":
                    params['docker_insecure_registries'] = \
                     defaults.docker_insecure_registries
                elif x == 'docker_registry_insecure':
                    params['docker_registry_insecure'] = \
                     defaults.docker_registry_insecure
                elif x == 'ansible_playbook':
                    params['ansible_playbook'] = \
                     defaults.ansible_playbook
                else:
                    return ("%s not defined in inventory" % x)

        for k, v in vars(defaults).iteritems():
            if not k in params.keys():
                params[k] = v

        pbook = params['ansible_playbook']
        try:
            with open(pbook) as file:
                pass
        except IOError as e:
            return ("Playbook not found : %s" % pbook)

        return STATUS_VALID

    def __init__(self, json_entity, args):
        super(ContrailAnsiblePlayBook, self).__init__()
        lb = []
        agent = []
        inv_file = None
        self.hosts_in_inv = json_entity[0]["hosts_in_inv"]
        cluster_id = json_entity[0]["cluster_id"]
        parameters = json_entity[0]["parameters"]
        inventory = parameters["inventory"]
        self.current_status = self.validate_provision_params(inventory, args)
        self.pbook_path = inventory["[all:vars]"]["ansible_playbook"]
        pbook_dir = os.path.dirname(self.pbook_path)

        inv_dir = pbook_dir + '/inventory/'
        inv_file = inv_dir + cluster_id + ".inv"
        create_inv_file(inv_file, inventory)

        self.var_mgr = VariableManager()
        self.ldr = DataLoader()
        self.args = args
        self.inventory = Inventory(loader=self.ldr,
                                   variable_manager=self.var_mgr,
                                   host_list=inv_file)
        self.var_mgr.set_inventory(self.inventory)

        Options = namedtuple('Options', [
            'connection', 'forks', 'module_path', 'become', 'become_method',
            'become_user', 'check', 'listhosts', 'listtasks', 'listtags',
            'syntax', 'verbosity'
        ])
        self.options = Options(connection='ssh',
                               forks=100,
                               module_path=None,
                               become=True,
                               become_method='sudo',
                               become_user='******',
                               check=False,
                               listhosts=None,
                               listtasks=None,
                               listtags=None,
                               syntax=None,
                               verbosity=None)

        self.pws = {}
        self.pb_executor = PlaybookExecutor(playbooks=[self.pbook_path],
                                            inventory=self.inventory,
                                            variable_manager=self.var_mgr,
                                            loader=self.ldr,
                                            options=self.options,
                                            passwords=self.pws)
        try:
            self._sm_logger = ServerMgrlogger()
        except:
            f = open("/var/log/contrail-server-manager/debug.log", "a")
            f.write("Ansible Callback Init - ServerMgrlogger init failed\n")
            f.close()

    def update_status(self):
        for h in self.hosts_in_inv:
            status_resp = {"server_id": h, "state": self.current_status}
            send_REST_request(self.args.ansible_srvr_ip,
                              SM_STATUS_PORT,
                              "ansible_status",
                              urllib.urlencode(status_resp),
                              method='PUT',
                              urlencode=True)

    def run(self):
        #import pdb; pdb.set_trace()
        stats = None
        if self.current_status == STATUS_VALID:
            self.current_status = STATUS_IN_PROGRESS

            self.update_status()
            try:
                rv = self.pb_executor.run()
            except Exception as e:
                self._sm_logger.log(self._sm_logger.ERROR, e)
                self.current_status = STATUS_FAILED
                self.update_status()
                return None

            stats = self.pb_executor._tqm._stats

            if rv == 0:
                self.current_status = STATUS_SUCCESS
            else:
                self.current_status = STATUS_FAILED

            # No need to update_status here. Per node status gets sent from
            # sm_ansible_callback.py

        else:
            self.update_status()
        return stats
Exemplo n.º 40
0
if len(host_list) == 1:
    sources += ','

#inventory1 = InventoryManager(loader=loader,sources=host_list)

inventoryManager2 = InventoryManager(loader=loader, sources="1.1.1.1.1, 12.2.4.5")
#inventoryManager2.add_host('12.12.111.420')

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



passwords = {}

# context.CLIARGS = ImmutableDict(connection='local', module_path=['/to/mymodules'], forks=10, become=None,
#       become_method=None, become_user=None, check=False, diff=False, syntax=False)

context.CLIARGS = ImmutableDict(listtags=False, listtasks=False, listhosts=False, syntax=False, connection='local',
                                module_path=None, forks=100, private_key_file=None,
                                ssh_common_args=None, ssh_extra_args=None, sftp_extra_args=None, scp_extra_args=None,
                                become=True, become_method='sudo',
                                become_user='******', verbosity=None, check=False, diff=False)

playbook = PlaybookExecutor(playbooks=[playbook_path], inventory=inventoryManager2,
                            variable_manager=variable_manager,
                            loader=loader, passwords=passwords)
res = playbook.run()

print(res)
print('The End')
Exemplo n.º 41
0
def push(request):
    request.COOKIES["username"] and request.session["username"]
    cookie_name = request.COOKIES["username"]
    if request.method == "POST" and request.POST:
        ipstr = request.POST["ip"]
        userstr = request.POST["user"]
        iplist = json.loads(ipstr)
        userlist = json.loads(userstr)
        pubList = []  #定义一个存放公钥的空列表
        userid_list = []  #定义一个存放用户id的空列表
        for user in userlist:  #遍历前端勾选的用户列表
            RsaDb = Rsa.objects.filter(user=user)  #过滤出勾选的用户的数据
            for rsaobj in RsaDb:
                pubList.append(rsaobj.rsa_pub)  #把勾选用户的公钥添加到pubList中
                userid_list.append(int(rsaobj.id))  #把勾选用户的id添加到userid_list中
        ipid_list = []  #定义一个存放主机id的空列表
        for ip in iplist:  #遍历前端勾选的ip地址列表
            HostDb = Hardware.objects.filter(ip=ip)  #过滤出勾选的ip的数据
            for hostobj in HostDb:
                ipid_list.append(int(hostobj.id))  #把勾选的主机id添加到ipid_list中

        # 把用户id,主机id写入数据库的权限表,权限表为:user_id,host_id
        for uid in userid_list:
            for pid in ipid_list:
                if not UserPermission.objects.filter(user_id=uid, host_id=pid):
                    up = UserPermission()
                    up.user_id = uid
                    up.host_id = pid
                    up.save()
        try:
            #调用ansible接口执行playbook,实现密钥的推送功能
            loader = DataLoader()  #用来加载解析yaml文件或JSON内容,并且支持vault的解密
            variable_manager = VariableManager(
            )  #管理变量的类,包括主机,组,扩展等变量,之前版本是在 inventory中的
            inventory = Inventory(
                loader=loader,
                variable_manager=variable_manager,
                host_list=iplist)  #根据inventory加载对应变量,这里host_list可以是文件也可以是IP列表
            variable_manager.set_inventory(inventory)
            passwords = dict(
                conn_pass='')  #设置密码,必须是dict类型。如果ansible主机对服务器有密钥认证,则不需要密码
            Options = namedtuple('Options', [
                'connection', 'remote_user', 'ask_sudo_pass', 'verbosity',
                'ack_pass', 'module_path', 'forks', 'become', 'become_method',
                'become_user', 'check', 'listhosts', 'listtasks', 'listtags',
                'syntax', 'sudo_user', 'sudo'
            ])  #初始化需要的对象
            options = Options(connection='smart',
                              remote_user='******',
                              ack_pass=None,
                              sudo_user='******',
                              forks=50,
                              sudo='yes',
                              ask_sudo_pass=False,
                              verbosity=5,
                              module_path=None,
                              become=True,
                              become_method='sudo',
                              become_user='******',
                              check=None,
                              listhosts=None,
                              listtasks=None,
                              listtags=None,
                              syntax=None)
            extra_vars = {
            }  #额外的参数,key.yml以及模板中的参数,对应ansible-playbook key.yml --extra-vars pub=xxx state=present/absent
            extra_vars["pub_list"] = pubList
            extra_vars["state"] = "present"
            variable_manager.extra_vars = extra_vars
            #playbooks填写yml文件路径,可以写多个,是个列表
            playbook = PlaybookExecutor(playbooks=['/home/ansible/key.yml'],
                                        inventory=inventory,
                                        variable_manager=variable_manager,
                                        loader=loader,
                                        options=options,
                                        passwords=passwords)
            playbook.run()  #执行
            state = "授权成功"
        except:
            state = "推送失败,请检查程序"
    else:
        state = "test"
    return JsonResponse({"state": state})
Exemplo n.º 42
0
class PlayBookRunner(object):
    """
    用于执行AnsiblePlaybook的接口.简化Playbook对象的使用.
    """
    Options = namedtuple('Options', [
        'listtags', 'listtasks', 'listhosts', 'syntax', 'connection',
        'module_path', 'forks', 'remote_user', 'private_key_file', 'timeout',
        'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args',
        'scp_extra_args', 'become', 'become_method', 'become_user',
        'verbosity', 'check', 'extra_vars'
    ])

    def __init__(self,
                 hosts=None,
                 playbook_path=None,
                 forks=C.DEFAULT_FORKS,
                 listtags=False,
                 listtasks=False,
                 listhosts=False,
                 syntax=False,
                 module_path=None,
                 remote_user='******',
                 timeout=C.DEFAULT_TIMEOUT,
                 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,
                 extra_vars=None,
                 connection_type="ssh",
                 passwords=None,
                 private_key_file=None,
                 check=False):

        C.RETRY_FILES_ENABLED = False
        self.callbackmodule = PlaybookResultCallBack()
        if playbook_path is None or not os.path.exists(playbook_path):
            raise AnsibleError("Not Found the playbook file: %s." %
                               playbook_path)
        self.playbook_path = playbook_path
        self.loader = DataLoader()
        self.variable_manager = VariableManager()
        self.passwords = passwords or {}
        self.inventory = JMSInventory(hosts)

        self.options = self.Options(listtags=listtags,
                                    listtasks=listtasks,
                                    listhosts=listhosts,
                                    syntax=syntax,
                                    timeout=timeout,
                                    connection=connection_type,
                                    module_path=module_path,
                                    forks=forks,
                                    remote_user=remote_user,
                                    private_key_file=private_key_file,
                                    ssh_common_args=ssh_common_args or "",
                                    ssh_extra_args=ssh_extra_args or "",
                                    sftp_extra_args=sftp_extra_args,
                                    scp_extra_args=scp_extra_args,
                                    become=become,
                                    become_method=become_method,
                                    become_user=become_user,
                                    verbosity=verbosity,
                                    extra_vars=extra_vars or [],
                                    check=check)

        self.variable_manager.extra_vars = load_extra_vars(
            loader=self.loader, options=self.options)
        self.variable_manager.options_vars = load_options_vars(self.options)

        self.variable_manager.set_inventory(self.inventory)

        # 初始化playbook的executor
        self.runner = PlaybookExecutor(playbooks=[self.playbook_path],
                                       inventory=self.inventory,
                                       variable_manager=self.variable_manager,
                                       loader=self.loader,
                                       options=self.options,
                                       passwords=self.passwords)

        if self.runner._tqm:
            self.runner._tqm._stdout_callback = self.callbackmodule

    def run(self):
        if not self.inventory.list_hosts('all'):
            raise AnsibleError('Inventory is empty')
        self.runner.run()
        self.runner._tqm.cleanup()
        return self.callbackmodule.output
Exemplo n.º 43
0
def invoke_ansible_playbook(module_path, e_vars, playbook_path="site.yml", console=True):
    """ Invokes playbook """
    
    loader = DataLoader()
    variable_manager = VariableManager()
    variable_manager.extra_vars = e_vars
    inventory = Inventory(loader=loader,
                          variable_manager=variable_manager,
                          host_list=['localhost'])
    passwords = {}
    utils.VERBOSITY = 4
    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'])
    options = Options(listtags=False,
                      listtasks=False,
                      listhosts=False,
                      syntax=False,
                      connection='ssh',
                      module_path=module_path,
                      forks=100,
                      remote_user='******',
                      private_key_file=None,
                      ssh_common_args=None,
                      ssh_extra_args=None,
                      sftp_extra_args=None,
                      scp_extra_args=None,
                      become=False,
                      become_method=None,
                      become_user='******',
                      verbosity=utils.VERBOSITY,
                      check=False)
    pbex = PlaybookExecutor(playbooks=[playbook_path],
                            inventory=inventory,
                            variable_manager=variable_manager,
                            loader=loader,
                            options=options,
                            passwords=passwords)
    if not console:
        cb = PlaybookCallback()
        pbex._tqm._stdout_callback = cb
        return_code = pbex.run()
        results = cb.results
    else:
        results = pbex.run()
    return results
Exemplo n.º 44
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
Exemplo n.º 45
0
    def run(self):

        super(PlaybookCLI, self).run()

        # Note: slightly wrong, this is written so that implicit localhost
        # Manage passwords
        sshpass = None
        becomepass = None
        vault_pass = None
        passwords = {}

        # don't deal with privilege escalation or passwords when we don't need to
        if not self.options.listhosts and not self.options.listtasks and not self.options.listtags and not self.options.syntax:
            self.normalize_become_options()
            (sshpass, becomepass) = self.ask_passwords()
            passwords = {'conn_pass': sshpass, 'become_pass': becomepass}

        if self.options.vault_password_file:
            # read vault_pass from a file
            vault_pass = read_vault_file(self.options.vault_password_file)
        elif self.options.ask_vault_pass:
            vault_pass = self.ask_vault_passwords(ask_vault_pass=True,
                                                  ask_new_vault_pass=False,
                                                  confirm_new=False)[0]

        loader = DataLoader(vault_password=vault_pass)

        extra_vars = {}
        for extra_vars_opt in self.options.extra_vars:
            extra_vars_opt = to_unicode(extra_vars_opt, errors='strict')
            if extra_vars_opt.startswith(u"@"):
                # Argument is a YAML file (JSON is a subset of YAML)
                data = loader.load_from_file(extra_vars_opt[1:])
            elif extra_vars_opt and extra_vars_opt[0] in u'[{':
                # Arguments as YAML
                data = loader.load(extra_vars_opt)
            else:
                # Arguments as Key-value
                data = parse_kv(extra_vars_opt)
            extra_vars = combine_vars(extra_vars, data)

        # FIXME: this should be moved inside the playbook executor code
        only_tags = self.options.tags.split(",")
        skip_tags = self.options.skip_tags
        if self.options.skip_tags is not None:
            skip_tags = self.options.skip_tags.split(",")

        # initial error check, to make sure all specified playbooks are accessible
        # before we start running anything through the playbook executor
        for playbook in self.args:
            if not os.path.exists(playbook):
                raise AnsibleError("the playbook: %s could not be found" %
                                   playbook)
            if not (os.path.isfile(playbook)
                    or stat.S_ISFIFO(os.stat(playbook).st_mode)):
                raise AnsibleError(
                    "the playbook: %s does not appear to be a file" % playbook)

        # create the variable manager, which will be shared throughout
        # the code, ensuring a consistent view of global variables
        variable_manager = VariableManager()
        variable_manager.extra_vars = extra_vars

        # create the inventory, and filter it based on the subset specified (if any)
        inventory = Inventory(loader=loader,
                              variable_manager=variable_manager,
                              host_list=self.options.inventory)
        variable_manager.set_inventory(inventory)

        # (which is not returned in list_hosts()) is taken into account for
        # warning if inventory is empty.  But it can't be taken into account for
        # checking if limit doesn't match any hosts.  Instead we don't worry about
        # limit if only implicit localhost was in inventory to start with.
        #
        # Fix this when we rewrite inventory by making localhost a real host (and thus show up in list_hosts())
        no_hosts = False
        if len(inventory.list_hosts()) == 0:
            # Empty inventory
            self.display.warning(
                "provided hosts list is empty, only localhost is available")
            no_hosts = True
        inventory.subset(self.options.subset)
        if len(inventory.list_hosts()) == 0 and no_hosts is False:
            # Invalid limit
            raise AnsibleError("Specified --limit does not match any hosts")

        # create the playbook executor, which manages running the plays via a task queue manager
        pbex = PlaybookExecutor(playbooks=self.args,
                                inventory=inventory,
                                variable_manager=variable_manager,
                                loader=loader,
                                display=self.display,
                                options=self.options,
                                passwords=passwords)

        results = pbex.run()

        if isinstance(results, list):
            for p in results:

                self.display.display('\nplaybook: %s\n' % p['playbook'])
                for play in p['plays']:
                    if self.options.listhosts:
                        self.display.display("\n  %s (%s): host count=%d" %
                                             (play['name'], play['pattern'],
                                              len(play['hosts'])))
                        for host in play['hosts']:
                            self.display.display("    %s" % host)
                    if self.options.listtasks:  #TODO: do we want to display block info?
                        self.display.display("\n  %s" % (play['name']))
                        for task in play['tasks']:
                            self.display.display("    %s" % task)
                    if self.options.listtags:  #TODO: fix once we figure out block handling above
                        self.display.display("\n  %s: tags count=%d" %
                                             (play['name'], len(play['tags'])))
                        for tag in play['tags']:
                            self.display.display("    %s" % tag)
            return 0
        else:
            return results
Exemplo n.º 46
0
    def _invoke_playbook(self, playbook='up', console=True):
        """
        Uses the Ansible API code to invoke the specified linchpin playbook

        :param playbook: Which ansible playbook to run (default: 'up')
        :param console: Whether to display the ansible console (default: True)
        """

        pb_path = '{0}/{1}'.format(
            self.lp_path, self.ctx.get_evar('playbooks_folder', 'provision'))
        module_path = '{0}/{1}/'.format(
            pb_path, self.get_cfg('lp', 'module_folder', 'library'))
        playbook_path = '{0}/{1}'.format(
            pb_path, self.get_cfg('playbooks', playbook, 'site.yml'))

        loader = DataLoader()
        variable_manager = VariableManager()
        variable_manager.extra_vars = self.get_evar()
        inventory = Inventory(loader=loader,
                              variable_manager=variable_manager,
                              host_list=[])
        passwords = {}
        # utils.VERBOSITY = 4

        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'
        ])

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

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

        if not console:
            results = {}
            return_code = 0

            cb = PlaybookCallback()

            with suppress_stdout():
                pbex._tqm._stdout_callback = cb

            return_code = pbex.run()
            results = cb.results

            return return_code, results
        else:
            # the console only returns a return_code
            return_code = pbex.run()
            return return_code, None
Exemplo n.º 47
0
def run_ansible(playbooks,
                inventory_path=None,
                roles=None,
                extra_vars=None,
                tags=None,
                on_error_continue=False,
                basedir='.'):
    """Run Ansible.

    Args:
        playbooks (list): list of paths to the playbooks to run
        inventory_path (str): path to the hosts file (inventory)
        extra_var (dict): extra vars to pass
        tags (list): list of tags to run
        on_error_continue(bool): Don't throw any exception in case a host is
            unreachable or the playbooks run with errors

    Raises:
        :py:class:`enoslib.errors.EnosFailedHostsError`: if a task returns an
            error on a host and ``on_error_continue==False``
        :py:class:`enoslib.errors.EnosUnreachableHostsError`: if a host is
            unreachable (through ssh) and ``on_error_continue==False``
    """

    inventory, variable_manager, loader, options = _load_defaults(
        inventory_path=inventory_path,
        roles=roles,
        extra_vars=extra_vars,
        tags=tags,
        basedir=basedir)
    passwords = {}
    for path in playbooks:
        logger.info("Running playbook %s with vars:\n%s" % (path, extra_vars))
        pbex = PlaybookExecutor(playbooks=[path],
                                inventory=inventory,
                                variable_manager=variable_manager,
                                loader=loader,
                                options=options,
                                passwords=passwords)

        code = pbex.run()
        stats = pbex._tqm._stats
        hosts = stats.processed.keys()
        result = [{h: stats.summarize(h)} for h in hosts]
        results = {"code": code, "result": result, "playbook": path}
        print(results)

        failed_hosts = []
        unreachable_hosts = []

        for h in hosts:
            t = stats.summarize(h)
            if t["failures"] > 0:
                failed_hosts.append(h)

            if t["unreachable"] > 0:
                unreachable_hosts.append(h)

        if len(failed_hosts) > 0:
            logger.error("Failed hosts: %s" % failed_hosts)
            if not on_error_continue:
                raise EnosFailedHostsError(failed_hosts)
        if len(unreachable_hosts) > 0:
            logger.error("Unreachable hosts: %s" % unreachable_hosts)
            if not on_error_continue:
                raise EnosUnreachableHostsError(unreachable_hosts)
Exemplo n.º 48
0
    def default(self):
        verbose = self.app.pargs.verbose

        init(autoreset=True)

        if len(self.app.pargs.extra_arguments) == 0:
            self.app.args.print_help()
            print(Fore.RED + 'Error! You must specify a playbook file to run')
            sys.exit(1)

        if str(self.app.pargs.become_method).upper() not in [
                'SUDO', 'SU', 'PBRUN', 'PFEXEC', 'DOAS', 'DZDO', 'KSU', 'RUNAS'
        ]:
            print(
                Fore.RED +
                'Error! Become method must be one of sudo, su, pbrun, pfexec, doas, dzdo, ksu or runas.'
            )
            sys.exit(1)

        #if len(self.app.pargs.extra_arguments) > 0:

        playbook_path = self.app.pargs.extra_arguments[0] if len(
            self.app.pargs.extra_arguments) > 0 else "site.yml"

        if not os.path.isfile(playbook_path):
            print(Fore.RED + 'Error! The playbook file does not exist')
            sys.exit(1)

        inventory_path = self.app.pargs.inventory

        if not os.path.isfile(inventory_path):
            print(Fore.RED + 'Error! The inventory file does not exist.')
            sys.exit(1)

        # Most of the code from here down is straight copy & paste from ansible source code,
        # with a few tweaks/hacks to clean up variables that we don't want to emit in the generated
        # YAML.
        loader = DataLoader()

        pb_cli = PlaybookCLI(sys.argv[1:])
        pb_cli.parse()

        # !!!!! WARNING: THESE WILL BE INCLUDED IN THE GENERATED YAML FILE !!!!!
        (ssh_pass, become_pass) = pb_cli.ask_passwords()
        passwords = {'conn_pass': ssh_pass, 'become_pass': become_pass}

        vault_pass = None

        if self.app.pargs.ask_vault_pass:
            vault_pass = pb_cli.ask_vault_passwords()
        else:
            vault_pass = pb_cli.read_vault_password_file(
                self.app.pargs.vault_password_file, loader)

        if vault_pass is not None:
            loader.set_vault_password(vault_pass)

        # create the inventory, and filter it based on the subset specified (if any)
        host_list = self.app.pargs.inventory if self.app.pargs.inventory else constants.DEFAULT_HOST_LIST

        # FIXME: This should parse any arguments provided via -e or --extra-vars. Currently it seems
        # to parse the argument value wrongly and returns
        # '_raw_params': '<last character in variable>'. This prevents the ability to use
        # --extra-vars when executing plays.
        variable_manager = VariableManager()

        options = Options(
            verbosity=self.app.pargs.verbose,
            inventory=self.app.pargs.inventory,
            subset=self.app.pargs.limit,
            extra_vars=[self.app.pargs.extra_vars],
            forks=self.app.pargs.forks,
            ask_vault_pass=self.app.pargs.ask_vault_pass,
            vault_password_files=self.app.pargs.vault_password_file,
            tags=self.app.pargs.tags,
            skip_tags=self.app.pargs.skip_tags,
            become=self.app.pargs.become,
            become_method=self.app.pargs.become_method,
            become_user=self.app.pargs.become_user,
            become_ask_pass=self.app.pargs.ask_become_pass,
            ask_pass=self.app.pargs.ask_pass,
            private_key_file=self.app.pargs.private_key,
            remote_user=self.app.pargs.user,
            connection=self.app.pargs.connection,
            timeout=self.app.pargs.timeout,
            ssh_common_args=self.app.pargs.ssh_common_args,
            sftp_extra_args=self.app.pargs.sftp_extra_args,
            ssh_extra_args=self.app.pargs.ssh_extra_args,
            scp_extra_args=self.app.pargs.scp_extra_args,
            flush_cache=self.app.pargs.flush_cache,
            module_path=self.app.pargs.module_path)

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

        variable_manager.extra_vars = load_extra_vars(loader=loader,
                                                      options=options)
        #variable_manager.options_vars = load_options_vars(options)

        if self.app.pargs.flush_cache:
            inventory.refresh_inventory()

        no_hosts = False

        if len(inventory.list_hosts()) == 0:
            print(
                Fore.YELLOW +
                "Warning! Provided hosts list is empty, only localhost is available."
            )
            no_hosts = True

        # FIXME: Limit is currently ignored.
        inventory.subset(self.app.pargs.limit)

        if len(inventory.list_hosts()) == 0 and no_hosts is False:
            # Invalid limit
            print(Fore.RED +
                  "Error! Specified --limit does not match any hosts.")
            sys.exit(1)

        play_data = loader.load_from_file(playbook_path)

        host_list = inventory.get_hosts(
            pattern=self.app.pargs.limit
        ) if self.app.pargs.limit else inventory.get_hosts()

        playbook = Playbook(loader=loader).load(
            playbook_path, variable_manager=variable_manager, loader=loader)

        pbex = PlaybookExecutor(playbooks=playbook.get_plays(),
                                inventory=inventory,
                                variable_manager=variable_manager,
                                loader=loader,
                                options=options,
                                passwords=passwords)

        # Ansible variables matching these strings will be excluded from the generated yaml content.
        ignored_elements = [
            'ansible_playbook_python', 'ansible_version', 'group_names',
            'inventory_hostname', 'inventory_hostname_short', 'omit',
            'playbook_dir', 'role_names'
        ]

        json_data = {}

        for host in host_list:
            host_vars = host.get_vars()
            host_name = host_vars[
                'ansible_host'] if 'ansible_host' in host_vars.keys(
                ) else host.get_name()

            for play in playbook.get_plays():
                loader.set_vault_password(vault_pass)
                all_vars = variable_manager.get_vars(loader=loader,
                                                     play=play,
                                                     host=host)

                json_data[host_name] = all_vars['vars']

                json_data[host_name]['ansible_groups'] = all_vars[
                    'group_names']
                json_data[host_name]['ansible_roles'] = all_vars['role_names']

                for elem in ignored_elements:
                    del json_data['{}'.format(host)][elem]

                json_data[host_name][
                    'ansible_become_user'] = self.app.pargs.become_user

                if passwords['become_pass'] is not None:
                    json_data[host_name]['ansible_become_pass'] = passwords[
                        'become_pass']

                json_data[host_name]['ansible_become'] = self.app.pargs.become
                json_data[host_name][
                    'ansible_become_method'] = self.app.pargs.become_method

                if self.app.pargs.ssh_extra_args:
                    json_data[host_name][
                        'ansible_ssh_extra_args'] = self.app.pargs.ssh_extra_args

                if self.app.pargs.scp_extra_args:
                    json_data[host_name][
                        'ansible_scp_extra_args'] = self.app.pargs.scp_extra_args

                if self.app.pargs.sftp_extra_args:
                    json_data[host_name][
                        'ansible_sftp_extra_args'] = self.app.pargs.sftp_extra_args

                if self.app.pargs.ssh_common_args:
                    json_data[host_name][
                        'ansible_ssh_common_args'] = self.app.pargs.ssh_common_args

                json_data[host_name][
                    'ansible_connection_timeout'] = self.app.pargs.timeout
                json_data[host_name][
                    'ansible_connection_method'] = self.app.pargs.connection

                if self.app.pargs.private_key:
                    json_data[host_name][
                        'ansible_private_key'] = self.app.pargs.private_key

                json_data[host_name][
                    'ansible_ask_pass'] = self.app.pargs.ask_pass

                if self.app.pargs.limit:
                    json_data[host_name][
                        'ansible_limit'] = self.app.pargs.limit

                # FIXME: Extra vars needs to be processed by Ansible instead of dumping them
                # here as a "dumb" string.
                if self.app.pargs.extra_vars:
                    json_data[host_name][
                        'ansible_extra_vars'] = self.app.pargs.extra_vars

            for key, value in json_data[host_name].iteritems():
                if type(value) is AnsibleVaultEncryptedUnicode:
                    json_data[host_name][key] = str(value)

        # Convert the processed python dictionary to a valid json string
        json_obj = json.dumps(json_data)

        # Take the json string, and convert it to a valid yaml string
        yml = YAML(typ="safe", pure=True)
        yml.default_flow_style = False

        yml_obj = yml.load(json_obj)

        if self.app.pargs.output is None:
            yml.dump(yml_obj, sys.stdout)
        else:
            with open(self.app.pargs.output, 'w') as outfile:
                yml.dump(yml_obj, outfile)

            print(Fore.GREEN +
                  "Ansible variable dictionary written to {}".format(
                      self.app.pargs.output))

        sys.exit(1)
Exemplo n.º 49
0
def run_playbooks(playbook, tags=[], context=None, options=None):
    """
    @param playbook: The playbook(s) to be run.
    @type playbook: str or iterable
    @param tags: Run only plays tagged with these (or)
    @type tags: list
    @param context: The nose context where the playbook(s) will be executed
    @type context: instance
    """

    cfgifc = ContextHelper().get_config()
    LOG.debug('In run_playbooks(%s)...', playbook)

    # Really not liking how variables are called constants and how there are N
    # ways of assigning them.
    C.DEFAULT_ROLES_PATH = [os.path.expanduser('~/.ansible/roles'),
                            os.path.join(HERE, 'roles')]
    C.RETRY_FILES_ENABLED = False
    C.DEFAULT_HASH_BEHAVIOUR = 'merge'
    C.ANSIBLE_PIPELINING = True
    action_loader.add_directory(os.path.join(HERE, 'action_plugins'))
    lookup_loader.add_directory(os.path.join(HERE, 'lookup_plugins'))
    loader = DataLoader()
    inventory = InventoryManager(loader=loader, sources='/dev/null')
    variable_manager = VariableManager(loader, inventory)
    o = OptionsStrict(connection='smart', forks=10, become=None,
                      become_method=None, become_user=None, check=False,
                      listhosts=False, listtasks=False, listtags=False,
                      syntax=False, module_path=[os.path.join(HERE, 'library')],
                      diff=False,  # tags=tags,
                      verbosity=1, timeout=1,
                      )

    if options.logger_level:
        display.logger_level = options.pop('logger_level')

    if options:
        o.update(options)

    passwords = dict(vault_pass='******')
    display.verbosity = o.verbosity

    inventory.add_group('all')
    a = inventory.groups['all']
    a.set_variable(VAR_F5TEST_CONFIG, cfgifc.api)
    a.set_variable('f5test_itemd', {})
    if context:
        tmp = nose_selector(context)
        address = test_address(context)
        a.set_variable('f5test_module', tmp.replace(':', '.'))
        # ITE compatibility: templates can refer to metadata values (e.g. TCID)
        if hasattr(context, ITE_METADATA):
            a.set_variable('f5test_itemd', getattr(context, ITE_METADATA))
        if address[1]:
            name = address[1].rsplit('.')[-1]
            a.set_variable('f5test_module_name', name)
    a.set_variable('playbook_name', os.path.splitext(os.path.basename(playbook))[0])
    for device in cfgifc.get_devices(KIND_ANY):
        prev = a
        name = ''
        for sub_kind in device.kind.bits:
            name += sub_kind
            inventory.add_group(name)
            prev.add_child_group(inventory.groups[name])
            prev = inventory.groups[name]
            name += '.'

        fingerprint = cfgifc.get_session().get_fingerprint(hash=True)
        for tag in tags:
            a.set_variable(tag, True)
        session = cfgifc.get_session()
        a.set_variable('f5test_session', OptionsStrict(name=session.name,
                                                       name_md5=session.name_md5))
        a.set_variable('f5test_respools', session.get_respool_handler().pools)
        a.set_variable('f5test_utils', JinjaUtils())
        a.set_variable('f5test_ranges', session.get_respool_handler().ranges)
        a.set_variable('machine_fingerprint', fingerprint)
        # Colon must mean something for Ansible
        if device.alias != 'localhost':
            inventory.add_host(device.alias, str(device.kind).replace(':', '.'))
        h = inventory.get_host(device.alias)
        h.set_variable('f5test_device', device)
        h.set_variable('f5test_kind', device.kind)
        h.set_variable('f5test_mgmt_address', device.get_address())
        h.set_variable('f5test_port_https', device.ports['https'])
        h.set_variable('f5test_username', device.get_admin_creds().username)
        h.set_variable('f5test_password', device.get_admin_creds().password)
        h.set_variable('ansible_host', device.get_discover_address())
        h.set_variable('ansible_ssh_port', device.ports['ssh'])
        h.set_variable('ansible_user', device.get_root_creds().username)
        h.set_variable('ansible_ssh_pass', device.get_root_creds().password)
        for spec, v in device.specs.get(HOST_VARS, {}).items():
            h.set_variable(spec, v)

        for group in device.groups:
            inventory.add_group(group)
            g = inventory.groups[group]
            a.add_child_group(g)
            g.add_host(h)

    names = [playbook] if isinstance(playbook, str) else playbook

    for g, v in cfgifc.api.get(GROUP_VARS, {}).items():
        group = inventory.groups.get(g)
        if group:
            group.vars.update(v)

    # Look for playbooks relative to caller's base directory
    frame = inspect.stack()[1]
    module = inspect.getmodule(frame[0])
    here = os.path.dirname(os.path.abspath(module.__file__))

    playbooks = [x if os.path.isabs(x) else os.path.join(here, FIXTURES_DIR, x)
                 for x in names]

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

    options = OptionsStrict(rc=-1, failed=[])
    cb = MyCallback(options=options)
    executor._tqm._callback_plugins.append(cb)
    try:
        options.rc = executor.run()
    finally:
        shutil.rmtree(C.DEFAULT_LOCAL_TMP, True)

    # p.vips.sync()
    return options
Exemplo n.º 50
0
    def run(self):

        super(PlaybookCLI, self).run()

        # Note: slightly wrong, this is written so that implicit localhost
        # Manage passwords
        sshpass = None
        becomepass = None
        passwords = {}

        if self.options.role_file:
            args = ["install"]
            if self.options.ignore_errors:
                args.append("--ignore-errors")

            force = ""
            if self.options.force:
                args.append("--force")

            if self.options.roles_path:
                args.extend(["--roles-path", self.options.roles_path])

            if self.options.no_deps:
                args.append("--no-deps")

            args.extend(["--role-file", self.options.role_file])
            gc = GalaxyCLI(args=args)
            gc.parse()
            gc.run()

        # initial error check, to make sure all specified playbooks are accessible
        # before we start running anything through the playbook executor
        for playbook in self.args:
            if not os.path.exists(playbook):
                raise AnsibleError("the playbook: %s could not be found" %
                                   playbook)
            if not (os.path.isfile(playbook)
                    or stat.S_ISFIFO(os.stat(playbook).st_mode)):
                raise AnsibleError(
                    "the playbook: %s does not appear to be a file" % playbook)

        # don't deal with privilege escalation or passwords when we don't need to
        if not self.options.listhosts and not self.options.listtasks and not self.options.listtags and not self.options.syntax:
            self.normalize_become_options()
            (sshpass, becomepass) = self.ask_passwords()
            passwords = {'conn_pass': sshpass, 'become_pass': becomepass}

        loader, inventory, variable_manager = self._play_prereqs(self.options)

        # (which is not returned in list_hosts()) is taken into account for
        # warning if inventory is empty.  But it can't be taken into account for
        # checking if limit doesn't match any hosts.  Instead we don't worry about
        # limit if only implicit localhost was in inventory to start with.
        #
        # Fix this when we rewrite inventory by making localhost a real host (and thus show up in list_hosts())
        no_hosts = False
        if len(inventory.list_hosts()) == 0:
            # Empty inventory
            display.warning(
                "provided hosts list is empty, only localhost is available")
            no_hosts = True
        inventory.subset(self.options.subset)
        if len(inventory.list_hosts()) == 0 and no_hosts is False:
            # Invalid limit
            raise AnsibleError("Specified --limit does not match any hosts")

        # flush fact cache if requested
        if self.options.flush_cache:
            self._flush_cache(inventory, variable_manager)

        # create the playbook executor, which manages running the plays via a task queue manager
        pbex = PlaybookExecutor(playbooks=self.args,
                                inventory=inventory,
                                variable_manager=variable_manager,
                                loader=loader,
                                options=self.options,
                                passwords=passwords)

        results = pbex.run()

        if isinstance(results, list):
            for p in results:

                display.display('\nplaybook: %s' % p['playbook'])
                for idx, play in enumerate(p['plays']):
                    if play._included_path is not None:
                        loader.set_basedir(play._included_path)
                    else:
                        pb_dir = os.path.realpath(
                            os.path.dirname(p['playbook']))
                        loader.set_basedir(pb_dir)

                    msg = "\n  play #%d (%s): %s" % (idx + 1, ','.join(
                        play.hosts), play.name)
                    mytags = set(play.tags)
                    msg += '\tTAGS: [%s]' % (','.join(mytags))

                    if self.options.listhosts:
                        playhosts = set(inventory.get_hosts(play.hosts))
                        msg += "\n    pattern: %s\n    hosts (%d):" % (
                            play.hosts, len(playhosts))
                        for host in playhosts:
                            msg += "\n      %s" % host

                    display.display(msg)

                    all_tags = set()
                    if self.options.listtags or self.options.listtasks:
                        taskmsg = ''
                        if self.options.listtasks:
                            taskmsg = '    tasks:\n'

                        def _process_block(b):
                            taskmsg = ''
                            for task in b.block:
                                if isinstance(task, Block):
                                    taskmsg += _process_block(task)
                                else:
                                    if task.action == 'meta':
                                        continue

                                    all_tags.update(task.tags)
                                    if self.options.listtasks:
                                        cur_tags = list(
                                            mytags.union(set(task.tags)))
                                        cur_tags.sort()
                                        if task.name:
                                            taskmsg += "      %s" % task.get_name(
                                            )
                                        else:
                                            taskmsg += "      %s" % task.action
                                        taskmsg += "\tTAGS: [%s]\n" % ', '.join(
                                            cur_tags)

                            return taskmsg

                        all_vars = variable_manager.get_vars(play=play)
                        play_context = PlayContext(play=play,
                                                   options=self.options)
                        for block in play.compile():
                            block = block.filter_tagged_tasks(
                                play_context, all_vars)
                            if not block.has_tasks():
                                continue
                            taskmsg += _process_block(block)

                        if self.options.listtags:
                            cur_tags = list(mytags.union(all_tags))
                            cur_tags.sort()
                            taskmsg += "      TASK TAGS: [%s]\n" % ', '.join(
                                cur_tags)

                        display.display(taskmsg)

            return 0
        else:
            return results
Exemplo n.º 51
0
    def test_get_serialized_batches(self):
        fake_loader = DictDataLoader({
            'no_serial.yml':
            '''
            - hosts: all
              gather_facts: no
              tasks:
              - debug: var=inventory_hostname
            ''',
            'serial_int.yml':
            '''
            - hosts: all
              gather_facts: no
              serial: 2
              tasks:
              - debug: var=inventory_hostname
            ''',
            'serial_pct.yml':
            '''
            - hosts: all
              gather_facts: no
              serial: 20%
              tasks:
              - debug: var=inventory_hostname
            ''',
            'serial_list.yml':
            '''
            - hosts: all
              gather_facts: no
              serial: [1, 2, 3]
              tasks:
              - debug: var=inventory_hostname
            ''',
            'serial_list_mixed.yml':
            '''
            - hosts: all
              gather_facts: no
              serial: [1, "20%", -1]
              tasks:
              - debug: var=inventory_hostname
            ''',
        })

        mock_inventory = MagicMock()
        mock_var_manager = MagicMock()

        # fake out options to use the syntax CLI switch, which will ensure
        # the PlaybookExecutor doesn't create a TaskQueueManager
        mock_options = MagicMock()
        mock_options.syntax.value = True

        templar = Templar(loader=fake_loader)

        pbe = PlaybookExecutor(
            playbooks=[
                'no_serial.yml', 'serial_int.yml', 'serial_pct.yml',
                'serial_list.yml', 'serial_list_mixed.yml'
            ],
            inventory=mock_inventory,
            variable_manager=mock_var_manager,
            loader=fake_loader,
            options=mock_options,
            passwords=[],
        )

        playbook = Playbook.load(pbe._playbooks[0],
                                 variable_manager=mock_var_manager,
                                 loader=fake_loader)
        play = playbook.get_plays()[0]
        play.post_validate(templar)
        mock_inventory.get_hosts.return_value = [
            'host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6',
            'host7', 'host8', 'host9'
        ]
        self.assertEqual(pbe._get_serialized_batches(play), [[
            'host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6',
            'host7', 'host8', 'host9'
        ]])

        playbook = Playbook.load(pbe._playbooks[1],
                                 variable_manager=mock_var_manager,
                                 loader=fake_loader)
        play = playbook.get_plays()[0]
        play.post_validate(templar)
        mock_inventory.get_hosts.return_value = [
            'host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6',
            'host7', 'host8', 'host9'
        ]
        self.assertEqual(
            pbe._get_serialized_batches(play),
            [['host0', 'host1'], ['host2', 'host3'], ['host4', 'host5'],
             ['host6', 'host7'], ['host8', 'host9']])

        playbook = Playbook.load(pbe._playbooks[2],
                                 variable_manager=mock_var_manager,
                                 loader=fake_loader)
        play = playbook.get_plays()[0]
        play.post_validate(templar)
        mock_inventory.get_hosts.return_value = [
            'host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6',
            'host7', 'host8', 'host9'
        ]
        self.assertEqual(
            pbe._get_serialized_batches(play),
            [['host0', 'host1'], ['host2', 'host3'], ['host4', 'host5'],
             ['host6', 'host7'], ['host8', 'host9']])

        playbook = Playbook.load(pbe._playbooks[3],
                                 variable_manager=mock_var_manager,
                                 loader=fake_loader)
        play = playbook.get_plays()[0]
        play.post_validate(templar)
        mock_inventory.get_hosts.return_value = [
            'host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6',
            'host7', 'host8', 'host9'
        ]
        self.assertEqual(
            pbe._get_serialized_batches(play),
            [['host0'], ['host1', 'host2'], ['host3', 'host4', 'host5'],
             ['host6', 'host7', 'host8'], ['host9']])

        playbook = Playbook.load(pbe._playbooks[4],
                                 variable_manager=mock_var_manager,
                                 loader=fake_loader)
        play = playbook.get_plays()[0]
        play.post_validate(templar)
        mock_inventory.get_hosts.return_value = [
            'host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6',
            'host7', 'host8', 'host9'
        ]
        self.assertEqual(
            pbe._get_serialized_batches(play),
            [['host0'], ['host1', 'host2'],
             ['host3', 'host4', 'host5', 'host6', 'host7', 'host8', 'host9']])

        # Test when serial percent is under 1.0
        playbook = Playbook.load(pbe._playbooks[2],
                                 variable_manager=mock_var_manager,
                                 loader=fake_loader)
        play = playbook.get_plays()[0]
        play.post_validate(templar)
        mock_inventory.get_hosts.return_value = ['host0', 'host1', 'host2']
        self.assertEqual(pbe._get_serialized_batches(play),
                         [['host0'], ['host1'], ['host2']])

        # Test when there is a remainder for serial as a percent
        playbook = Playbook.load(pbe._playbooks[2],
                                 variable_manager=mock_var_manager,
                                 loader=fake_loader)
        play = playbook.get_plays()[0]
        play.post_validate(templar)
        mock_inventory.get_hosts.return_value = [
            'host0', 'host1', 'host2', 'host3', 'host4', 'host5', 'host6',
            'host7', 'host8', 'host9', 'host10'
        ]
        self.assertEqual(
            pbe._get_serialized_batches(play),
            [['host0', 'host1'], ['host2', 'host3'], ['host4', 'host5'],
             ['host6', 'host7'], ['host8', 'host9'], ['host10']])
Exemplo n.º 52
0
class AnsibleRunner(object):
    def __init__(self,
                 vault_pass='',
                 connection='local',
                 module_path='',
                 forks=100,
                 become=None,
                 become_method=None,
                 become_user=None,
                 remote_user=None,
                 check=False,
                 listhosts=None,
                 listtasks=None,
                 listtags=None,
                 syntax=None,
                 private_key_file=None):
        """
        初始化ansible信息,主要是提供playbook_executor类使用的
        options, variable_manager, loader, passwords
        """
        Options = namedtuple('Options', [
            'connection', 'module_path', 'forks', 'become', 'become_method',
            'become_user', 'remote_user', 'check', 'listhosts', 'listtasks',
            'listtags', 'syntax', 'private_key_file'
        ])
        self.options = Options(connection=connection,
                               module_path=module_path,
                               forks=forks,
                               become=become,
                               become_method=become_method,
                               become_user=become_user,
                               remote_user=remote_user,
                               check=check,
                               listhosts=listhosts,
                               listtasks=listtasks,
                               listtags=listtags,
                               syntax=syntax,
                               private_key_file=private_key_file)
        self.variable_manager = VariableManager()
        self.loader = DataLoader()
        self.passwords = dict(vault_pass=vault_pass)

    def init_inventory(self, host_list='localhost'):
        """
        初始化inventory
        host_list接受json数据传递给inv_api的inv_file
        """
        host_list = inv_file(host_list)
        self.inventory = Inventory(loader=self.loader,
                                   variable_manager=self.variable_manager,
                                   host_list=host_list)
        os.remove(host_list)
        self.variable_manager.set_inventory(self.inventory)

    def init_playbook(self, playbooks):
        """
        使用playbook exexutor来直接执行playbook文件
        playbooks需要转换成list,否则无法被迭代,playbooks是一个文件或者路径
        """
        self.pbex = PlaybookExecutor(playbooks=[playbooks],
                                     inventory=self.inventory,
                                     variable_manager=self.variable_manager,
                                     loader=self.loader,
                                     options=self.options,
                                     passwords=self.passwords)

    def run_it(self):
        result = self.pbex.run()
        return result
Exemplo n.º 53
0
    def run(self):
        super(PlaybookDocutizer, self).run()

        for playbook in self.args:
            if not os.path.exists(playbook):
                raise AnsibleError("the playbook: %s could not be found" % playbook)
            if not (os.path.isfile(playbook)):
                raise AnsibleError("the playbook: %s does not appear to be a file" % playbook)

        self._shared_loader_obj = SharedPluginLoaderObj()

        self._loader, self._inventory, self._variable_manager = self._play_prereqs(self.options)

        pbex = PlaybookExecutor(playbooks=self.args, inventory=self._inventory, variable_manager=self._variable_manager, loader=self._loader, options=self.options, passwords={})
        results = pbex.run()

        if isinstance(results, list):
            for p in results:
                plays = []
                for idx, play in enumerate(p['plays']):
                    display.display('Processing play %d: %s' % (idx+1, play.name))

                    if play._included_path is not None:
                        self._loader.set_basedir(play._included_path)
                    else:
                        pb_dir = os.path.realpath(os.path.dirname(p['playbook']))
                        self._loader.set_basedir(pb_dir)

                    hosts = CLI.get_host_list(self._inventory, self.options.subset)
                    # TODO(iwalker): do we really need a host? can this just run with localhost?
                    if len(hosts) == 0:
                        raise AnsibleError('No hosts were specified')

                    # NOTE(iwalker): can loop through all hosts and evaluate the tasks, but since we are not doing any
                    # conditional evaluation, we'll just pick the first host and use that.
                    host = hosts[0]
                    display.v('Processing against host: %s' % (host.get_name()))

                    self._all_vars = self._variable_manager.get_vars(play=play, host=host)
                    play_context = PlayContext(play=play, options=self.options)

                    processed_blocks = []
                    for block in play.compile():
                        block = block.filter_tagged_tasks(play_context, self._all_vars)
                        if not block.has_tasks():
                            continue
                        processed_blocks.append(self._process_block(block))

                    tasks = []
                    for block in processed_blocks:
                        if len(block) == 0:
                            continue
                        for task in block:
                            tasks.append(task)

                    processed_handlers = []
                    for block in play.compile_roles_handlers():
                        processed_handlers.extend(self._process_block(block))

                    play_info = {
                        'filename': p['playbook'],
                        'hosts': hosts,
                        'name': play.name,
                        'roles': play.roles,
                        'tasks': tasks,
                        'handlers': processed_handlers,
                        'become': play.become,
                        'remote_user': play.remote_user,
                    }

                    plays.append(play_info)

                env = AnsibleEnvironment(trim_blocks=True,
                                         extensions=['jinja2.ext.loopcontrols'],
                                         loader=FileSystemLoader(self.options.template_path))

                display.display('Rendering template containing %d plays' % (len(plays)))
                template = env.get_template(self.options.template_master)
                output = template.render(plays=plays,
                                         options=self.options)

                display.display('Saving output to %s' % (self.options.output))
                with codecs.open(self.options.output, mode='w', encoding='utf-8') as f:
                    f.write(output)
Exemplo n.º 54
0
Options = namedtuple('Options',
                     ['connection', 'module_path', 'forks', 'become', 'become_method', 'become_user', 'check', 'diff',
                      "listhosts","listtasks","listtags", "syntax"])

options = Options(connection='smart', module_path=None, forks=10, become=None, become_method=None, become_user=None,
                  check=False, diff=False, listhosts=None,listtasks=None,listtags=None, syntax=None)

#  PlaybookExecutor 类

passwords = dict()

play_book = PlaybookExecutor(
    playbooks=["fbook1.yaml"],

    inventory=inventory,
    variable_manager=variable_manager,
    loader=loader,
    options=options,
    passwords=passwords
)

play_book.run()



"""
$ python ansible_api_k2.py

PLAY [192.168.193.139,192.168.193.140] ****************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************************
Exemplo n.º 55
0
    def run(self):

        super(PlaybookCLI, self).run()

        # Note: slightly wrong, this is written so that implicit localhost
        # Manage passwords
        sshpass = None
        becomepass = None
        vault_pass = None
        passwords = {}

        # initial error check, to make sure all specified playbooks are accessible
        # before we start running anything through the playbook executor
        for playbook in self.args:
            if not os.path.exists(playbook):
                raise AnsibleError("the playbook: %s could not be found" %
                                   playbook)
            if not (os.path.isfile(playbook)
                    or stat.S_ISFIFO(os.stat(playbook).st_mode)):
                raise AnsibleError(
                    "the playbook: %s does not appear to be a file" % playbook)

        # don't deal with privilege escalation or passwords when we don't need to
        if not self.options.listhosts and not self.options.listtasks and not self.options.listtags and not self.options.syntax:
            self.normalize_become_options()
            (sshpass, becomepass) = self.ask_passwords()
            passwords = {'conn_pass': sshpass, 'become_pass': becomepass}

        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=loader)
            loader.set_vault_password(vault_pass)
        elif self.options.ask_vault_pass:
            vault_pass = self.ask_vault_passwords()
            loader.set_vault_password(vault_pass)

        # create the variable manager, which will be shared throughout
        # the code, ensuring a consistent view of global variables
        variable_manager = VariableManager()
        variable_manager.extra_vars = load_extra_vars(loader=loader,
                                                      options=self.options)

        variable_manager.options_vars = load_options_vars(self.options)

        # create the inventory, and filter it based on the subset specified (if any)
        inventory = Inventory(loader=loader,
                              variable_manager=variable_manager,
                              host_list=self.options.inventory)
        variable_manager.set_inventory(inventory)

        # (which is not returned in list_hosts()) is taken into account for
        # warning if inventory is empty.  But it can't be taken into account for
        # checking if limit doesn't match any hosts.  Instead we don't worry about
        # limit if only implicit localhost was in inventory to start with.
        #
        # Fix this when we rewrite inventory by making localhost a real host (and thus show up in list_hosts())
        no_hosts = False
        if len(inventory.list_hosts()) == 0:
            # Empty inventory
            display.warning(
                "provided hosts list is empty, only localhost is available")
            no_hosts = True
        inventory.subset(self.options.subset)
        if len(inventory.list_hosts()) == 0 and no_hosts is False:
            # Invalid limit
            raise AnsibleError("Specified --limit does not match any hosts")

        # flush fact cache if requested
        if self.options.flush_cache:
            for host in inventory.list_hosts():
                variable_manager.clear_facts(host)

        # create the playbook executor, which manages running the plays via a task queue manager
        pbex = PlaybookExecutor(playbooks=self.args,
                                inventory=inventory,
                                variable_manager=variable_manager,
                                loader=loader,
                                options=self.options,
                                passwords=passwords)

        results = pbex.run()

        if isinstance(results, list):
            for p in results:

                display.display('\nplaybook: %s' % p['playbook'])
                for idx, play in enumerate(p['plays']):
                    msg = "\n  play #%d (%s): %s" % (idx + 1, ','.join(
                        play.hosts), play.name)
                    mytags = set(play.tags)
                    msg += '\tTAGS: [%s]' % (','.join(mytags))

                    if self.options.listhosts:
                        playhosts = set(inventory.get_hosts(play.hosts))
                        msg += "\n    pattern: %s\n    hosts (%d):" % (
                            play.hosts, len(playhosts))
                        for host in playhosts:
                            msg += "\n      %s" % host

                    display.display(msg)

                    all_tags = set()
                    if self.options.listtags or self.options.listtasks:
                        taskmsg = ''
                        if self.options.listtasks:
                            taskmsg = '    tasks:\n'

                        def _process_block(b):
                            taskmsg = ''
                            for task in b.block:
                                if isinstance(task, Block):
                                    taskmsg += _process_block(task)
                                else:
                                    if task.action == 'meta':
                                        continue

                                    all_tags.update(task.tags)
                                    if self.options.listtasks:
                                        cur_tags = list(
                                            mytags.union(set(task.tags)))
                                        cur_tags.sort()
                                        if task.name:
                                            taskmsg += "      %s" % task.get_name(
                                            )
                                        else:
                                            taskmsg += "      %s" % task.action
                                        taskmsg += "\tTAGS: [%s]\n" % ', '.join(
                                            cur_tags)

                            return taskmsg

                        all_vars = variable_manager.get_vars(loader=loader,
                                                             play=play)
                        play_context = PlayContext(play=play,
                                                   options=self.options)
                        for block in play.compile():
                            block = block.filter_tagged_tasks(
                                play_context, all_vars)
                            if not block.has_tasks():
                                continue
                            taskmsg += _process_block(block)

                        if self.options.listtags:
                            cur_tags = list(mytags.union(all_tags))
                            cur_tags.sort()
                            taskmsg += "      TASK TAGS: [%s]\n" % ', '.join(
                                cur_tags)

                        display.display(taskmsg)

            return 0
        else:
            return results
Exemplo n.º 56
0
def deleteData(request):
    request.COOKIES["username"] and request.session["username"]
    cookie_name = request.COOKIES["username"]
    if request.method == "GET" and request.GET:
        try:
            # if request.GET["user"]:
            user = request.GET["user"]
            sql = "select u.id,p.ip from Rsa_rsa as u,Hardware_hardware as p,Rsa_userpermission as up where up.user_id = u.id and up.host_id = p.id and u.user = '******';" % user
            ip_data = UserPermission.objects.raw(sql)
            ip_list = []  #定义一个用户来存放ip地址的list,因为可能一个用户对应多个ip地址,所以ip作为一个list来保存
            for data in ip_data:
                ip_list.append(str(data.ip))  #得到用户对应的ip地址的list
            user_obj = Rsa.objects.filter(user=user)
            for data in user_obj:
                id = int(data.id)
                pub = data.rsa_pub
            # user_obj.delete()
            up_obj = UserPermission.objects.filter(user_id=id)
            up_obj.delete()
            try:
                #调用ansible接口执行playbook,实现密钥的删除功能
                loader = DataLoader()  #用来加载解析yaml文件或JSON内容,并且支持vault的解密
                variable_manager = VariableManager(
                )  #管理变量的类,包括主机,组,扩展等变量,之前版本是在 inventory中的
                inventory = Inventory(
                    loader=loader,
                    variable_manager=variable_manager,
                    host_list=ip_list
                )  #根据inventory加载对应变量,这里host_list可以是文件也可以是IP列表
                variable_manager.set_inventory(inventory)
                passwords = dict(
                    conn_pass='')  #设置密码,必须是dict类型。如果ansible主机对服务器有密钥认证,则不需要密码
                Options = namedtuple('Options', [
                    'connection', 'remote_user', 'ask_sudo_pass', 'verbosity',
                    'ack_pass', 'module_path', 'forks', 'become',
                    'become_method', 'become_user', 'check', 'listhosts',
                    'listtasks', 'listtags', 'syntax', 'sudo_user', 'sudo'
                ])  #初始化需要的对象
                options = Options(connection='smart',
                                  remote_user='******',
                                  ack_pass=None,
                                  sudo_user='******',
                                  forks=50,
                                  sudo='yes',
                                  ask_sudo_pass=False,
                                  verbosity=5,
                                  module_path=None,
                                  become=True,
                                  become_method='sudo',
                                  become_user='******',
                                  check=None,
                                  listhosts=None,
                                  listtasks=None,
                                  listtags=None,
                                  syntax=None)
                extra_vars = {
                }  #额外的参数,key.yml以及模板中的参数,对应ansible-playbook key.yml --extra-vars pub=xxx state=present/absent
                extra_vars["pub_list"] = pub
                extra_vars["state"] = "absent"  #状态设置为移除
                variable_manager.extra_vars = extra_vars
                #playbooks填写yml文件路径,可以写多个,是个列表
                playbook = PlaybookExecutor(
                    playbooks=['/home/ansible/key.yml'],
                    inventory=inventory,
                    variable_manager=variable_manager,
                    loader=loader,
                    options=options,
                    passwords=passwords)
                playbook.run()  #执行
                state = "删除成功"
            except:
                state = "删除失败,请检查程序日志"
            return HttpResponseRedirect("/Rsa/userlist_dis")
        except:
            # else:
            # if request.GET["ip"]:
            ip = str(request.GET["ip"])
            sql = "select u.id,u.user,p.ip from Rsa_rsa as u,Hardware_hardware as p,Rsa_userpermission as up where up.user_id = u.id and up.host_id = p.id and p.ip = '%s';" % ip
            user_data = UserPermission.objects.raw(sql)
            user_list = [
            ]  #定义一个用户来存放用户名的list,因为可能一个ip地址对应多个用户,所以用户名作为一个list来保存
            for data in user_data:
                user_list.append(data.user)  #得到IP地址对应的用户名的list

            host_obj = Hardware.objects.filter(ip=ip)
            for data in host_obj:
                id = int(data.id)  #得到IP地址的id
            pub_list = []
            for user in user_list:
                user_obj = Rsa.objects.filter(user=user)
                for data in user_obj:
                    pub_list.append(data.rsa_pub)
            # host_obj.delete()
            up_obj = UserPermission.objects.filter(host_id=id)
            up_obj.delete()
            try:
                #调用ansible接口执行playbook,实现密钥的删除功能
                ip_list = [
                ]  #inventory中的host_list必须接受的是一个list,so我们把这个IP放到一个list中
                ip_list.append(ip)
                loader = DataLoader()  #用来加载解析yaml文件或JSON内容,并且支持vault的解密
                variable_manager = VariableManager(
                )  #管理变量的类,包括主机,组,扩展等变量,之前版本是在 inventory中的
                inventory = Inventory(
                    loader=loader,
                    variable_manager=variable_manager,
                    host_list=ip_list
                )  #根据inventory加载对应变量,这里host_list可以是文件也可以是IP列表
                variable_manager.set_inventory(inventory)
                passwords = dict(
                    conn_pass='')  #设置密码,必须是dict类型。如果ansible主机对服务器有密钥认证,则不需要密码
                Options = namedtuple('Options', [
                    'connection', 'remote_user', 'ask_sudo_pass', 'verbosity',
                    'ack_pass', 'module_path', 'forks', 'become',
                    'become_method', 'become_user', 'check', 'listhosts',
                    'listtasks', 'listtags', 'syntax', 'sudo_user', 'sudo'
                ])  #初始化需要的对象
                options = Options(connection='smart',
                                  remote_user='******',
                                  ack_pass=None,
                                  sudo_user='******',
                                  forks=50,
                                  sudo='yes',
                                  ask_sudo_pass=False,
                                  verbosity=5,
                                  module_path=None,
                                  become=True,
                                  become_method='sudo',
                                  become_user='******',
                                  check=None,
                                  listhosts=None,
                                  listtasks=None,
                                  listtags=None,
                                  syntax=None)
                extra_vars = {
                }  #额外的参数,key.yml以及模板中的参数,对应ansible-playbook key.yml --extra-vars pub=xxx state=present/absent
                extra_vars["pub_list"] = pub_list
                extra_vars["state"] = "absent"  #状态设置为移除
                variable_manager.extra_vars = extra_vars
                #playbooks填写yml文件路径,可以写多个,是个列表
                playbook = PlaybookExecutor(
                    playbooks=['/home/ansible/key.yml'],
                    inventory=inventory,
                    variable_manager=variable_manager,
                    loader=loader,
                    options=options,
                    passwords=passwords)
                playbook.run()  #执行
                state = "删除成功"
            except:
                state = "删除失败,请检查程序日志"
            return HttpResponseRedirect("/Rsa/hostlist_dis")
Exemplo n.º 57
0
])

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 = dict(vault_pass='******')

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

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

executor.run()
Exemplo n.º 58
0
 def playbook_all(self, playbook_name, tags_set, extra_vars_dict,
                  keyfile_path):
     ret = -1
     playbook_path = self._preinstall_playbook_path_builder(playbook_name)
     r_print.debug(playbook_path)
     inventry_path = '{work_dir}/.inventry'.format(
         work_dir=rdbox.config.get("ansible", "work_dir"))
     try:
         loader = DataLoader()
         self._create_inventry_file_from_k8s(inventry_path)
         inventory = InventoryManager(loader=loader, sources=inventry_path)
         variable_manager = VariableManager(loader=loader,
                                            inventory=inventory)
         variable_manager.extra_vars = extra_vars_dict
         if not os.path.exists(playbook_path):
             raise FileNotFoundError(errno.ENOENT,
                                     os.strerror(errno.ENOENT),
                                     playbook_path)
         Options = namedtuple('Options', [
             'listtags', 'listtasks', 'listhosts', 'syntax', 'connection',
             'module_path', 'forks', 'remote_user', 'private_key_file',
             'ssh_common_args', 'ssh_extra_args', 'sftp_extra_args',
             'scp_extra_args', 'become', 'become_method', 'become_user',
             'verbosity', 'check', 'diff', 'tags'
         ])
         options = Options(listtags=False,
                           listtasks=False,
                           listhosts=False,
                           syntax=False,
                           connection='smart',
                           module_path=None,
                           forks=5,
                           remote_user=None,
                           private_key_file=keyfile_path,
                           ssh_common_args=None,
                           ssh_extra_args=None,
                           sftp_extra_args=None,
                           scp_extra_args=None,
                           become=True,
                           become_method='sudo',
                           become_user='******',
                           verbosity=None,
                           check=False,
                           diff=False,
                           tags=tags_set)
         passwords = {}
         pbex = PlaybookExecutor(playbooks=[playbook_path],
                                 inventory=inventory,
                                 variable_manager=variable_manager,
                                 loader=loader,
                                 options=options,
                                 passwords=passwords)
         # redirect
         bak_stdout = sys.stdout
         bak_stderr = sys.stderr
         sys.stdout = StdLoggerWriter(r_print.debug)
         sys.stderr = StdLoggerWriter(r_print.info)
         ret = pbex.run()
     except Exception:
         sys.stdout = bak_stdout
         sys.stderr = bak_stderr
         import traceback
         r_print.error(traceback.format_exc())
         ret = -1
     finally:
         sys.stdout = bak_stdout
         sys.stderr = bak_stderr
     # Cleanup
     try:
         self._remove_file(inventry_path)
     except Exception:
         # No problem
         pass
     return ret
Exemplo n.º 59
0
    'connection', 'remote_user', 'ask_sudo_pass', 'verbosity', 'ack_pass',
    'module_path', 'forks', 'become', 'become_method', 'become_user', 'check',
    'listhosts', 'listtasks', 'listtags', 'syntax', 'sudo_user', 'sudo', 'diff'
])
options = Options(connection='smart',
                  remote_user=None,
                  ack_pass=None,
                  sudo_user=None,
                  forks=5,
                  sudo=None,
                  ask_sudo_pass=False,
                  verbosity=5,
                  module_path=None,
                  become=None,
                  become_method=None,
                  become_user=None,
                  check=False,
                  diff=False,
                  listhosts=None,
                  listtasks=None,
                  listtags=None,
                  syntax=None)

playbook = PlaybookExecutor(playbooks=['/etc/ansible/web.yml'],
                            inventory=inventory,
                            variable_manager=variable_manager,
                            loader=loader,
                            options=options,
                            passwords=passwords)
playbook.run()
Exemplo n.º 60
0
            self.task_status[h] = {
                'ok': t['ok'],
                'changed': t['changed'],
                'unreachable': t['unreachable'],
                'skipped': t['skipped'],
                'failed': t['failed']
            }


callback = PlayBookResultsCollector()

passwords = dict()
playbook = PlaybookExecutor(
    playbooks=['f1.yml'],
    inventory=inventory,
    variable_manager=variable_manager,
    loader=loader,
    options=options,
    passwords=passwords,
)

playbook._tqm._stdout_callback = callback
playbook.run()

#print callback.host_ok.items()
result_raw = {
    'ok': {},
    'failed': {},
    'unreachable': {},
    'skipped': {},
    'status': {}
}