예제 #1
0
def connect_host(hosts,user,port,passwd,sudopasswd):
    #安装python-simplejson
    g_inv = ansible.inventory.Inventory(hosts)
    results = Runner(
         inventory=g_inv,
         module_name='raw', 
         module_args="yum -y install python-simplejson",
         pattern="all",
         sudo=True,
         remote_user=user,
         remote_port=port,
         remote_pass=passwd,
         sudo_pass=sudopasswd
    ).run()
    #由于,上个模式肯定运行失败,所以再次运行setup任务
    results = Runner(
         inventory=g_inv,
         module_name='setup', 
         module_args=" ",
         pattern="all",
         sudo=True,
         remote_user=user,
         remote_port=port,
         remote_pass=passwd,
         sudo_pass=sudopasswd
    ).run()

    ret={}
    if results is None:
       for host in hosts:
           ret[host]=(False,"No hosts found",{})
       return ret

    for host in hosts:
        ret[host]=(False,"unknow error",{})

    for (host, result) in results['contacted'].items():
        if not 'failed' in result:
            ret[host]=(True,"",get_info_from_result(result['ansible_facts']) )
        else:
            ret[host]=(False,result['msg'],{})
            print result
            
            
    for (host, result) in results['dark'].items():
        ret[host]=(False,result['msg'],{})

    return ret
예제 #2
0
def exec_command(request):
    ret = ''
    if request.method == 'POST':
        action = request.get_full_path().split('=')[1]
        if action == 'exec':
            target = request.POST.get('target')
            command = request.POST.get('command')
            tgtcheck = Host.objects.filter(name=target)
            argcheck = command not in DANGER_COMMAND
            if tgtcheck and argcheck:
                results = Runner(pattern=target,
                                 forks=10,
                                 module_name='command',
                                 module_args=command).run()
                if results.get('dark'):
                    ret = 'Failed. Please run again.'
                else:
                    ret = results.get('contacted')

            elif not tgtcheck:
                ret = 'No specific host.'
            elif not argcheck:
                ret = 'Please contact admistrator.'

    return render(request, 'exec_command.html', locals())
예제 #3
0
    def run(self, task, module_args, module_name="shell", timeout=10, forks=10, pattern='*', su_user=None):

        runner = Runner(
            module_name=module_name,
            module_args=module_args,
            inventory=self.inventory,
            pattern=pattern,
            forks=forks,
            timeout=timeout,
            su=su_user and True or False,
            su_user=su_user,
        )
        self.result_raw['celery_task_id'] = task
        tmp = runner.run()

        for (host, value) in tmp.get('contacted', {}).iteritems():
            if value.get('invocation', {}).get('module_name', '') != 'setup':
                if not self.result_raw.get(host):
                    self.result_raw[host] = {}
                self.result_raw[host]['result'] = value
        for (host, value) in tmp.get('dark', {}).iteritems():
            if not self.result_raw.get(host):
                    self.result_raw[host] = {}
            value['outcome'] = 'dark'
            self.result_raw[host]['result'] = value

        log_redis(self.result_raw)
        return self.result_raw
예제 #4
0
def ansible_cmd(pattern,module,args,forks):
    result = Runner(
            module_name = module,
            module_args = args,
            pattern = pattern,
            forks = forks).run()
    return result
예제 #5
0
    def run(self,
            command,
            module_name="command",
            timeout=10,
            forks=10,
            pattern=''):
        """
        run command from andible ad-hoc.
        command  : 必须是一个需要执行的命令字符串, 比如 
                 'uname -a'
        """
        data = {}

        if module_name not in ["raw", "command", "shell"]:
            raise CommandValueError(
                "module_name",
                "module_name must be of the 'raw, command, shell'")
        hoc = Runner(
            module_name=module_name,
            module_args=command,
            timeout=timeout,
            inventory=self.inventory,
            pattern=pattern,
            forks=forks,
        )
        self.results_raw = hoc.run()
