Esempio n. 1
0
def ansible_playbook(playbook = 'playbook.yaml', **kwargs):    
    """ Runs a playbook as part of a Cloudify lifecycle operation """
    ansible_home = utils.get_ansible_home()

    executible = utils.get_executible_path('ansible-playbook')
    inventory_path = os.path.join(ansible_home, '{}.inventory'.format(ctx.deployment.id))
    playbook_path = os.path.join(ansible_home, playbook)
    
    os.environ['HOME'] = ansible_home
    os.environ['ANSIBLE_CONFIG'] = os.path.join(ansible_home, 'ansible.cfg')
    
    command = [executible, '-i', inventory_path,
               playbook_path, '--timeout=60', '-vvvv']

    ctx.logger.info('Running command: {}.'.format(command))

    output = utils.run_command(command)

    ctx.logger.info('Command Output: {}.'.format(output))

    ctx.logger.info('Finished running the Ansible Playbook.')
    
    del os.environ['HOME']
Esempio n. 2
0
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.')