Example #1
0
def ansible_playbook(playbooks, inventory, **kwargs):
    """ Runs a playbook as part of a Cloudify lifecycle operation """

    inventory_path = utils.get_inventory_path(inventory)
    ctx.logger.info('Inventory path: {0}.'.format(inventory_path))

    for playbook in playbooks:
        playbook_path = utils.get_playbook_path(playbook)
        ctx.logger.info('Playbook path: {0}.'.format(playbook_path))
        command = ['ansible-playbook', '-i', inventory_path, playbook_path]
        ctx.logger.info('Running command: {0}.'.format(command))
        output = utils.run_command(command)
        ctx.logger.info('Command Output: {0}.'.format(output))
        ctx.logger.info('Finished running the Ansible Playbook.')

    return "install success"
def ansible_playbook(playbooks, inventory=list(), **kwargs):
    """ Runs a playbook as part of a Cloudify lifecycle operation """

    inventory_path = utils.get_inventory_path(inventory)
    ctx.logger.info('Inventory path: {0}.'.format(inventory_path))

    for playbook in playbooks:
        playbook_path = utils.get_playbook_path(playbook)
        ctx.logger.info('Playbook path: {0}.'.format(playbook_path))
        user = utils.get_agent_user()
        command = ['ansible-playbook', '--sudo', '-u', user,
                   '-i', inventory_path, playbook_path,
                   '--timeout=60', '-vvvv']
        ctx.logger.info('Running command: {0}.'.format(command))
        output = utils.run_command(command)
        ctx.logger.info('Command Output: {0}.'.format(output))
        ctx.logger.info('Finished running the Ansible Playbook.')
def ansible_playbook(playbooks, inventory=list(), **kwargs):
    """ Runs a playbook as part of a Cloudify lifecycle operation """

    inventory_path = utils.get_inventory_path(inventory)
    ctx.logger.info('Inventory path: {0}.'.format(inventory_path))

    for playbook in playbooks:
        playbook_path = utils.get_playbook_path(playbook)
        ctx.logger.info('Playbook path: {0}.'.format(playbook_path))
        user = utils.get_agent_user()
        command = [
            'ansible-playbook', '--sudo', '-u', user, '-i', inventory_path,
            playbook_path, '--timeout=60', '-vvvv'
        ]
        ctx.logger.info('Running command: {0}.'.format(command))
        output = utils.run_command(command)
        ctx.logger.info('Command Output: {0}.'.format(output))
        ctx.logger.info('Finished running the Ansible Playbook.')
def ansible_playbook(playbooks, inventory=list(), extravars='', **kwargs):
    """ Runs a playbook as part of a Cloudify lifecycle operation """

    inventory_path = utils.get_inventory_path(inventory)
    ctx.logger.info('Inventory path: {}.'.format(inventory_path))
    extraargs = ' --extra-vars "{}"'.format(extravars) if extravars else ''
    for playbook in playbooks:
        playbook_path = utils.get_playbook_path(playbook)
        ctx.logger.info('Playbook path: {}.'.format(playbook_path))
        user = utils.get_agent_user()
        # command = ['ansible-playbook', '--sudo', '-u', user,
        #            '-i', inventory_path, '--timeout=60', '-vvvv']
        # if extraargs:
        #     command += shlex.split(extraargs)
        # command += shlex.split(playbook_path)
        command = 'ansible-playbook --sudo -u {} -i {} {} --timeout=60 -vvvv{}'.format(
            user, inventory_path, playbook_path,
            extraargs if extraargs else '')
        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.')
Example #5
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.')