예제 #6
0
파일: ezcommon.py 프로젝트: noca/ezdeploy
    def deploy(self, deploy_script=None):
        if deploy_script is None:
            if not os.path.isfile(self.git_dir + '/conf/deploy.sh'):
                logging.error("No deploy script")
                return False

            deploy_script = self.git_dir + '/conf/deploy.sh'

        arg = "{} {} {} {}".format(
            deploy_script,
            self.config['package_url'],
            self.deploy_dir,
            self.config['target_dir'],
            self.config['name'],
        )

        for s in self.config['servers']:
            results = Runner(
                pattern=s,
                module_name='script',
                module_args=arg,
            ).run()

            print results
            for (h, r) in results['contacted'].items():
                if r['rc'] != 0:
                    logging.error("{} deploy failed.".format(s))
                    return False

        return True
예제 #7
0
 def run(self,
         module_name='shell',
         module_args='',
         timeout=10,
         forks=10,
         pattern='*',
         become=False,
         become_method='sudo',
         become_user='******',
         become_pass='',
         transport='paramiko'):
     """
     run module from andible ad-hoc.
     module_name: ansible module_name
     module_args: ansible module args
     """
     hoc = Runner(module_name=module_name,
                  module_args=module_args,
                  timeout=timeout,
                  inventory=self.inventory,
                  pattern=pattern,
                  forks=forks,
                  become=become,
                  become_method=become_method,
                  become_user=become_user,
                  become_pass=become_pass,
                  transport=transport)
     self.results_raw = hoc.run()
     logging.debug(self.results_raw)
     return self.results_raw
예제 #8
0
    def run(self,
            module_arg,
            module_name="shell",
            complex_args=None,
            timeout=10,
            forks=10,
            pattern='*'):
        """
        run command from andible ad-hoc.

        Args:
            module_arg: ansible module argument
            complex_args: complex structure argument
            module_name: which module want to use, default use shell
            timeout: set runner api
            forks: see runner api
            pattern: set runner api
        """
        hoc = Runner(
            module_name=module_name,
            module_args=module_arg,
            complex_args=complex_args,
            timeout=timeout,
            inventory=self.inventory,
            pattern=pattern,
            forks=forks,
            module_path=C.DEFAULT_MODULE_PATH,
        )
        return_data = hoc.run()
        logger.info(return_data)
        return AnsibleResult(return_data)
예제 #9
0
 def configure_db(self):
     """
     Add the necessary tables to the default PostgreSQL server running on the
     host and prepare the necessary roles and databases.
     """
     # Update psql settings
     pg_conf = paths.P_PG_CONF
     lif = ["listen_addresses = '*'",
            "shared_buffers = 256MB",
            "wal_buffers = 8MB",
            "checkpoint_segments = 16",
            "checkpoint_completion_target = 0.9"]
     for l in lif:
         log.debug("Updating PostgreSQL conf file {0} setting: {1}".format(pg_conf, l))
         regexp = ' '.join(l.split(' ')[:2])
         try:
             Runner(inventory=Inventory(['localhost']),
                    transport='local',
                    become=True,
                    become_user='******',
                    module_name="lineinfile",
                    module_args=('dest={0} backup=yes line="{1}" owner=postgres regexp="{2}"'
                                 .format(pg_conf, l, regexp))
                    ).run()
         except Exception, e:
             log.error("Exception updating psql conf {0}: {1}".format(l, e))
