def setUpRole(cls, role_name, galaxy_args=None, skeleton_path=None): if galaxy_args is None: galaxy_args = [] if skeleton_path is not None: cls.role_skeleton_path = skeleton_path galaxy_args += ['--role-skeleton', skeleton_path] # Make temp directory for testing cls.test_dir = tempfile.mkdtemp() if not os.path.isdir(cls.test_dir): os.makedirs(cls.test_dir) cls.role_dir = os.path.join(cls.test_dir, role_name) cls.role_name = role_name # create role using default skeleton gc = GalaxyCLI(args=['ansible-galaxy', 'init', '-c', '--offline'] + galaxy_args + ['--init-path', cls.test_dir, cls.role_name]) gc.parse() gc.run() cls.gc = gc if skeleton_path is None: cls.role_skeleton_path = gc.galaxy.default_role_skeleton_path
def setUpClass(cls): '''creating prerequisites for installing a role; setUpClass occurs ONCE whereas setUp occurs with every method tested.''' # class data for easy viewing: role_dir, role_tar, role_name, role_req, role_path if os.path.exists("./delete_me"): shutil.rmtree("./delete_me") # creating framework for a role gc = GalaxyCLI(args=["init"]) with patch('sys.argv', ["-c", "--offline", "delete_me"]): gc.parse() gc.run() cls.role_dir = "./delete_me" cls.role_name = "delete_me" # making a temp dir for role installation cls.role_path = os.path.join(tempfile.mkdtemp(), "roles") if not os.path.isdir(cls.role_path): os.makedirs(cls.role_path) # creating a tar file name for class data cls.role_tar = './delete_me.tar.gz' cls.makeTar(cls.role_tar, cls.role_dir) # creating a temp file with installation requirements cls.role_req = './delete_me_requirements.yml' fd = open(cls.role_req, "w") fd.write("- 'src': '%s'\n 'name': '%s'\n 'path': '%s'" % (cls.role_tar, cls.role_name, cls.role_path)) fd.close()
def _install_roles_from_file(plugin_path): # Ansible Galaxy - install roles from file for req_file in ['requirements.yml', 'requirements.yaml']: galaxy_reqs_file = os.path.join(plugin_path, req_file) if not os.path.isfile(galaxy_reqs_file): continue LOG.debug( "Installing Ansible Galaxy " "requirements from file... ({})".format(galaxy_reqs_file)) from ansible.cli.galaxy import GalaxyCLI from ansible.errors import AnsibleError glxy_cli_params = [ 'ansible-galaxy', 'role', 'install', '-r', galaxy_reqs_file ] if InfraredPluginManager._is_collection_requirements( galaxy_reqs_file): glxy_cli_params[1] = 'collection' LOG.debug("Ansible Galaxy command: {}".format(glxy_cli_params)) glxy_cli = GalaxyCLI(glxy_cli_params) glxy_cli.parse() LOG.debug( "Trying to install Ansible Galaxy required " "collection 5 times to circumvent potential network issues") try: glxy_cli.run() except AnsibleError as e: raise IRGalaxyRoleInstallFailedException( "Failed to install Ansible Galaxy requirements, aborting! Error: {}" .format(e)) else: LOG.debug("Ansible Galaxy requirements files weren't found.")
def test_exit_without_ignore(self): ''' tests that GalaxyCLI exits with the error specified unless the --ignore-errors flag is used ''' gc = GalaxyCLI(args=["install"]) # testing without --ignore-errors flag with patch('sys.argv', ["-c", "fake_role_name"]): galaxy_parser = gc.parse() with patch.object(ansible.utils.display.Display, "display", return_value=None) as mocked_display: # testing that error expected is raised self.assertRaises(AnsibleError, gc.run) self.assertTrue( mocked_display.called_once_with( "- downloading role 'fake_role_name', owned by ")) # testing with --ignore-errors flag with patch('sys.argv', ["-c", "fake_role_name", "--ignore-errors"]): galalxy_parser = gc.parse() with patch.object(ansible.utils.display.Display, "display", return_value=None) as mocked_display: # testing that error expected is not raised with --ignore-errors flag in use gc.run() self.assertTrue( mocked_display.called_once_with( "- downloading role 'fake_role_name', owned by "))
def test_parse_search(self): ''' testing the options parswer when the action 'search' is given ''' gc = GalaxyCLI(args=["ansible-galaxy", "search"]) gc.parse() self.assertEqual(context.CLIARGS['platforms'], None) self.assertEqual(context.CLIARGS['galaxy_tags'], None) self.assertEqual(context.CLIARGS['author'], None)
def test_parse_setup(self): ''' testing the options parser when the action 'setup' is given ''' gc = GalaxyCLI(args=["ansible-galaxy", "setup", "source", "github_user", "github_repo", "secret"]) gc.parse() self.assertEqual(context.CLIARGS['verbosity'], 0) self.assertEqual(context.CLIARGS['remove_id'], None) self.assertEqual(context.CLIARGS['setup_list'], False)
def test_exit_without_ignore_with_flag(self): ''' tests that GalaxyCLI exits without the error specified if the --ignore-errors flag is used ''' # testing with --ignore-errors flag gc = GalaxyCLI(args=["ansible-galaxy", "install", "--server=None", "fake_role_name", "--ignore-errors"]) gc.parse() with patch.object(ansible.utils.display.Display, "display", return_value=None) as mocked_display: gc.run() self.assertTrue(mocked_display.called_once_with("- downloading role 'fake_role_name', owned by "))
def test_parse_import(self): ''' testing the options parser when the action 'import' is given ''' gc = GalaxyCLI(args=["ansible-galaxy", "import", "foo", "bar"]) gc.parse() self.assertEqual(context.CLIARGS['wait'], True) self.assertEqual(context.CLIARGS['reference'], None) self.assertEqual(context.CLIARGS['check_status'], False) self.assertEqual(context.CLIARGS['verbosity'], 0)
def test_exit_without_ignore_without_flag(self): ''' tests that GalaxyCLI exits with the error specified if the --ignore-errors flag is not used ''' gc = GalaxyCLI(args=["install", "--server=None", "fake_role_name"]) gc.parse() with patch.object(ansible.utils.display.Display, "display", return_value=None) as mocked_display: # testing that error expected is raised self.assertRaises(AnsibleError, gc.run) self.assertTrue(mocked_display.called_once_with("- downloading role 'fake_role_name', owned by "))
def test_parse_install(self): ''' testing the options parser when the action 'install' is given ''' gc = GalaxyCLI(args=["ansible-galaxy", "install"]) gc.parse() self.assertEqual(context.CLIARGS['ignore_errors'], False) self.assertEqual(context.CLIARGS['no_deps'], False) self.assertEqual(context.CLIARGS['role_file'], None) self.assertEqual(context.CLIARGS['force'], False)
def test_exit_without_ignore_with_flag(self): ''' tests that GalaxyCLI exits without the error specified if the --ignore-errors flag is used ''' # testing with --ignore-errors flag gc = GalaxyCLI(args=["install", "--server=None", "fake_role_name", "--ignore-errors"]) gc.parse() with patch.object(ansible.utils.display.Display, "display", return_value=None) as mocked_display: gc.run() self.assertTrue(mocked_display.called_once_with("- downloading role 'fake_role_name', owned by "))
def test_run(self): ''' verifies that the GalaxyCLI object's api is created and that execute() is called. ''' gc = GalaxyCLI(args=["ansible-galaxy", "install", "--ignore-errors", "imaginary_role"]) gc.parse() with patch.object(ansible.cli.CLI, "run", return_value=None) as mock_run: gc.run() # testing self.assertIsInstance(gc.galaxy, ansible.galaxy.Galaxy) self.assertEqual(mock_run.call_count, 1) self.assertTrue(isinstance(gc.api, ansible.galaxy.api.GalaxyAPI))
def test_run(self): ''' verifies that the GalaxyCLI object's api is created and that execute() is called. ''' gc = GalaxyCLI(args=["ansible-galaxy", "install", "--ignore-errors", "imaginary_role"]) gc.parse() with patch.object(ansible.cli.CLI, "execute", return_value=None) as mock_ex: with patch.object(ansible.cli.CLI, "run", return_value=None) as mock_run: gc.run() # testing self.assertEqual(mock_run.call_count, 1) self.assertTrue(isinstance(gc.api, ansible.galaxy.api.GalaxyAPI)) self.assertEqual(mock_ex.call_count, 1)
def test_exit_without_ignore_without_flag(self): ''' tests that GalaxyCLI exits with the error specified if the --ignore-errors flag is not used ''' gc = GalaxyCLI(args=[ "ansible-galaxy", "install", "--server=None", "fake_role_name" ]) gc.parse() with patch.object(ansible.utils.display.Display, "display", return_value=None) as mocked_display: # testing that error expected is raised self.assertRaises(AnsibleError, gc.run) self.assertTrue( mocked_display.called_once_with( "- downloading role 'fake_role_name', owned by "))
def test_execute_remove(self): # installing role gc = GalaxyCLI(args=["install", "--offline", "-p", self.role_path, "-r", self.role_req, '--force']) gc.parse() gc.run() # location where the role was installed role_file = os.path.join(self.role_path, self.role_name) # removing role gc = GalaxyCLI(args=["remove", "-p", role_file, self.role_name]) gc.parse() gc.run() # testing role was removed removed_role = not os.path.exists(role_file) self.assertTrue(removed_role)
def test_execute_remove(self): # installing role gc = GalaxyCLI(args=["ansible-galaxy", "install", "-p", self.role_path, "-r", self.role_req, '--force']) gc.parse() gc.run() # location where the role was installed role_file = os.path.join(self.role_path, self.role_name) # removing role gc = GalaxyCLI(args=["ansible-galaxy", "remove", role_file, self.role_name]) gc.parse() gc.run() # testing role was removed removed_role = not os.path.exists(role_file) self.assertTrue(removed_role)
def _install_roles_from_file(plugin_path): # Ansible Galaxy - install roles from file for req_file in ['requirements.yml', 'requirements.yaml']: galaxy_reqs_file = os.path.join(plugin_path, req_file) if not os.path.isfile(galaxy_reqs_file): continue LOG.debug("Installing Galaxy " "requirements... ({})".format(galaxy_reqs_file)) from ansible.cli.galaxy import GalaxyCLI from ansible.errors import AnsibleError glxy_cli = GalaxyCLI(['install']) glxy_cli.parse() glxy_cli.options.role_file = galaxy_reqs_file try: glxy_cli.execute_install() except AnsibleError as e: raise IRFailedToAddPlugin(e.message) else: LOG.debug("Galaxy requirements files weren't found.")
def test_exit_without_ignore(self): ''' tests that GalaxyCLI exits with the error specified unless the --ignore-errors flag is used ''' gc = GalaxyCLI(args=["install"]) # testing without --ignore-errors flag with patch('sys.argv', ["-c", "fake_role_name"]): galaxy_parser = gc.parse() with patch.object(ansible.utils.display.Display, "display", return_value=None) as mocked_display: # testing that error expected is raised self.assertRaises(AnsibleError, gc.run) self.assertTrue(mocked_display.called_once_with("- downloading role 'fake_role_name', owned by ")) # testing with --ignore-errors flag with patch('sys.argv', ["-c", "fake_role_name", "--ignore-errors"]): galalxy_parser = gc.parse() with patch.object(ansible.utils.display.Display, "display", return_value=None) as mocked_display: # testing that error expected is not raised with --ignore-errors flag in use gc.run() self.assertTrue(mocked_display.called_once_with("- downloading role 'fake_role_name', owned by "))
def test_execute_remove(self): # installing role gc = GalaxyCLI(args=["install", "--offline", "-p", self.role_path, "-r", self.role_req]) galaxy_parser = gc.parse() gc.run() # checking that installation worked role_file = os.path.join(self.role_path, self.role_name) self.assertTrue(os.path.exists(role_file)) # removing role gc = GalaxyCLI(args=["remove", "-c", "-p", self.role_path, self.role_name]) galaxy_parser = gc.parse() super(GalaxyCLI, gc).run() gc.api = ansible.galaxy.api.GalaxyAPI(gc.galaxy) completed_task = gc.execute_remove() # testing role was removed self.assertTrue(completed_task == 0) self.assertTrue(not os.path.exists(role_file))
def test_run(self): ''' verifies that the GalaxyCLI object's api is created and that execute() is called. ''' gc = GalaxyCLI(args=["install"]) with patch('sys.argv', ["-c", "-v", '--ignore-errors', 'imaginary_role']): galaxy_parser = gc.parse() with patch.object(ansible.cli.CLI, "execute", return_value=None) as mock_ex: with patch.object(ansible.cli.CLI, "run", return_value=None) as mock_run: gc.run() # testing self.assertEqual(mock_run.call_count, 1) self.assertTrue( isinstance(gc.api, ansible.galaxy.api.GalaxyAPI)) self.assertEqual(mock_ex.call_count, 1)
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
if (options.no_ansible_galaxy and options.no_ansible): ansible_vars_path = None inventory_path = None vars_file = conf.write_ansible_vars( dstname=ansible_vars_path) inventory_file = conf.write_ansible_inventory(dstname=inventory_path) exit_code = 0 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",
def install_ansible_requirements(): cmd = ['ansible-galaxy', 'install', '-r', 'ansible-requirements.yml'] cli = GalaxyCLI(cmd) sys.argv = cmd cli.parse() return cli.run()
def test_parse_login(self): ''' testing the options parser when the action 'login' is given ''' gc = GalaxyCLI(args=["ansible-galaxy", "login"]) gc.parse() self.assertEqual(context.CLIARGS['verbosity'], 0) self.assertEqual(context.CLIARGS['token'], None)
def test_parse_remove(self): ''' testing the options parser when the action 'remove' is given ''' gc = GalaxyCLI(args=["ansible-galaxy", "remove", "foo"]) gc.parse() self.assertEqual(context.CLIARGS['verbosity'], 0)
def test_parse_init(self): ''' testing the options parser when the action 'init' is given ''' gc = GalaxyCLI(args=["ansible-galaxy", "init", "foo"]) gc.parse() self.assertEqual(context.CLIARGS['offline'], False) self.assertEqual(context.CLIARGS['force'], False)
inventory_path = os.path.join(tmpdir, 'inventory') if (options.no_ansible_galaxy and options.no_ansible): ansible_vars_path = None inventory_path = None vars_file = conf.write_ansible_vars(dstname=ansible_vars_path) inventory_file = conf.write_ansible_inventory(dstname=inventory_path) exit_code = 0 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",