Ejemplo n.º 1
0
def ansible_playbook_module(module, playbook, inventorys,  **kwargs):
    """
    Runs a anisble module
    :param module:
    :param kwargs:
    :return:
    """
    path = utils.get_file(module)
    ctx.logger.info('Playbook module: {0}.'.format(path))
    command = ["tar", "zxvf", path, '-C', '/opt']
    output = utils.run_command(command)
    ctx.logger.info('Command Output: {0}.'.format(output))
    inventory = utils.get_inventory_1(playbook, inventorys, **kwargs)
    command = ['ansible-playbook', '-i', inventory, playbook]
    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.')
Ejemplo n.º 2
0
def ansible_playbook_uninstall(playbook, inventorys,  **kwargs):
    """
    :param playbook:
    :param inventorys:
    :param kwargs:
    :return:
    """
    inventory = utils.get_inventory_2(playbook, inventorys)
    command = ['ansible-playbook', '-i', inventory, playbook]
    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.')
Ejemplo n.º 3
0
def ansible_playbook_ips(playbooks, inventorys=list(), **kwargs):
    """ Runs a playbook as part of a Cloudify lifecycle operation """

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

    for playbook in playbooks:
        playbook_path = utils.get_file(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.')
Ejemplo n.º 4
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))
        user = utils.get_agent_user()
        command = ['ansible-playbook', '-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.')
Ejemplo n.º 5
0
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.')
Ejemplo n.º 6
0
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.')
Ejemplo n.º 7
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']
Ejemplo n.º 8
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.')