예제 #10
0
파일: hosts.py 프로젝트: zhangyage/abck
def get_alias_info(pattern, remote_user):
    runner = Runner(module_name='setup',
                    module_args='',
                    pattern=pattern,
                    inventory=autils.get_inventory(g.mysql_db),
                    remote_user=remote_user)
    info = runner.run()

    results = {}

    if info['contacted']:
        if not info['contacted'][pattern].has_key('failed'):
            facts = info['contacted'][pattern]['ansible_facts']
            # Server info
            results['system_vendor'] = facts['ansible_system_vendor']
            results['product_name'] = facts['ansible_product_name']
            results['product_serial'] = facts['ansible_product_serial']
            # System info
            results['distribution'] = facts['ansible_distribution']
            results['distribution_version'] = facts[
                'ansible_distribution_version']
            results['architecture'] = facts['ansible_architecture']
            results['kernel'] = facts['ansible_kernel']
            # Cpu info
            results['processor_count'] = facts['ansible_processor_count']
            results['processor_cores'] = facts['ansible_processor_cores']
            results['processor_vcpus'] = facts['ansible_processor_vcpus']
            results['processor_threads_per_core'] = facts[
                'ansible_processor_threads_per_core']
            results['processor'] = set(facts['ansible_processor'])
            # Swap info
            results['swaptotal_mb'] = facts['ansible_swaptotal_mb']
            results['swapfree_mb'] = facts['ansible_swapfree_mb']
            # Mem info
            results['memtotal_mb'] = facts['ansible_memtotal_mb']
            results['memfree_mb'] = facts['ansible_memfree_mb']
            # Disk info
            results['devices'] = facts['ansible_devices']
            # Mount info
            results['mounts'] = facts['ansible_mounts']
            # Ip info
            results['default_ipv4'] = facts['ansible_default_ipv4']
            results['all_ipv4_addresses'] = facts['ansible_all_ipv4_addresses']
            # Software
            results['selinux'] = facts['ansible_selinux']
            results['python_version'] = facts['ansible_python_version']
        else:
            facts = info['contacted'][pattern]
            results['failed'] = facts['failed']
            results['msg'] = facts['msg']
    elif info['dark']:
        facts = info['dark'][pattern]
        results['failed'] = facts['failed']
        results['msg'] = facts['msg']
    else:
        results['failed'] = False
        results['msg'] = "host not found"

    return results
예제 #11
0
def ansible(**kwargs):
    results = Runner(**kwargs).run()
    rjson = json.dumps(results, indent=2)
    flash(rjson, 'ansible_results')
    # import pdb; pdb.set_trace()
    user = User.query.get(current_user.id)
    db.session.add(Log(user=user, log_info=rjson))
    return results
예제 #12
0
파일: builder.py 프로젝트: nos1609/copr
 def _create_ans_conn(self, username=None):
     ans_conn = Runner(remote_user=username or self.opts.build_user,
                       host_list=self.hostname + ",",
                       pattern=self.hostname,
                       forks=1,
                       transport=self.opts.ssh.transport,
                       timeout=self.timeout)
     return ans_conn
예제 #13
0
    def _run(self, *module_args, **complex_args):
        """Execute an ansible adhoc command returning the results in a AdHocResult object."""
        # Assemble module argument string
        if True:
            module_args = ' '.join(module_args)
        else:
            if module_args:
                complex_args.update(dict(_raw_params=' '.join(module_args)))

        # Assert hosts matching the provided pattern exist
        hosts = self.options['inventory_manager'].list_hosts()
        no_hosts = False
        if len(hosts) == 0:
            no_hosts = True
            warnings.warn(
                "provided hosts list is empty, only localhost is available")

        self.options['inventory_manager'].subset(self.options.get('subset'))
        hosts = self.options['inventory_manager'].list_hosts(
            self.options['host_pattern'])
        if len(hosts) == 0 and not no_hosts:
            raise ansible.errors.AnsibleError(
                "Specified hosts and/or --limit does not match any hosts")

        # Log the module and parameters
        log.debug("[%s] %s: %s" % (self.options['host_pattern'],
                                   self.options['module_name'], complex_args))

        # Build module runner object
        kwargs = dict(
            inventory=self.options.get('inventory_manager'),
            pattern=self.options.get('host_pattern'),
            module_name=self.options.get('module_name'),
            module_args=module_args,
            complex_args=complex_args,
            transport=self.options.get('connection'),
            remote_user=self.options.get('user'),
            module_path=self.options.get('module_path'),
            become=self.options.get('become'),
            become_method=self.options.get('become_method'),
            become_user=self.options.get('become_user'),
        )

        # Run the module
        runner = Runner(**kwargs)
        results = runner.run()

        # Log the results
        log.debug(results)

        if 'dark' in results and results['dark']:
            raise AnsibleConnectionFailure("Host unreachable",
                                           dark=results['dark'],
                                           contacted=results['contacted'])

        # Success!
        return AdHocResult(contacted=results['contacted'])
