def run_playbook(ansible_hosts, ansible_config, fqp, opts): try: if ansible_config: os.environ["ANSIBLE_CONFIG"] = ansible_config else: try: del os.environ["ANSIBLE_CONFIG"] except KeyError: pass if ansible_hosts: os.environ["ANSIBLE_HOSTS"] = ansible_hosts else: try: del os.environ["ANSIBLE_HOSTS"] except KeyError: pass import ansible_runner reload(ansible_runner) # Dropped support for observer_pretend - to be redone runner = ansible_runner.Runner(playbook=fqp, run_data=opts, host_file=ansible_hosts) stats, aresults = runner.run() except Exception, e: return { "stats": None, "aresults": None, "exception": traceback.format_exc() }
def run_playbook(ansible_hosts, ansible_config, fqp, opts, q): try: if ansible_config: os.environ["ANSIBLE_CONFIG"] = ansible_config else: try: del os.environ["ANSIBLE_CONFIG"] except KeyError: pass if ansible_hosts: os.environ["ANSIBLE_HOSTS"] = ansible_hosts else: try: del os.environ["ANSIBLE_HOSTS"] except KeyError: pass import ansible_runner reload(ansible_runner) # Dropped support for observer_pretend - to be redone runner = ansible_runner.Runner(playbook=fqp, run_data=opts, host_file=ansible_hosts) stats, aresults = runner.run() except Exception, e: logger.log_exc("Exception executing playbook", extra={'exception': str(e)}) stats = None aresults = None
def _run_ansible_runner(config_builder): runner = ansible_runner.Runner(config=config_builder.prepare()) LOGGER.debug(f'_run_ansible_runner: before run: {runner}') runner.run() LOGGER.debug(f'_run_ansible_runner: after run: {runner}') if runner.status != 'successful': raise AnsibleExecutionError(rc=runner.rc, stdout=runner.stdout.read()) return _find_result(runner.events)
def _run_ansible(config_builder): runner = ansible_runner.Runner(config=config_builder.prepare()) runner.run() if runner.status != 'successful': raise AnsibleExecutionError( rc=runner.rc, stdout=runner.stdout.read() ) return _find_result(runner.events)
def _run_ansible_runner(config_builder): runner = ansible_runner.Runner(config=config_builder.prepare()) LOGGER.debug(f'_run_ansible_runner: before run: {runner}') runner.run() LOGGER.debug(f'_run_ansible_runner: after run: {obj_info(runner)}') # Always collect results, so that we log them results = _find_result(runner.events) if runner.status != 'successful': raise AnsibleExecutionError(rc=runner.rc, stdout=runner.stdout.read()) return results
def get_all_host_fact(): write_inventory_from_db() rc = ansible_runner.RunnerConfig(private_data_dir=data_dir, module="setup", module_args='', host_pattern='all', quiet=True, forks=len(inventory_list), inventory=inventory_file, json_mode=True) rc.prepare() r = ansible_runner.Runner(config=rc) r.run() fact_list = [] for mystrs in inventory_list: for str in mystrs: myfact = r.get_fact_cache(str) fact_list.append({"ansible_name": str, "facts": myfact}) # print(fact_list) return fact_list
def _run(self, params, **kwargs): """ The actual entry point into the ansible_runner package. :param params: any arguments that ansible_runner.RunnerConfig() would accept (as a dict) :param kwargs: any arguments that ansible_runner.Runner() would accept :returns: ansible_runner.Runner object which holds info about the Ansible execution """ runner_config = ansible_runner.RunnerConfig(**params) runner_config.prepare() cmd = runner_config.generate_ansible_command() try: log.debug(f"Running the Ansible runner cmd='{cmd}'") runner = ansible_runner.Runner(runner_config, **kwargs) runner.run() except ansible_runner.exceptions.AnsibleRunnerException as e: raise ExecutionError(e) if runner.status != "successful": # Ansible runner 1.4.6-X does not expose the stderr property, so we # need to fallback to stdout instead # TODO: We'll be able to drop this code once ansible-runner 2.0 is # widely available if getattr(runner, "stderr", None) is not None: error = runner.stderr.read() else: error = runner.stdout.read() raise ExecutionError( f"Failed to execute Ansible command '{cmd}': {error}") return runner
def run_ansible(playbook_directory, project_directory, playbook, event_queue): """ Executes ansible-runner :param directory: string of directory (absolute or relative) to run ansible-runner :return: str of results """ params = { 'project_dir': playbook_directory, 'private_data_dir': playbook_directory, 'playbook': playbook, 'inventory': 'localhost', 'verbosity': 1, 'extravars': { 'project_directory': project_directory }, } rc = ansible_runner.runner_config.RunnerConfig(**params) rc.prepare() runner = ansible_runner.Runner(config=rc, event_handler=lambda x: event_queue.put(x)) runner.run() return 'OK'
def run(self, playbook, inventory, workdir, playbook_dir=None, connection='smart', output_callback=None, base_dir=constants.DEFAULT_VALIDATIONS_BASEDIR, ssh_user='******', key=None, module_path=None, limit_hosts=None, tags=None, skip_tags=None, verbosity=0, quiet=False, extra_vars=None, gathering_policy='smart', extra_env_variables=None, parallel_run=False, callback_whitelist=None, ansible_cfg=None, ansible_timeout=30, ansible_artifact_path=None, log_path=None, run_async=False, python_interpreter=None): """Execute one or multiple Ansible playbooks :param playbook: The Absolute path of the Ansible playbook :type playbook: ``string`` :param inventory: Either proper inventory file or a comma-separated list :type inventory: ``string`` :param workdir: The absolute path of the Ansible-runner artifacts directory :type workdir: ``string`` :param playbook_dir: The absolute path of the Validations playbooks directory :type playbook_dir: ``string`` :param connection: Connection type (local, smart, etc). (efaults to 'smart') :type connection: String :param output_callback: Callback for output format. Defaults to 'yaml'. :type output_callback: ``string`` :param base_dir: The absolute path of the default validations base directory :type base_dir: ``string`` :param ssh_user: User for the ssh connection (Defaults to 'root') :type ssh_user: ``string`` :param key: Private key to use for the ssh connection. :type key: ``string`` :param module_path: Location of the ansible module and library. :type module_path: ``string`` :param limit_hosts: Limit the execution to the hosts. :type limit_hosts: ``string`` :param tags: Run specific tags. :type tags: ``string`` :param skip_tags: Skip specific tags. :type skip_tags: ``string`` :param verbosity: Verbosity level for Ansible execution. :type verbosity: ``integer`` :param quiet: Disable all output (Defaults to False) :type quiet: ``boolean`` :param extra_vars: Set additional variables as a Dict or the absolute path of a JSON or YAML file type. :type extra_vars: Either a Dict or the absolute path of JSON or YAML :param gathering_policy: This setting controls the default policy of fact gathering ('smart', 'implicit', 'explicit'). (Defaults to 'smart') :type gathering_facts: ``string`` :param extra_env_vars: Set additional ansible variables using an extravar dictionary. :type extra_env_vars: ``dict`` :param parallel_run: Isolate playbook execution when playbooks are to be executed with multi-processing. :type parallel_run: ``boolean`` :param callback_whitelist: Comma separated list of callback plugins. Custom output_callback is also whitelisted. (Defaults to ``None``) :type callback_whitelist: ``string`` :param ansible_cfg: Path to an ansible configuration file. One will be generated in the artifact path if this option is None. :type ansible_cfg: ``string`` :param ansible_timeout: Timeout for ansible connections. (Defaults to ``30 minutes``) :type ansible_timeout: ``integer`` :param ansible_artifact_path: The Ansible artifact path :type ansible_artifact_path: ``string`` :param log_path: The absolute path of the validations logs directory :type log_path: ``string`` :param run_async: Enable the Ansible asynchronous mode (Defaults to 'False') :type run_async: ``boolean`` :param python_interpreter: Path to the Python interpreter to be used for module execution on remote targets, or an automatic discovery mode (``auto``, ``auto_silent`` or the default one ``auto_legacy``) :type python_interpreter: ``string`` :return: A ``tuple`` containing the the absolute path of the executed playbook, the return code and the status of the run :rtype: ``tuple`` """ if not playbook_dir: playbook_dir = workdir if not ansible_artifact_path: if log_path: ansible_artifact_path = "{}/artifacts/".format(log_path) else: ansible_artifact_path = \ constants.VALIDATION_ANSIBLE_ARTIFACT_PATH playbook = self._playbook_check(playbook, playbook_dir) self.log.info('Running Ansible playbook: {},' ' Working directory: {},' ' Playbook directory: {}'.format(playbook, workdir, playbook_dir)) # Get env variables: env = {} env = os.environ.copy() extravars = self._get_extra_vars(extra_vars) callback_whitelist, output_callback = self._callbacks( callback_whitelist, output_callback, extra_env_variables, env) # Set ansible environment variables env.update( self._ansible_env_var(output_callback, ssh_user, workdir, connection, gathering_policy, module_path, key, extra_env_variables, ansible_timeout, callback_whitelist, base_dir, python_interpreter)) if 'ANSIBLE_CONFIG' not in env and not ansible_cfg: ansible_cfg = os.path.join(ansible_artifact_path, 'ansible.cfg') config = configparser.ConfigParser() config.add_section('defaults') config.set('defaults', 'internal_poll_interval', '0.05') with open(ansible_cfg, 'w') as f: config.write(f) env['ANSIBLE_CONFIG'] = ansible_cfg elif 'ANSIBLE_CONFIG' not in env and ansible_cfg: env['ANSIBLE_CONFIG'] = ansible_cfg if log_path: env['VALIDATIONS_LOG_DIR'] = log_path envvars = self._encode_envvars(env=env) r_opts = { 'private_data_dir': workdir, 'inventory': self._inventory(inventory, ansible_artifact_path), 'playbook': playbook, 'verbosity': verbosity, 'quiet': quiet, 'extravars': extravars, 'artifact_dir': workdir, 'rotate_artifacts': 256, 'ident': '' } if not BACKWARD_COMPAT: r_opts.update({ 'envvars': envvars, 'project_dir': playbook_dir, 'fact_cache': ansible_artifact_path, 'fact_cache_type': 'jsonfile' }) else: parallel_run = False if skip_tags: r_opts['skip_tags'] = skip_tags if tags: r_opts['tags'] = tags if limit_hosts: r_opts['limit'] = limit_hosts if parallel_run: r_opts['directory_isolation_base_path'] = ansible_artifact_path runner_config = ansible_runner.runner_config.RunnerConfig(**r_opts) runner_config.prepare() runner_config.env['ANSIBLE_STDOUT_CALLBACK'] = \ envvars['ANSIBLE_STDOUT_CALLBACK'] if BACKWARD_COMPAT: runner_config.env.update(envvars) runner = ansible_runner.Runner(config=runner_config) if run_async: thr = threading.Thread(target=runner.run) thr.start() return playbook, runner.rc, runner.status status, rc = runner.run() return playbook, rc, status
def run(self, playbook, inventory, workdir, playbook_dir=None, connection='smart', output_callback='yaml', ssh_user='******', key=None, module_path=None, limit_hosts=None, tags=None, skip_tags=None, verbosity=0, quiet=False, extra_vars=None, gathering_policy='smart', extra_env_variables=None, parallel_run=False, callback_whitelist=None, ansible_cfg=None, ansible_timeout=30): if not playbook_dir: playbook_dir = workdir playbook = self._playbook_check(playbook, playbook_dir) self.log.info('Running Ansible playbook: {},' ' Working directory: {},' ' Playbook directory: {}'.format(playbook, workdir, playbook_dir)) ansible_fact_path = self._creates_ansible_fact_dir() extravars = self._get_extra_vars(extra_vars) callback_whitelist = self._callback_whitelist(callback_whitelist, output_callback) # Set ansible environment variables env = self._ansible_env_var(output_callback, ssh_user, workdir, connection, gathering_policy, module_path, key, extra_env_variables, ansible_timeout, callback_whitelist) command_path = None with utils.TempDirs( dir_path=constants.VALIDATION_RUN_LOG_PATH, chdir=False, ) as ansible_artifact_path: if 'ANSIBLE_CONFIG' not in env and not ansible_cfg: ansible_cfg = os.path.join(ansible_artifact_path, 'ansible.cfg') config = configparser.ConfigParser() config.add_section('defaults') config.set('defaults', 'internal_poll_interval', '0.05') with open(ansible_cfg, 'w') as f: config.write(f) env['ANSIBLE_CONFIG'] = ansible_cfg elif 'ANSIBLE_CONFIG' not in env and ansible_cfg: env['ANSIBLE_CONFIG'] = ansible_cfg r_opts = { 'private_data_dir': workdir, 'project_dir': playbook_dir, 'inventory': self._inventory(inventory, ansible_artifact_path), 'envvars': self._encode_envvars(env=env), 'playbook': playbook, 'verbosity': verbosity, 'quiet': quiet, 'extravars': extravars, 'fact_cache': ansible_fact_path, 'fact_cache_type': 'jsonfile', 'artifact_dir': ansible_artifact_path, 'rotate_artifacts': 256 } if skip_tags: r_opts['skip_tags'] = skip_tags if tags: r_opts['tags'] = tags if limit_hosts: r_opts['limit'] = limit_hosts if parallel_run: r_opts['directory_isolation_base_path'] = ansible_artifact_path runner_config = ansible_runner.runner_config.RunnerConfig(**r_opts) runner_config.prepare() # NOTE(cloudnull): overload the output callback after prepare # to define the specific format we want. # This is only required until PR # https://github.com/ansible/ansible-runner/pull/387 # is merged and released. After this PR has been # made available to us, this line should be removed. runner_config.env['ANSIBLE_STDOUT_CALLBACK'] = \ r_opts['envvars']['ANSIBLE_STDOUT_CALLBACK'] runner = ansible_runner.Runner(config=runner_config) status, rc = runner.run() return runner.stdout.name, playbook, rc, status
def execute(self, *args, **kwargs) -> list: if not ansible_runner: raise Exception("ansible-runner libraries are unavailable. Please " "install ansible-runner.") LOG.debug( "%s ansible execute - args: %s, kwargs: %s, working_dir: %s, hosts: %s, data; %s", self, args, kwargs, self.working_dir, self.hosts, self.data, ) LOG.info("%s | Running", self) # check if playbook is relative to working dir and exists, or # fall back to the provided playbook path and hope it exists playbook_path = os.path.join(self.working_dir, self.playbook) if not os.path.isfile(playbook_path): playbook_path = self.playbook # default to an inventory in working dir if not defined on the task inventory_path = os.path.join(self.working_dir, "inventory.yaml") if self.inventory is not None and os.path.exists( os.path.join(self.working_dir, self.inventory)): inventory_path = os.path.join(self.working_dir, self.inventory) elif not os.path.exists(inventory_path): inventory_path = None env = self._default_ansible_paths() cfg_path = os.path.join(self.working_dir, "ansible.cfg") if os.path.exists(cfg_path): env["ANSIBLE_CONFIG"] = cfg_path runner_opts = { "private_data_dir": self.working_dir, "project_dir": self.working_dir, "playbook": playbook_path, "envvars": env, } if inventory_path: runner_opts["inventory"] = inventory_path runner_opts.update(self.runner_options) runner_config = ansible_runner.runner_config.RunnerConfig( **runner_opts) runner_config.prepare() # TODO: the call back needs to be overridden here if you want something # other than the default. # runner_config.env["ANSIBLE_STDOUT_CALLBACK"] = "tripleo_dense" # By default ansible-runner forces a per-execution fact cache but we # want to share fact caches between our tasks. This can be disabled # at a task level by setting global_fact_cache to false if self.global_fact_cache: runner_config.env[ "ANSIBLE_CACHE_PLUGIN_CONNECTION"] = "~/.ansible/fact_cache" runner = ansible_runner.Runner(config=runner_config) status, rc = runner.run() data = {"stdout": runner.stdout, "stats": runner.stats} # https://ansible-runner.readthedocs.io/en/stable/python_interface.html#the-runner-object status = rc == 0 and status == "successful" if not status: raise ExecutionFailed( "{} | Ansible job execution failed. rc: {}, status {}".format( self, rc, status)) LOG.info("%s | Completed", self) return [TaskResult(status, data)]
# import paramiko import ansible_runner data_dir = "/root/flask-kubernetes/utils" rc = ansible_runner.RunnerConfig(private_data_dir=data_dir, module="setup", module_args='', host_pattern='all', forks=1, inventory="inventory.ini", json_mode=True) print(rc.generate_ansible_command()) rc.prepare() r = ansible_runner.Runner(config=rc) a = r.run() print("{}: {}".format(r.status, r.rc)) for each_host_event in r.events: print(each_host_event['stdout']) # for str in a: # print(a["ansible_processor_cores"]) # cpu = a['contacted']["10.147.19.123"]['ansible_facts']['ansible_processor'][1] # print(cpu) # r# = ansible_runner.runner.Runner(config=rc) # r.run() # # list = {"name": None, "percent": [0, ]} # #