def configure(user=None, key=None, **kwargs): agent_key_path = utils.get_keypair_path(key) configuration = '[defaults]\n' \ 'host_key_checking=False\n' \ 'private_key_file={0}\n'.format(agent_key_path) ctx.logger.info('Configuring Anisble.') file_path = utils.write_configuration_file(configuration) ctx.logger.info('Configured Ansible.') os.environ['ANSIBLE_CONFIG'] = file_path os.environ['USER'] = utils.get_agent_user(user) os.environ['HOME'] = home = os.path.expanduser("~") if os.path.exists(os.path.join(home, '.ansible')): shutil.rmtree(os.path.join(home, '.ansible')) os.makedirs(os.path.join(home, '.ansible'))
def configure(user, keypair, rolesfile, roles, private_ip_address, playbook = None, **kwargs): ctx.logger.info('Configuring Ansible.') os.environ['USER'] = user os.environ['HOME'] = os.path.expanduser("~") ansible_home = utils.get_ansible_home() if not os.path.exists(ansible_home): os.makedirs(ansible_home) ctx.logger.info('Created folder for ansible scripts: {}'.format(ansible_home)) ctx.logger.info('Getting the path to the keypair.') path_to_key = utils.get_keypair_path(keypair) os.chmod(path_to_key, 0600) ctx.logger.info('Got the keypair path: {}'.format(path_to_key)) configuration = '[defaults]\n' \ 'host_key_checking=False\n' \ 'remote_user={0}\n'\ 'private_key_file={1}\n'\ '[ssh_connection]\n'\ 'control_path=%(directory)s/%%h-%%r\n'.format(user, path_to_key) file_path = utils.write_configuration_file(ansible_home, configuration) os.environ['ANSIBLE_CONFIG'] = file_path ctx.logger.info('Getting the path to the playbook.') if playbook == None: ctx.logger.info('No Playbook given, creating it from roles.') hosts = [private_ip_address] playbook_path = utils.create_playbook_from_roles(hosts, roles) else: playbook_path = utils.get_playbook_path(playbook, ansible_home) ctx.logger.info('Got the playbook path: {}.'.format(playbook_path)) ctx.logger.info('Upload the rolesfile file.') roles_path = utils.get_roles(rolesfile, ansible_home) ctx.logger.info('Got the rolesfile path: {}'.format(roles_path)) ctx.logger.info('Unzip the rolesfile file.') command = ['unzip', '-o', roles_path,'-d', os.path.dirname(roles_path)] ctx.logger.info('Running command: {}.'.format(command)) output = utils.run_command(command) ctx.logger.info('Command Output: {}.'.format(output)) """ctx.logger.info('Delete the rolesfile archive.') os.remove(roles_path) command = ['rm', '-rf', roles_path] ctx.logger.info('Running command: {}.'.format(command)) output = utils.run_command(command) ctx.logger.info('Command Output: {}.'.format(output)) """ ctx.logger.info('Getting the inventory path.') ips = [private_ip_address] inventory_path = utils.get_inventory_path(ips, os.path.dirname(playbook_path)) ctx.logger.info('Got the inventory path: {}.'.format(inventory_path)) ctx.logger.info('Configured Ansible.')