def execute(self): """ Executes linting/integration tests and returns None. Flake8 performs the code linting. Testinfra executes integration tests. :return: None """ ansible = ansible_playbook.AnsiblePlaybook( self._molecule.config.config['ansible'], {}, _env=self._molecule.env) testinfra_options = config.merge_dicts( self._molecule.driver.testinfra_args, self._molecule.verifier_options) testinfra_options['ansible_env'] = ansible.env if self._molecule.args.get('debug'): testinfra_options['debug'] = True if self._molecule.args.get('sudo'): testinfra_options['sudo'] = True tests = self._get_tests() if len(tests) > 0: if 'flake8' not in self._molecule.disabled: self._flake8(tests) self._testinfra(tests, **testinfra_options)
def execute(self, exit=True): """ Execute the actions necessary to perform a `molecule syntax` and return a tuple. :param exit: (Unused) Provided to complete method signature. :return: Return a tuple provided by :meth:`.AnsiblePlaybook.execute`. """ debug = self.args.get('debug') self.molecule.create_templates() if 'requirements_file' in self.molecule.config.config[ 'ansible'] and not self.molecule.state.installed_deps: galaxy = ansible_galaxy.AnsibleGalaxy(self.molecule.config.config, debug=debug) galaxy.execute() self.molecule.state.change_state('installed_deps', True) ansible = ansible_playbook.AnsiblePlaybook( self.molecule.config.config['ansible'], {}, debug=debug) ansible.add_cli_arg('syntax-check', True) ansible.add_cli_arg('inventory_file', 'localhost,') util.print_info('Checking playbook\'s syntax ...') return ansible.execute(hide_errors=True)
def _get_ansible_instance(self): ac = self._molecule.config.config['ansible'] ac['playbook'] = self._playbook ansible = ansible_playbook.AnsiblePlaybook( ac, self._molecule.driver.ansible_connection_params) return ansible
def execute(self, exit=True): """ Execute the actions necessary to perform a `molecule check` and return a tuple. :param exit: (Unused) Provided to complete method signature. :return: Return a tuple provided by :meth:`.AnsiblePlaybook.execute`. """ if not self.molecule.state.created: msg = ('Instance(s) not created, `check` should be run ' 'against created instance(s).') util.print_error(msg) util.sysexit() debug = self.args.get('debug') ansible = ansible_playbook.AnsiblePlaybook( self.molecule.config.config['ansible'], self.molecule.driver.ansible_connection_params, debug=debug) ansible.add_cli_arg('check', True) util.print_info("Performing a 'Dry Run' of playbook...") return ansible.execute(hide_errors=True) return (None, None)
def test_ignores_host_group_vars(): a = ansible_playbook.AnsiblePlaybook({ 'host_vars': 'foo', 'group_vars': 'bar' }, {}) assert not a._cli.get('host_vars') assert not a._cli.get('group_vars')
def _get_ansible_instance(self): ac = self._molecule.config.config['ansible'] ac['playbook'] = self._playbook ansible = ansible_playbook.AnsiblePlaybook(ac) for k, v in self._molecule.driver.ansible_connection_params.items(): ansible.add_cli_arg(k, v) return ansible
def _get_ansible_instance(self): ac = self._molecule.config.config['ansible'] ac['playbook'] = self._playbook debug = self._molecule.args.get('debug') ansible = ansible_playbook.AnsiblePlaybook( ac, self._molecule.driver.ansible_connection_params, debug=debug) return ansible
def test_provision(docker_instance): docker_instance.up() pb = docker_instance.ansible_connection_params pb['playbook'] = 'playbook.yml' pb['inventory'] = 'test1,test2,' ansible = ansible_playbook.AnsiblePlaybook(pb) # TODO(retr0h): Understand why driver is None assert (None, '') == ansible.execute()
def test_provision(molecule_instance, docker_instance): molecule_instance.driver = docker_instance molecule_instance.config.config['ansible'].update( {'inventory': 'test1,test2,'}) docker_instance.up() ansible = ansible_playbook.AnsiblePlaybook( molecule_instance.config.config['ansible'], molecule_instance.driver.ansible_connection_params) assert (None, '') == ansible.execute()
def _get_ansible_playbook(self, playbook, **kwargs): """ Get an instance of AnsiblePlaybook and returns it. :param playbook: A string containing an absolute path to a provisioner's playbook. :param kwargs: An optional keyword arguments. :return: object """ return ansible_playbook.AnsiblePlaybook(self.inventory_file, playbook, self._config, **kwargs)
def test_inventory_generation(molecule_instance, docker_instance): molecule_instance._driver = docker_instance molecule_instance._driver.up() molecule_instance._create_inventory_file() pb = molecule_instance._driver.ansible_connection_params pb['playbook'] = 'playbook.yml' pb['inventory'] = 'test1,test2,' ansible = ansible_playbook.AnsiblePlaybook(pb) # TODO(retr0h): Understand why driver is None assert (None, '') == ansible.execute()
def execute(self, exit=True): ansible = ansible_playbook.AnsiblePlaybook( self._molecule.config.config['ansible'], _env=self._molecule._env) testinfra_options = util.merge_dicts( self._molecule._driver.testinfra_args, self._molecule.config.config['testinfra']) testinfra_options['env'] = ansible.env testinfra_options['debug'] = self._molecule._args.get('--debug', False) testinfra_options['sudo'] = self._molecule._args.get('--sudo', False) tests_glob = self._get_tests() if len(tests_glob) > 0: self._testinfra(tests_glob, **testinfra_options)
def execute(self, exit=True): self.molecule._create_templates() if 'requirements_file' in self.molecule.config.config[ 'ansible'] and not self.molecule._state.installed_deps: galaxy = ansible_galaxy.AnsibleGalaxy(self.molecule.config.config) galaxy.install() self.molecule._state.change_state('installed_deps', True) ansible = ansible_playbook.AnsiblePlaybook( self.molecule.config.config['ansible']) ansible.add_cli_arg('syntax-check', True) ansible.add_cli_arg('inventory_file', 'localhost,') util.print_info("Checking playbooks syntax ...") return ansible.execute(hide_errors=True)
def test_inventory_generation(molecule_instance, docker_instance): molecule_instance.driver = docker_instance molecule_instance.config.config['ansible'].update( {'inventory': 'test1,test2,'}) molecule_instance.driver.up() molecule_instance.create_inventory_file() ansible = ansible_playbook.AnsiblePlaybook( molecule_instance.config.config['ansible'], molecule_instance.driver.ansible_connection_params) for instance in molecule_instance.driver.instances: expected = '{} ansible_connection=docker\n'.format(instance['name']) assert expected == molecule_instance.driver.inventory_entry(instance) assert (None, '') == ansible.execute()
def test_inventory_generation(molecule_instance, docker_instance): molecule_instance.driver = docker_instance molecule_instance.driver.up() molecule_instance.create_inventory_file() pb = molecule_instance.driver.ansible_connection_params pb['playbook'] = 'playbook.yml' pb['inventory'] = 'test1,test2,' ansible = ansible_playbook.AnsiblePlaybook(pb) for instance in molecule_instance.driver.instances: expected = '{} ansible_connection=docker\n'.format(instance['name']) assert expected == molecule_instance.driver.inventory_entry(instance) # TODO(retr0h): Understand why driver is None assert (None, '') == ansible.execute()
def execute(self, exit=True): """ Execute the actions necessary to perform a `molecule syntax` and return a tuple. :param exit: (Unused) Provided to complete method signature. :return: Return a tuple provided by :meth:`.AnsiblePlaybook.execute`. """ debug = self.args.get('debug') self.molecule.create_templates() d = dependency.Dependency(self.args, self.command_args, self.molecule) d.execute() ansible = ansible_playbook.AnsiblePlaybook( self.molecule.config.config['ansible'], {}, debug=debug) ansible.add_cli_arg('syntax-check', True) ansible.add_cli_arg('inventory_file', 'localhost,') util.print_info("Checking playbook's syntax...") return ansible.execute(hide_errors=True)
def execute(self, exit=True): """ Execute the actions necessary to perform a `molecule check` and return a tuple. :param exit: (Unused) Provided to complete method signature. :return: Return a tuple provided by :meth:`.AnsiblePlaybook.execute`. """ if not self.molecule.state.created: msg = ('Instance(s) not created, `check` should be run ' 'against created instance(s)') LOG.error('ERROR: {}'.format(msg)) util.sysexit() ansible = ansible_playbook.AnsiblePlaybook( self.molecule.config.config['ansible'], self.molecule.driver.ansible_connection_params) ansible.add_cli_arg('check', True) util.print_info('Performing a "Dry Run" of playbook ...') return ansible.execute(hide_errors=True)
def execute(self): """ Executes linting/integration tests, and returns None. Flake8 performs the code linting. Testinfra executes integration tests. :return: None """ ansible = ansible_playbook.AnsiblePlaybook( self._molecule.config.config['ansible'], _env=self._molecule.env) testinfra_options = config.merge_dicts( self._molecule.driver.testinfra_args, self._molecule.verifier_options) testinfra_options['env'] = ansible.env testinfra_options['debug'] = self._molecule.args.get('--debug', False) testinfra_options['sudo'] = self._molecule.args.get('--sudo', False) tests_glob = self._get_tests() if len(tests_glob) > 0: self._flake8(tests_glob) self._testinfra(tests_glob, **testinfra_options)
def ansible_playbook_instance(config_instance): return ansible_playbook.AnsiblePlaybook('inventory', 'playbook', config_instance)
def ansible_playbook_instance(ansible_section_data): return ansible_playbook.AnsiblePlaybook(ansible_section_data['ansible'])
def execute(self, idempotent=False, create_instances=True, create_inventory=True, exit=True, hide_errors=True): """ Execute the actions necessary to perform a `molecule converge` and return a tuple. :param idempotent: An optional flag to perform the converge again, and parse the output for idempotence. :param create_inventory: An optional flag to toggle inventory creation. :param create_instances: An optional flag to toggle instance creation. :return: Return a tuple of (`exit status`, `command output`), otherwise sys.exit on command failure. """ if self.molecule.state.created: create_instances = False if self.molecule.state.converged: create_inventory = False if self.molecule.state.multiple_platforms: self.command_args['platform'] = 'all' else: if ((self.command_args.get('platform') == 'all') and self.molecule.state.created): create_instances = True create_inventory = True if create_instances and not idempotent: c = create.Create(self.command_args, self.args, self.molecule) c.execute() if create_inventory: self.molecule.create_inventory_file() # Install role dependencies only during `molecule converge` if not idempotent and 'requirements_file' in \ self.molecule.config.config['ansible'] and not \ self.molecule.state.installed_deps: galaxy = ansible_galaxy.AnsibleGalaxy(self.molecule.config.config) galaxy.install() self.molecule.state.change_state('installed_deps', True) ansible = ansible_playbook.AnsiblePlaybook( self.molecule.config.config['ansible'], self.molecule.driver.ansible_connection_params) # Target tags passed in via CLI if self.command_args.get('tags'): ansible.add_cli_arg('tags', self.command_args.get('tags')) if idempotent: # Don't log stdout/err ansible.remove_cli_arg('_out') ansible.remove_cli_arg('_err') # Disable color for regexp ansible.add_env_arg('ANSIBLE_NOCOLOR', 'true') ansible.add_env_arg('ANSIBLE_FORCE_COLOR', 'false') ansible.bake() if self.args.get('debug'): ansible_env = { k: v for (k, v) in ansible.env.items() if 'ANSIBLE' in k } other_env = { k: v for (k, v) in ansible.env.items() if 'ANSIBLE' not in k } util.print_debug( 'OTHER ENVIRONMENT', yaml.dump(other_env, default_flow_style=False, indent=2)) util.print_debug( 'ANSIBLE ENVIRONMENT', yaml.dump(ansible_env, default_flow_style=False, indent=2)) util.print_debug('ANSIBLE PLAYBOOK', str(ansible._ansible)) util.print_info('Starting Ansible Run ...') status, output = ansible.execute(hide_errors=hide_errors) if status is not None: if exit: util.sysexit(status) return status, None if not self.molecule.state.converged: self.molecule.state.change_state('converged', True) return None, output
def test_init_connection_params(ansible_v1_section_data, ansible_playbook_instance): connection_params = {'foo': 'bar'} a = ansible_playbook.AnsiblePlaybook(ansible_v1_section_data['ansible'], connection_params) assert 'bar' == a._cli.get('foo')
def execute(self, idempotent=False, create_instances=True, create_inventory=True, exit=True, hide_errors=True): """ Execute the actions necessary to perform a `molecule converge` and return a tuple. :param idempotent: An optional flag to perform the converge again, and parse the output for idempotence. :param create_inventory: An optional flag to toggle inventory creation. :param create_instances: An optional flag to toggle instance creation. :return: Return a tuple of (`exit status`, `command output`), otherwise sys.exit on command failure. """ debug = self.args.get('debug') if self.molecule.state.created: create_instances = False if self.molecule.state.converged: create_inventory = False if self.molecule.state.multiple_platforms: self.command_args['platform'] = 'all' else: if ((self.command_args.get('platform') == 'all') and self.molecule.state.created): create_instances = True create_inventory = True if create_instances and not idempotent: c = create.Create(self.args, self.command_args, self.molecule) c.execute() if create_inventory: self.molecule.create_inventory_file() d = dependency.Dependency(self.args, self.command_args, self.molecule) d.execute() ansible = ansible_playbook.AnsiblePlaybook( self.molecule.config.config['ansible'], self.molecule.driver.ansible_connection_params, raw_ansible_args=self.command_args.get('ansible_args'), debug=debug) if idempotent: # Don't log stdout/err ansible.remove_cli_arg('_out') ansible.remove_cli_arg('_err') # Idempotence task regexp cannot handle diff ansible.remove_cli_arg('diff') # Disable color for regexp ansible.add_env_arg('ANSIBLE_NOCOLOR', 'true') ansible.add_env_arg('ANSIBLE_FORCE_COLOR', 'false') if debug: ansible_env = { k: v for (k, v) in ansible.env.items() if 'ANSIBLE' in k } util.print_debug( 'ANSIBLE ENVIRONMENT', yaml.dump(ansible_env, default_flow_style=False, indent=2)) util.print_info('Starting Ansible Run ...') status, output = ansible.execute(hide_errors=hide_errors) if status is not None: if exit: util.sysexit(status) return status, None if not self.molecule.state.converged: self.molecule.state.change_state('converged', True) return None, output
def test_ignores_requirements_file(): a = ansible_playbook.AnsiblePlaybook({'requirements_file': 'foo/bar'}, {}) assert not a._cli.get('requirements_file')