예제 #14
0
    def _run(self):
        self.start = datetime.now()
        self.save()

        # initial jobs
        for host in Inventory(ANSIBLE_INVENTORY).list_hosts(self.inventory):
            self.job_set.add(Job(host=host, cmd=self.cmd,
                                 start=datetime.now()))
        self.save()

        runner = Runner(module_name='shell',
                        module_args=self.cmd,
                        pattern=self.inventory,
                        sudo=self.sudo,
                        forks=ANSIBLE_FORKS,
                        host_list=ANSIBLE_INVENTORY)

        _, poller = runner.run_async(time_limit=WORKER_TIMEOUT)

        now = time.time()

        while True:

            if poller.completed or time.time(
            ) - now > WORKER_TIMEOUT:  # TIMEOUT
                break

            results = poller.poll()
            results = results.get('contacted')

            if results:
                for host, result in results.items():
                    job = self.job_set.get(host=host)
                    job.end = result.get('end')
                    job.rc = result.get('rc')
                    job.stdout = result.get('stdout')
                    job.stderr = result.get('stderr')
                    job.save()

            time.sleep(1)

        jobs_timeout = filter(lambda job: job.rc is None,
                              self.job_set.all())  # rc is None
        jobs_failed = filter(lambda job: job.rc, self.job_set.all())  # rc > 0

        for job in jobs_timeout:
            job.rc = 1
            job.stderr = 'JOB TIMEOUT'  # marked as 'TIMEOUT'
            job.save()

        self.rc = (jobs_timeout or jobs_failed) and 1 or 0

        self.end = datetime.now()
        self.use = datetime.now()
        self.save()

        self.done()
예제 #15
0
 def run(self):
     runner = Runner(
         module_name=self.module_name,
         module_args=self.module_args,
         pattern=self.pattern,
         forks=10
     )
     self.results_raw = runner.run()
     data = json.dumps(self.results_raw, indent=4)
     print data
예제 #16
0
파일: commands.py 프로젝트: href/suitable
    def execute(self, servers, arguments):
        runner_args = {
            'module_name': self.module_name,
            'module_args': arguments,
            'pattern': 'all',
            'host_list': ' '.join(servers)
        }

        runner = Runner(**runner_args)
        return runner.run()
예제 #17
0
def network_verify():
    heat_config_runner = Runner(host_list='hosts',
                                module_name='network_check',
                                remote_user='******',
                                become=True,
                                module_args='')
    run_result = heat_config_runner.run()
    heat_config = {}
    for k, v in run_result['contacted'].items():
        heat_config[k] = v['ansible_facts']
 def get_local_facts(self):
     """
     Loads the Ansible local facts in self.facts
     Calls the Ansible Python API v1 'setup' module
     """
     inventory = Inventory(["localhost"])
     runner = Runner(module_name='setup',
                     module_args='',
                     pattern='localhost',
                     inventory=inventory,
                     transport="local")
     result = runner.run()
     self.facts = result["contacted"]["localhost"]["ansible_facts"]
예제 #19
0
    def execute(self, ansible_mod, ansible_patt, ansible_args=None):
        ret = None

        if ansible_args:
            runner_obj = Runner(
                module_name=ansible_mod,
                module_args=ansible_args,
                host_list=self.ansible_host_list,
                pattern=ansible_patt,
                become=True,
                become_user='******',
            )
        else:
            runner_obj = Runner(
                module_name=ansible_mod,
                host_list=self.ansible_host_list,
                pattern=ansible_patt,
                become=True,
                become_user='******',
            )

        ret = runner_obj.run()
예제 #20
0
def show_template(host, path, gather_facts=True,
                  inventory_file=None, password_file=None,
                  user=None):
    inventory = get_inventory(inventory_file, password_file)
    setup_cache = get_gathered_facts(
            host, inventory, user) if gather_facts else {}
    # Show the template
    runner = Runner(
        inventory=inventory,
        setup_cache=setup_cache,
    )
    host_vars = runner.get_inject_vars(host)
    print template_from_file('.', path, host_vars)
