def cell(create, destroy, playbook, inventory, key_file, aws_config, with_freeipa): """Manage treadmill cell on AWS""" playbook_args = [ 'ansible-playbook', '-i', inventory, '-e', 'aws_config={}'.format(aws_config) + ' freeipa={}'.format(with_freeipa), ] if create: playbook_args.extend([ playbook or deploy_path_join('cell.yml'), '--key-file', key_file, ]) elif destroy: playbook_args.append(playbook or deploy_path_join('destroy-cell.yml')) else: return playbook_cli = PlaybookCLI(playbook_args) playbook_cli.parse() playbook_cli.run()
def main(): """Run playbook""" for flag in ('--check',): if flag not in sys.argv: sys.argv.append(flag) obj = PlaybookCLI(sys.argv) obj.parse() obj.run()
def run_pbook(self): """Run playbook in check mode with console-stdout suppressed""" for flag in ('--check',): if flag not in self.ansible_pbook_args: self.ansible_pbook_args.append(flag) obj = PlaybookCLI(self.ansible_pbook_args) obj.parse() obj.run()
def runPlaybook(state, pbook): try: playbookCLI = PlaybookCLI( ["ansible-playbook", "-l", state.limit, pbook]) playbookCLI.parse() playbookCLI.run() except AnsibleError as e: click.echo(e)
def node(create, playbook, inventory, key_file, aws_config): """Manage treadmill node""" if create: playbook_cli = PlaybookCLI([ 'ansible-playbook', '-i', inventory, playbook, '--key-file', key_file, '-e', 'aws_config={}'.format(aws_config), ]) playbook_cli.parse() playbook_cli.run()
def _run_playbook(cli_args, vars_dict): """Runs ansible cli with vars dict :param vars_dict: dict, Will be passed as Ansible extra-vars :param cli_args: the list of command line arguments :return: ansible results """ # TODO(yfried): use ansible vars object instead of tmpfile # NOTE(oanufrii): !!!this import should be exactly here!!! # Ansible uses 'display' singleton from '__main__' and # gets it on module level. While we monkeypatching our # '__main__' in 'ansible_playbook' function import of # PlaybookCLI shoul be after that, to get patched # '__main__'. Otherwise ansible gets unpatched '__main__' # and creates new 'display' object with default (0) # verbosity. from ansible.cli.playbook import PlaybookCLI with tempfile.NamedTemporaryFile( prefix="ir-settings-", delete=True) as tmp: tmp.write(yaml.safe_dump(vars_dict, default_flow_style=False)) # make sure created file is readable. tmp.flush() cli_args.extend(['--extra-vars', "@" + tmp.name]) cli = PlaybookCLI(cli_args) LOG.debug('Starting ansible cli with args: {}'.format(cli_args[1:])) cli.parse() return cli.run()
def run(self): """Runs ansible CLI. Runs ansible CLI using PlaybookCLI and command line arguments stored in the self.cli_args list. Raises: CommandError: If there was error while running the command. Returns: int or list: return from the ansible PlaybookExecutor run() """ # Set the ansible.cfg that will be used by octario octario_ansible_config = self._get_ansible_config_file() if octario_ansible_config: os.environ['ANSIBLE_CONFIG'] = octario_ansible_config # Import must happen after setting ANSIBLE_CONFIG, otherwise # environment variable will not be used. from ansible.cli.playbook import PlaybookCLI ansible_cli = PlaybookCLI(self.cli_args) ansible_cli.parse() results = ansible_cli.run() if results: raise exceptions.CommandError("Failed to run tester: %s" % self.tester.get_playbook_path()) return results
def execute_playbooks(playbook_dir, host_file, extra_vars, host, logger=None, limit_playbooks=[]): # Import PlaybookCLI here because if it is imported before reading ansible.cfg, # ansible will try to create a tempdir that it does not have permission for. from ansible.cli.playbook import PlaybookCLI # Force requirement of a logger for 2.0 playbook runs if not logger: logger = deploy_logger inventory_dir = "%s/ansible" % settings.ANSIBLE_ROOT # Run playbooks results = [] for pb in limit_playbooks: logger.info("Executing playbook %s/%s" % (playbook_dir, pb)) args = [ "--inventory=%s" % inventory_dir, "--limit=%s" % host, "--extra-vars=%s" % json.dumps(extra_vars), "%s/%s" % (playbook_dir, pb) ] pb_runner = PlaybookCLI(args) pb_runner.parse() results.append(pb_runner.run()) if results[-1] != 0: break return results
def _run_playbook(cli_args, vars_dict): """Runs ansible cli with vars dict :param vars_dict: dict, Will be passed as Ansible extra-vars :param cli_args: the list of command line arguments :return: ansible results """ # TODO(yfried): use ansible vars object instead of tmpfile # NOTE(oanufrii): !!!this import should be exactly here!!! # Ansible uses 'display' singleton from '__main__' and # gets it on module level. While we monkeypatching our # '__main__' in 'ansible_playbook' function import of # PlaybookCLI shoul be after that, to get patched # '__main__'. Otherwise ansible gets unpatched '__main__' # and creates new 'display' object with default (0) # verbosity. from ansible.cli.playbook import PlaybookCLI with tempfile.NamedTemporaryFile(prefix="ir-settings-", delete=True) as tmp: tmp.write(yaml.safe_dump(vars_dict, default_flow_style=False)) # make sure created file is readable. tmp.flush() cli_args.extend(['--extra-vars', "@" + tmp.name]) cli = PlaybookCLI(cli_args) LOG.debug('Starting ansible cli with args: {}'.format(cli_args[1:])) cli.parse() return cli.run()
def run_playbook(module, extra_vars=None, extra_args=None): # Assemble parameters for playbook call path = 'test/test_playbooks/{}.yml'.format(module) playbook_opts = ['ansible-playbook', path] if extra_vars: playbook_opts.extend(['--extra-vars', extra_vars]) if extra_args: playbook_opts.extend(extra_args) cli = PlaybookCLI(playbook_opts) cli.parse() return cli.run()
def __run_playbook(inventory_path, playbook_path, key_path, tags, ansibleopts): vault_password_file = vault.VAULT_PASSWORD_FILENAME if os.path.exists(vault.VAULT_PLAIN_PASSWORD_FILENAME): vault_password_file = vault.VAULT_PLAIN_PASSWORD_FILENAME cmd = ['ansible-playbook', '-i', inventory_path, '--private-key', key_path, '--vault-password-file={}'.format(vault_password_file), playbook_path] if len(tags) > 0: cmd += ['--tags', ','.join(tags)] if ansibleopts is not None: cmd += ansibleopts.strip().split() cli = PlaybookCLI(cmd) cli.parse() return cli.run()
def apply_config(json_data): config_file = save_config_fille(json_data) app.logger.info('user config file: %s', config_file) args = [ ' ', '-i', '/inventory', '05_aci_deploy_app.yml', '-e', '@{}'.format(config_file) ] playbook = PlaybookCLI(args) exit_code = playbook.run() os.unlink(config_file) return exit_code == 0
def deploy(self): os.chdir(self.wd) # need to chdir before importing! from ansible.cli.playbook import PlaybookCLI cli = PlaybookCLI( [ sys.argv[0], '-i', 'inventory', '-f', '10', self.playbook ] ) cli.parse() exit = cli.run() os.chdir(self.pwd) return exit
def _run_playbook(cli_args, settings): """ Runs ansible cli. :param cli_args: the list of command line arguments :param settings: the settings dictionary :return: ansible results """ with tempfile.NamedTemporaryFile(prefix="ir-settings-", delete=True) as tmp: tmp.write(yaml.safe_dump(settings, default_flow_style=False)) # make sure ansible can read that file. tmp.flush() cli_args.extend(['--extra-vars', "@" + tmp.name]) cli = PlaybookCLI(cli_args) LOG.debug('Starting ansible cli with args: {}'.format(cli_args[1:])) cli.parse() return cli.run()
def _run_playbook(cli_args, settings): """ Runs ansible cli. :param cli_args: the list of command line arguments :param settings: the settings dictionary :return: ansible results """ with tempfile.NamedTemporaryFile( prefix="ir-settings-", delete=True) as tmp: tmp.write(yaml.safe_dump(settings, default_flow_style=False)) # make sure ansible can read that file. tmp.flush() cli_args.extend(['--extra-vars', "@" + tmp.name]) cli = PlaybookCLI(cli_args) LOG.debug('Starting ansible cli with args: {}'.format(cli_args[1:])) cli.parse() return cli.run()
def main(cliargs=None): data_path = resource_filename(__name__, 'data') packaging_playbooks_path = os.path.join(data_path, 'playbooks') cfg_path = os.path.join(data_path, 'ansible.cfg') if os.path.exists(cfg_path): os.environ["ANSIBLE_CONFIG"] = cfg_path # this needs to be global, as otherwise PlaybookCLI fails # to set the verbosity correctly from ansible.utils.display import Display global display display = Display() inventory_path = os.path.join(os.getcwd(), 'package_manifest.yaml') package_choices = find_packages(inventory_path) playbooks = find_playbooks(packaging_playbooks_path) parser = obal_argument_parser(playbooks.keys(), package_choices) args = parser.parse_args(cliargs) playbook_path = playbooks[args.action] if not os.path.exists(inventory_path): print("Could not find your package_manifest.yaml") exit(1) if not os.path.exists(playbook_path): print("Could not find the packaging playbooks") exit(1) from ansible.cli.playbook import PlaybookCLI ansible_args = generate_ansible_args(inventory_path, playbook_path, args) ansible_playbook = (["ansible-playbook"] + ansible_args) if args.verbose: print(ansible_playbook) cli = PlaybookCLI(ansible_playbook) cli.parse() exit_code = cli.run() sys.exit(exit_code)
def runPlaybooks(playbooks, _inventory, params=None, args=None, vault_secrets=None): inventoryArgs = ["-i", _inventory] if _inventory else [] args = ["ansible-playbook"] + inventoryArgs + (args or []) + playbooks logger.info("running " + " ".join(args)) cli = PlaybookCLI(args) context._init_global_context = _init_global_context cli.parse() # replace C.DEFAULT_STDOUT_CALLBACK with our own so we have control over logging # config/base.yml sets C.DEFAULT_STDOUT_CALLBACK == 'default' (ansible/plugins/callback/default.py) # (cli/console.py and cli/adhoc.py sets it to 'minimal' but PlaybookCLI.run() in cli/playbook.py uses the default) # see also https://github.com/projectatomic/atomic-host-tests/blob/master/callback_plugins/default.py resultsCB = ResultCallback() resultsCB.set_options() C.DEFAULT_STDOUT_CALLBACK = resultsCB _play_prereqs = cli._play_prereqs def hook_play_prereqs(): loader, inventory, variable_manager = _play_prereqs() if vault_secrets: loader.set_vault_secrets(vault_secrets) if params: variable_manager._extra_vars.update(params) resultsCB.inventoryManager = inventory resultsCB.variableManager = variable_manager return loader, inventory, variable_manager cli._play_prereqs = hook_play_prereqs oldVerbosity = display.verbosity if logging.getLogger("unfurl.ansible").getEffectiveLevel() <= 10: # debug display.verbosity = 2 try: resultsCB.exit_code = cli.run() finally: display.verbosity = oldVerbosity return resultsCB
def _run_playbook(cli_args, vars_dict): """Runs ansible cli with vars dict :param vars_dict: dict, Will be passed as Ansible extra-vars :param cli_args: the list of command line arguments :return: ansible results """ # TODO(yfried): use ansible vars object instead of tmpfile with tempfile.NamedTemporaryFile( prefix="ir-settings-", delete=True) as tmp: tmp.write(yaml.safe_dump(vars_dict, default_flow_style=False)) # make sure created file is readable. tmp.flush() cli_args.extend(['--extra-vars', "@" + tmp.name]) cli = PlaybookCLI(cli_args) LOG.debug('Starting ansible cli with args: {}'.format(cli_args[1:])) cli.parse() return cli.run()
def main(cliargs=None, application_config=ApplicationConfig): # pylint: disable=R0914 """ Main command """ cfg_path = application_config.ansible_config_path() if os.path.exists(cfg_path): os.environ["ANSIBLE_CONFIG"] = cfg_path # this needs to be global, as otherwise PlaybookCLI fails # to set the verbosity correctly from ansible.utils.display import Display # pylint: disable=all global display # pylint: disable=C0103,W0603 display = Display() inventory_path = application_config.inventory_path() targets = find_targets(inventory_path) parser = obsah_argument_parser(application_config, targets=targets) args = parser.parse_args(cliargs) if args.playbook.takes_target_parameter and not os.path.exists( inventory_path): print("Could not find your inventory at {}".format(inventory_path)) sys.exit(1) from ansible.cli.playbook import PlaybookCLI # pylint: disable=all ansible_args = generate_ansible_args(inventory_path, args, parser.obsah_arguments) ansible_playbook = (["ansible-playbook"] + ansible_args) if args.verbose: print("ANSIBLE_CONFIG={}".format(os.environ["ANSIBLE_CONFIG"]), ' '.join(ansible_playbook)) cli = PlaybookCLI(ansible_playbook) cli.parse() exit_code = cli.run() sys.exit(exit_code)
def main(cliargs=None): # pylint: disable=R0914 """ Main command """ cfg_path = get_ansible_config_path() if os.path.exists(cfg_path): os.environ["ANSIBLE_CONFIG"] = cfg_path # this needs to be global, as otherwise PlaybookCLI fails # to set the verbosity correctly from ansible.utils.display import Display global display # pylint: disable=C0103,W0603 display = Display() inventory_path = os.path.join(os.getcwd(), 'package_manifest.yaml') package_choices = find_packages(inventory_path) playbooks = find_playbooks(get_playbooks_path()) parser = obal_argument_parser(playbooks, package_choices) args = parser.parse_args(cliargs) if args.playbook.takes_package_parameter and not os.path.exists(inventory_path): print("Could not find your package_manifest.yaml") exit(1) from ansible.cli.playbook import PlaybookCLI ansible_args = generate_ansible_args(inventory_path, args) ansible_playbook = (["ansible-playbook"] + ansible_args) if args.verbose: print(ansible_playbook) cli = PlaybookCLI(ansible_playbook) cli.parse() exit_code = cli.run() sys.exit(exit_code)
def run_playbook(args): pb_cli = PlaybookCLI(args) pb_cli.parse() return pb_cli.run()
def _run_playbook(cli_args, vars_dict, ir_workspace, ir_plugin): """Runs ansible cli with vars dict :param vars_dict: dict, Will be passed as Ansible extra-vars :param cli_args: the list of command line arguments :param ir_workspace: An Infrared Workspace object represents the active workspace :param ir_plugin: An InfraredPlugin object of the current plugin :return: ansible results """ # TODO(yfried): use ansible vars object instead of tmpfile # NOTE(oanufrii): !!!this import should be exactly here!!! # Ansible uses 'display' singleton from '__main__' and # gets it on module level. While we monkeypatching our # '__main__' in 'ansible_playbook' function import of # PlaybookCLI shoul be after that, to get patched # '__main__'. Otherwise ansible gets unpatched '__main__' # and creates new 'display' object with default (0) # verbosity. from ansible.cli.playbook import PlaybookCLI from ansible.errors import AnsibleOptionsError from ansible.errors import AnsibleParserError with tempfile.NamedTemporaryFile(mode='w+', prefix="ir-settings-", delete=True) as tmp: tmp.write(yaml.safe_dump(vars_dict, default_flow_style=False)) # make sure created file is readable. tmp.flush() cli_args.extend(['--extra-vars', "@" + tmp.name]) cli = PlaybookCLI(cli_args) LOG.debug('Starting ansible cli with args: {}'.format(cli_args[1:])) try: cli.parse() stdout = not bool( strtobool(os.environ.get('IR_ANSIBLE_NO_STDOUT', 'no'))) stderr = not bool( strtobool(os.environ.get('IR_ANSIBLE_NO_STDERR', 'no'))) ansible_outputs_dir = \ os.path.join(ir_workspace.path, 'ansible_outputs') ansible_vars_dir = \ os.path.join(ir_workspace.path, 'ansible_vars') timestamp = datetime.utcnow().strftime("%Y-%m-%d_%H-%M-%S.%f") filename_template = \ "ir_{timestamp}_{plugin_name}{postfix}.{file_ext}" for _dir in (ansible_outputs_dir, ansible_vars_dir): try: os.makedirs(_dir) except OSError as e: if e.errno != errno.EEXIST: raise if bool(strtobool(os.environ.get('IR_GEN_VARS_JSON', 'no'))): filename = filename_template.format(timestamp=timestamp, plugin_name=ir_plugin.name, postfix='', file_ext='json') vars_file = os.path.join(ansible_vars_dir, filename) with open(vars_file, 'w') as fp: json.dump(vars_dict, fp, indent=4, sort_keys=True) with IRSTDFDManager(stdout=stdout, stderr=stderr) as fd_manager: if bool( strtobool(os.environ.get('IR_ANSIBLE_LOG_OUTPUT', 'no'))): filename = filename_template.format( timestamp=timestamp, plugin_name=ir_plugin.name, postfix='', file_ext='log') log_file = os.path.join(ansible_outputs_dir, filename) fd_manager.add(open(log_file, 'w')) if bool( strtobool( os.environ.get('IR_ANSIBLE_LOG_OUTPUT_NO_ANSI', 'no'))): filename = filename_template.format( timestamp=timestamp, plugin_name=ir_plugin.name, postfix='_no_ansi', file_ext='log') log_file = os.path.join(ansible_outputs_dir, filename) fd_manager.add(NoAnsiFile(open(log_file, 'w'))) # Return the result: # 0: Success # 1: "Error" # 2: Host failed # 3: Unreachable # 4: Parser Error # 5: Options error return cli.run() except (AnsibleParserError, AnsibleOptionsError) as error: LOG.error('{}: {}'.format(type(error), error)) raise error
def run_playbook(path, extra_vars=""): cli = PlaybookCLI(['ansible-playbook', path, "--extra-vars", extra_vars]) cli.parse() return cli.run()
def main(): inventory_path = os.path.join(os.getcwd(), 'package_manifest.yaml') package_choices = find_packages(inventory_path) parser = argparse.ArgumentParser() parser.add_argument('-e', '--extra-vars', dest="extra_vars", action="append", default=[], help="""set additional variables as key=value or YAML/JSON, if filename prepend with @""") parser.add_argument("-v", "--verbose", action="count", dest="verbose", help="verbose output") parser.add_argument("--start-at-task", action="store", dest="start_at_task", help="Start at a specific task") parser.add_argument("--step", action="store_true", dest="step", default=False, help="interactive: confirm each task before running") parser.add_argument("--list-tasks", action="store_true", dest="list_tasks", default=False, help="list tasks that will be run in the playbook") parser.add_argument("action", choices=_PLAYBOOKS.keys(), help="""which action to execute""") parser.add_argument('package', metavar='package', choices=package_choices, help="the package to build") if argcomplete: argcomplete.autocomplete(parser) args = parser.parse_args() packaging_playbooks_path = resource_filename(__name__, 'data') playbook = _PLAYBOOKS[args.action] playbook_path = os.path.join(packaging_playbooks_path, playbook) cfg_path = os.path.join(packaging_playbooks_path, 'ansible.cfg') if os.path.exists(cfg_path): os.environ["ANSIBLE_CONFIG"] = cfg_path if not os.path.exists(inventory_path): print("Could not find your package_manifest.yaml") exit(1) if not os.path.exists(playbook_path): print("Could not find the packaging playbooks") exit(1) from ansible.cli.playbook import PlaybookCLI ansible_args = [ playbook_path, '--inventory', inventory_path, '--limit', args.package ] for extra_var in args.extra_vars: ansible_args.extend(["-e", extra_var]) if args.verbose: ansible_args.append("-%s" % str("v" * args.verbose)) if args.start_at_task: ansible_args.append("--start-at-task") ansible_args.append(args.start_at_task) if args.step: ansible_args.append("--step") if args.list_tasks: ansible_args = ["--list-tasks"] ansible_playbook = (["ansible-playbook"] + ansible_args) if args.verbose: print(ansible_playbook) cli = PlaybookCLI(ansible_playbook) cli.parse() cli.run()
if not options.no_ansible_galaxy: print("[+] launching ansible-galaxy") default_opts = ["ansible-galaxy"] default_opts += ["install"] default_opts += ["-r", "ansible-requirements.yml"] default_opts += ["--force"] galaxy_cli = GalaxyCLI(default_opts) galaxy_cli.parse() exit_code = galaxy_cli.run() if exit_code: clean_and_exit(tmpdir, exit_code) if not options.no_ansible: print("[+] launching ansible-playbook") default_opts = ["ansible-playbook"] default_opts += ["-i", "default_groups"] default_opts += ["-i", inventory_file] default_opts += ["-e", "@{}".format(vars_file)] if '-u' not in options.ansible_args: default_opts += ["-u", "vagrant"] if options.offline: default_opts += [ "--module-path", "ansible_plugins/modules:offline/ansible_modules/offline"] ansible_args = default_opts + options.ansible_args play_cli = PlaybookCLI(ansible_args) play_cli.parse() exit_code = play_cli.run() clean_and_exit(tmpdir, exit_code)
print("[+] launching ansible-galaxy") default_opts = ["ansible-galaxy"] default_opts += ["install"] default_opts += ["-r", "ansible-requirements.yml"] default_opts += ["--force"] galaxy_cli = GalaxyCLI(default_opts) galaxy_cli.parse() exit_code = galaxy_cli.run() if exit_code: clean_and_exit(tmpdir, exit_code) if not options.no_ansible: print("[+] launching ansible-playbook") default_opts = ["ansible-playbook"] default_opts += ["-i", "default_groups"] default_opts += ["-i", inventory_file] default_opts += ["-e", "@{}".format(vars_file)] if '-u' not in options.ansible_args: default_opts += ["-u", "vagrant"] if options.offline: default_opts += [ "--module-path", "ansible_plugins/modules:offline/ansible_modules/offline" ] ansible_args = default_opts + options.ansible_args play_cli = PlaybookCLI(ansible_args) play_cli.parse() exit_code = play_cli.run() clean_and_exit(tmpdir, exit_code)