class TestConfig(testtools.TestCase): def setUp(self): super(TestConfig, self).setUp() self.data = { 'playbook': 'playbook.yml', 'config_file': 'test.cfg', 'limit': 'all', 'verbose': 'vvvv', 'diff': True, 'host_key_checking': False, 'raw_ssh_args': [ '-o UserKnownHostsFile=/dev/null', '-o IdentitiesOnly=yes', '-o ControlMaster=auto', '-o ControlPersist=60s' ], 'raw_env_vars': { 'TEST_1': 'test_1' } } self.ansible = AnsiblePlaybook(self.data) def test_arg_loading(self): # string value set self.assertEqual(self.ansible.cli['limit'], self.data['limit']) # true value set self.assertEqual(self.ansible.cli['diff'], self.data['diff']) # false values don't exist in arg dict at all self.assertIsNone(self.ansible.cli.get('sudo_user')) def test_parse_arg_special_cases(self): # raw environment variables are set self.assertIsNone(self.ansible.cli.get('raw_env_vars')) self.assertEqual(self.ansible.env['TEST_1'], self.data['raw_env_vars']['TEST_1']) # raw_ssh_args set self.assertIsNone(self.ansible.cli.get('raw_ssh_args')) self.assertEqual(self.ansible.env['ANSIBLE_SSH_ARGS'], ' '.join(self.data['raw_ssh_args'])) # host_key_checking gets set in environment as string 'false' self.assertIsNone(self.ansible.cli.get('host_key_checking')) self.assertEqual(self.ansible.env['ANSIBLE_HOST_KEY_CHECKING'], 'false') # config_file is set in environment self.assertIsNone(self.ansible.cli.get('config_file')) self.assertEqual(self.ansible.env['ANSIBLE_CONFIG'], self.data['config_file']) # playbook is set as attribute self.assertIsNone(self.ansible.cli.get('playbook')) self.assertEqual(self.ansible.playbook, self.data['playbook']) # verbose is set in the right place self.assertIsNone(self.ansible.cli.get('verbose')) self.assertIn('-' + self.data['verbose'], self.ansible.cli_pos) def test_add_cli_arg(self): # redefine a previously defined value self.ansible.add_cli_arg('limit', 'test') self.assertEqual(self.ansible.cli['limit'], 'test') # add a new value self.ansible.add_cli_arg('molecule_1', 'test') self.assertEqual(self.ansible.cli['molecule_1'], 'test') # values set as false shouldn't get added self.ansible.add_cli_arg('molecule_2', None) self.assertNotIn('molecule_2', self.ansible.cli) def test_remove_cli_arg(self): self.ansible.remove_cli_arg('limit') self.assertNotIn('limit', self.ansible.cli) def test_add_env_arg(self): # redefine a previously defined value self.ansible.add_env_arg('TEST_1', 'now') self.assertEqual(self.ansible.env['TEST_1'], 'now') # add a new value self.ansible.add_env_arg('MOLECULE_1', 'test') self.assertEqual(self.ansible.env['MOLECULE_1'], 'test') def test_remove_env_arg(self): self.ansible.remove_env_arg('TEST_1') self.assertNotIn('TEST_1', self.ansible.env)
def execute(self, idempotent=False, create_instances=True, create_inventory=True, exit=True, hide_errors=True): """ :param idempotent: Optionally provision servers quietly so output can be parsed for idempotence :param create_inventory: Toggle inventory creation :param create_instances: Toggle instance creation :return: Provisioning output """ if self.molecule._state.get('created'): create_instances = False if self.molecule._state.get('converged'): create_inventory = False if self.static: create_instances = False create_inventory = False if create_instances and not idempotent: command_args, args = utilities.remove_args(self.command_args, self.args, ['--tags']) c = Create(command_args, 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']: print('{}Installing role dependencies ...{}'.format( colorama.Fore.CYAN, colorama.Fore.RESET)) galaxy_install = AnsibleGalaxyInstall(self.molecule._config.config[ 'ansible']['requirements_file']) galaxy_install.add_env_arg( 'ANSIBLE_CONFIG', self.molecule._config.config['ansible']['config_file']) galaxy_install.bake() output = galaxy_install.execute() ansible = AnsiblePlaybook(self.molecule._config.config['ansible']) # params to work with provisioner for k, v in self.molecule._provisioner.ansible_connection_params.items( ): ansible.add_cli_arg(k, v) # target tags passed in via CLI if self.molecule._args.get('--tags'): ansible.add_cli_arg('tags', self.molecule._args['--tags'].pop(0)) if idempotent: ansible.remove_cli_arg('_out') ansible.remove_cli_arg('_err') ansible.add_env_arg('ANSIBLE_NOCOLOR', 'true') ansible.add_env_arg('ANSIBLE_FORCE_COLOR', 'false') # Save the previous callback plugin if any. callback_plugin = ansible.env.get('ANSIBLE_CALLBACK_PLUGINS', '') # Set the idempotence plugin. if callback_plugin: ansible.add_env_arg( 'ANSIBLE_CALLBACK_PLUGINS', callback_plugin + ':' + os.path.join( sys.prefix, 'share/molecule/ansible/plugins/callback/idempotence')) else: ansible.add_env_arg('ANSIBLE_CALLBACK_PLUGINS', os.path.join( sys.prefix, 'share/molecule/ansible/plugins/callback/idempotence')) ansible.bake() if self.molecule._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} utilities.debug('OTHER ENVIRONMENT', yaml.dump(other_env, default_flow_style=False, indent=2)) utilities.debug('ANSIBLE ENVIRONMENT', yaml.dump(ansible_env, default_flow_style=False, indent=2)) utilities.debug('ANSIBLE PLAYBOOK', str(ansible.ansible)) status, output = ansible.execute(hide_errors=hide_errors) if status is not None: if exit: sys.exit(status) return status, None if not self.molecule._state.get('converged'): self.molecule._state['converged'] = True self.molecule._write_state_file() return None, output
def execute(self, idempotent=False, create_instances=True, create_inventory=True, exit=True, hide_errors=True): """ :param idempotent: Optionally provision servers quietly so output can be parsed for idempotence :param create_inventory: Toggle inventory creation :param create_instances: Toggle instance creation :return: Provisioning output """ if self.molecule._state.get('created'): create_instances = False if self.molecule._state.get('converged'): create_inventory = False if self.static: create_instances = False create_inventory = False if create_instances and not idempotent: command_args, args = utilities.remove_args(self.command_args, self.args, ['--tags']) c = Create(command_args, 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']: print('{}Installing role dependencies ...{}'.format( Fore.CYAN, Fore.RESET)) galaxy_install = AnsibleGalaxyInstall( self.molecule._config.config['ansible']['requirements_file']) galaxy_install.add_env_arg( 'ANSIBLE_CONFIG', self.molecule._config.config['ansible']['config_file']) galaxy_install.bake() output = galaxy_install.execute() ansible = AnsiblePlaybook(self.molecule._config.config['ansible']) # target tags passed in via CLI if self.molecule._args.get('--tags'): ansible.add_cli_arg('tags', self.molecule._args['--tags'].pop(0)) if idempotent: ansible.remove_cli_arg('_out') ansible.remove_cli_arg('_err') ansible.add_env_arg('ANSIBLE_NOCOLOR', 'true') ansible.add_env_arg('ANSIBLE_FORCE_COLOR', 'false') # Save the previous callback plugin if any. callback_plugin = ansible.env.get('ANSIBLE_CALLBACK_PLUGINS', '') # Set the idempotence plugin. if callback_plugin: ansible.add_env_arg( 'ANSIBLE_CALLBACK_PLUGINS', callback_plugin + ':' + os.path.join( sys.prefix, 'share/molecule/ansible/plugins/callback/idempotence')) else: ansible.add_env_arg( 'ANSIBLE_CALLBACK_PLUGINS', os.path.join( sys.prefix, 'share/molecule/ansible/plugins/callback/idempotence')) ansible.bake() if self.molecule._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 } utilities.debug( 'OTHER ENVIRONMENT', yaml.dump(other_env, default_flow_style=False, indent=2)) utilities.debug( 'ANSIBLE ENVIRONMENT', yaml.dump(ansible_env, default_flow_style=False, indent=2)) utilities.debug('ANSIBLE PLAYBOOK', str(ansible.ansible)) status, output = ansible.execute(hide_errors=hide_errors) if status is not None: if exit: sys.exit(status) return status, None if not self.molecule._state.get('converged'): self.molecule._state['converged'] = True self.molecule._write_state_file() return None, output
def execute(self, idempotent=False, create_instances=True, create_inventory=True): """ Provisions all instances using ansible-playbook. :param idempotent: Optionally provision servers quietly so output can be parsed for idempotence :param create_inventory: Toggle inventory creation :param create_instances: Toggle instance creation :return: Provisioning output """ if self.molecule._state.get('created'): create_instances = False if self.molecule._state.get('converged'): create_inventory = False if self.static: create_instances = False create_inventory = False if create_instances and not idempotent: c = Create(self.args, self.molecule) c.execute() if create_inventory: self.molecule._create_inventory_file() ansible = AnsiblePlaybook(self.molecule._config.config['ansible']) # target tags passed in via CLI ansible.add_cli_arg('tags', self.molecule._args.get('--tags')) if idempotent: ansible.remove_cli_arg('_out') ansible.remove_cli_arg('_err') ansible.add_env_arg('ANSIBLE_NOCOLOR', 'true') ansible.add_env_arg('ANSIBLE_FORCE_COLOR', 'false') # Save the previous callback plugin if any. callback_plugin = ansible.env.get('ANSIBLE_CALLBACK_PLUGINS', '') # Set the idempotence plugin. if callback_plugin: ansible.add_env_arg('ANSIBLE_CALLBACK_PLUGINS', callback_plugin + ':' + os.path.join( sys.prefix, 'share/molecule/ansible/plugins/callback/idempotence')) else: ansible.add_env_arg('ANSIBLE_CALLBACK_PLUGINS', os.path.join(sys.prefix, 'share/molecule/ansible/plugins/callback/idempotence')) ansible.bake() if self.molecule._args['--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} utilities.debug('OTHER ENVIRONMENT', yaml.dump(other_env, default_flow_style=False, indent=2)) utilities.debug('ANSIBLE ENVIRONMENT', yaml.dump(ansible_env, default_flow_style=False, indent=2)) utilities.debug('ANSIBLE PLAYBOOK', str(ansible.ansible)) output = ansible.execute() if not self.molecule._state.get('converged'): self.molecule._state['converged'] = True self.molecule._write_state_file() return output