예제 #21
0
파일: check.py 프로젝트: tedwardia/copr
def check_health(opts, vm_name, vm_ip):
    """
    Test connectivity to the VM

    :param vm_ip: ip address to the newly created VM
    :raises: :py:class:`~backend.exceptions.CoprWorkerSpawnFailError`: validation fails
    """
    # setproctitle("check VM: {}".format(vm_ip))

    log = get_redis_logger(opts, "vmm.check_health.detached", "vmm")

    runner_options = dict(remote_user=opts.build_user or "root",
                          host_list="{},".format(vm_ip),
                          pattern=vm_ip,
                          forks=1,
                          transport=opts.ssh.transport,
                          timeout=opts.vm_ssh_check_timeout)
    connection = Runner(**runner_options)
    connection.module_name = "shell"
    connection.module_args = "echo hello"

    result = {
        "vm_ip": vm_ip,
        "vm_name": vm_name,
        "msg": "",
        "result": "OK",
        "topic": EventTopics.HEALTH_CHECK
    }
    err_msg = None
    try:
        res = connection.run()
        if vm_ip not in res.get("contacted", {}):
            err_msg = ("VM is not responding to the testing playbook."
                       "Runner options: {}".format(runner_options) +
                       "Ansible raw response:\n{}".format(res))

    except Exception as error:
        err_msg = "Failed to check  VM ({})due to ansible error: {}".format(
            vm_ip, error)
        log.exception(err_msg)

    try:
        if err_msg:
            result["result"] = "failed"
            result["msg"] = err_msg
        rc = get_redis_connection(opts)
        rc.publish(PUBSUB_MB, json.dumps(result))
    except Exception as err:
        log.exception(
            "Failed to publish msg health check result: {} with error: {}".
            format(result, err))
예제 #22
0
 def run_ansible(self,
                 module_name='shell',
                 module_args='',
                 ip='',
                 keyfile='',
                 pattern='all',
                 hosts_file=None,
                 become=False):
     """
     run module use ansible ad-hoc.
     Args:
         module_name (string): ansible module name
         module_args (string): ansible module arg
         ip (string): destination ip, if ip more than one, use comma split, when set this, must specify keyfile
         keyfile (string): ssh private key file path
         pattern (string): specify hosts by pattern
         hosts_file (string): hosts file path
         become (bool): if true, will run command in remote instance use root.
                        if false, user as same as current instance user(ubuntu).
     """
     logger.debug('execute ansible module: %s, args: %s in instances: %s' %
                  (module_name, module_args, ip))
     if ip:
         hoc = Runner(
             module_name=module_name,
             module_args=module_args,
             host_list=ip.split(','),
             private_key_file=keyfile,
             become=become,
         )
     else:
         inventory = Inventory(hosts_file)
         hoc = Runner(module_name=module_name,
                      module_args=module_args,
                      inventory=inventory,
                      pattern=pattern)
     self.results_raw = hoc.run()
     logger.debug('ansible results: %s' % self.results_raw)
예제 #23
0
 def __init__(self,
              name,
              host_list,
              module_name,
              module_args,
              pattern='*',
              inject=None):
     super(AnsibleTask, self).__init__(name, inject=inject, provides=name)
     self.name = name
     self.host_list = host_list
     self.logger = logging.getLogger("AnsibleTask")
     self.runner = Runner(host_list=host_list,
                          pattern=pattern,
                          module_name=module_name,
                          module_args=module_args)
예제 #24
0
def exec_cmd( module_name, module_args, pattern, forks, debug, remote_port, region ):

    results = Runner(
                    module_name = module_name,
                    module_args = module_args,
                    pattern = pattern,
                    remote_user = '******',
                    remote_port = remote_port,
                    sudo = True,
                    sudo_user='******',
                    private_key_file = '/home/ec2-user/.ssh/awscn_sengled_release.pem'.replace('cn', region), 
                    host_list = os.path.join( os.path.dirname( os.path.abspath(__file__) ), 'hosts'), 
                    forks = forks,
                    ).run()

    return output_format(results, module_args, pattern, debug)
예제 #25
0
 def run(self, module_name='shell', module_args='', timeout=10, forks=2, pattern='*',
         become=False, become_method='sudo', become_user='******', become_pass='', transport='paramiko'):
     hoc = Runner(module_name=module_name,
                  module_args=module_args,
                  timeout=timeout,
                  inventory=self.inventory,
                  pattern=pattern,
                  forks=forks,
                  become=become,
                  become_method=become_method,
                  become_user=become_user,
                  become_pass=become_pass,
                  transport=transport
                  )
     self.results_raw = hoc.run()
     return self.results_raw
예제 #26
0
 def __init__(self,
              name,
              host_list,
              module_name,
              module_args,
              pattern='*',
              inject=None):
     super(AnsibleTask, self).__init__(
         name, inject=inject,
         provides=name)  #provides send out the results to engine.storage
     self.name = name
     self.host_list = host_list
     self.logger = logging.getLogger("RecoveryHandler:Base")
     self.runner = Runner(host_list=host_list,
                          pattern=pattern,
                          module_name=module_name,
                          module_args=module_args)
예제 #27
0
 def run_module(self, inventory, source, sshpass, sudopass):
     runner_cb = BattleschoolRunnerCallbacks()
     runner_cb.options = self.options
     runner_cb.options.module_name = self.module_name()
     module_args = self.module_args(source)
     #TODO: get workstation from options
     runner = Runner(
         pattern='workstation',
         module_name=self.module_name(),
         module_path=self.options.module_path,
         module_args=module_args,
         inventory=inventory,
         callbacks=runner_cb,
         timeout=self.options.timeout,
         transport=self.options.connection,
         #sudo=self.options.sudo,
         sudo=False,
         sudo_user=self.options.sudo_user,
         sudo_pass=sudopass,
         check=self.options.check,
         diff=self.options.diff,
         private_key_file=self.options.private_key_file,
         remote_user=self.options.remote_user,
         remote_pass=sshpass,
         forks=self.options.forks)
     try:
         results = runner.run()
         for result in results['contacted'].values():
             if 'failed' in result or result.get('rc', 0) != 0:
                 display("ERROR: failed source type (%s) '%s': %s" %
                         (self.type(), module_args, result['msg']),
                         stderr=True,
                         color='red')
                 sys.exit(2)
         if results['dark']:
             display("ERROR: failed source type (%s) '%s': DARK" %
                     (self.type(), module_args),
                     stderr=True,
                     color='red')
             sys.exit(2)
     except errors.AnsibleError, e:
         # Generic handler for ansible specific errors
         display("ERROR: %s" % str(e), stderr=True, color='red')
         sys.exit(1)
예제 #28
0
    def run(self, module_arg, module_name="shell", timeout=10, forks=10, pattern='*'):
        """
        run command from andible ad-hoc.

        Args:
            module_arg: ansible module argument
            module_name: which module want to use, default use shell
            timeout: set runner api
            forks: see runner api
            pattern: set runner api
        """
        hoc = Runner(module_name=module_name,
                     module_args=module_arg,
                     timeout=timeout,
                     inventory=self.inventory,
                     pattern=pattern,
                     forks=forks,
                     )
        self.results_raw = hoc.run()
        return AnsibleResult(self.results_raw)
예제 #29
0
파일: hosts.py 프로젝트: zhangyage/abck
def add_sshkey(alias):
    form = AddSSHKeyForm()
    if form.validate_on_submit():
        remote_user = form.remote_user.data
        remote_pass = form.remote_pass.data

        runner = Runner(module_name='authorized_key',
                        module_args={
                            'user': remote_user,
                            'key': open(app.config['ANSIBLE_KEY']).read()
                        },
                        pattern=alias,
                        inventory=autils.get_inventory(g.mysql_db),
                        remote_user=remote_user,
                        remote_pass=remote_pass)
        info = runner.run()
        return redirect(url_for('.index'))

    host = g.mysql_db.get('SELECT * FROM hosts WHERE alias=%s', alias)
    return render_template('add_sshkey.html', host=host, form=form)
예제 #30
0
def heat_config_check():
    heat_config_runner = Runner(host_list='hosts',
                                module_name='heat_config',
                                remote_user='******',
                                become=True,
                                module_args='')
    run_result = heat_config_runner.run()
    heat_config = {}
    for k, v in run_result['contacted'].items():
        heat_config[k] = v['ansible_facts']

    discoverd_data = get_discoverd_data()
    print '\n\nIronic discoverd data:'
    for hwid, data in discoverd_data.items():
        print hwid, data

    # NOTE(shadower): the entire heat_config is HUGE
    print '\n\nos-net-config input:'
    for ip, config in heat_config.items():
        print ip
        pprint.pprint(config['complete'].get('os_net_config', {}))
    pprint.pprint(heat_config)