예제 #1
0
    def test_09_host_remove(self):
        """remove a host from an existent group
        """
        inventory = AnsibleInventory(INV_FILENAME, excl=True)
        inventory.group_add('mygroup')
        self.assertIn('mygroup', inventory.groups)

        # two write operations not supported..
        # Needed another AnsibleInventary Object
        inventory = AnsibleInventory(INV_FILENAME, excl=True)
        inventory.host_add('mygroup', 'myhost')
        self.assertIn('myhost', inventory.group_show('mygroup'))

        # two write operations not supported..
        # Needed another AnsibleInventary Object
        inventory = AnsibleInventory(INV_FILENAME, excl=True)
        inventory.host_remove('mygroup', 'myhost')
        self.assertNotIn('myhost', inventory.group_show('mygroup'))
예제 #2
0
def get_group_members(group_name):
    r = APIResponse()
    inventory = AnsibleInventory()
    try:
        group_hosts = inventory.group_show(group_name)
    except InventoryGroupMissing:
        r.status, r.msg = 'NOTFOUND', "Group doesn't exist"
    else:
        r.status, r.data = "OK", {"members": group_hosts}
    return r
예제 #3
0
파일: hosts.py 프로젝트: lamtov/lamtv10_ops
def add_host(host_name, group_name, ssh_port=None):
    r = APIResponse()
    r.data = {"hostname": host_name}
    inventory = AnsibleInventory(excl=True)
    if not inventory.loaded:
        r.status, r.msg = "LOCKED", \
                          "Unable to lock the inventory file, try later"
        return r

    if group_name not in inventory.groups:
        # invalid request no such group
        r.status, r.msg = "INVALID", "No such group found in the inventory"
        inventory.unlock()
        return r

    group_members = inventory.group_show(group_name)
    if host_name in group_members:
        # host already in that group!
        r.status, r.msg = "OK", \
                          "Host already in the group {}".format(group_name)
        inventory.unlock()
        return r

    # At this point, the group is valid, and the host requested isn't already
    # in it, so proceed

    # TODO is name an IP - if so is it valid?
    # TODO if it's a name, does it resolve with DNS?
    if configuration.settings.ssh_checks:
        ssh_ok, msg = ssh_connect_ok(host_name, port=ssh_port)
        if ssh_ok:
            logger.info("SSH - {}".format(msg))
        else:
            logger.error("SSH - {}".format(msg))
            error_info = msg.split(':', 1)
            if error_info[0] == "NOAUTH":
                pub_key_file = os.path.join(
                    configuration.settings.playbooks_root_dir,  # noqa
                    "env/ssh_key.pub")
                r.data = {"pub_key": fread(pub_key_file)}

            r.status, r.msg = error_info

            inventory.unlock()
            return r
    else:
        logger.warning("Skipped SSH connection test for {}".format(host_name))
        r.msg = 'skipped SSH checks due to ssh_checks disabled by config'

    inventory.host_add(group_name, host_name, ssh_port)
    r.status = "OK"
    r.msg = "{} added".format(host_name)

    return r
예제 #4
0
def remove_hostvars(host_name, group_name):
    r = APIResponse()
    r.data = {"hostname": host_name}

    hostvars_path = os.path.join(configuration.settings.playbooks_root_dir,
                                 "project", "host_vars", host_name)

    logger.debug("Deleting HOSTVARs for {} from filesystem".format(host_name))
    try:
        os.remove(hostvars_path)
    except OSError as e:
        if e.errno == 2:
            # file doesn't exist, ignore the error
            pass
        else:
            raise

    # now remove from the inventory
    inventory = AnsibleInventory(excl=True)
    if not inventory.loaded:
        r.status, r.msg = "LOCKED", \
                          "Unable to lock the inventory file, try later"
        return r

    if host_name not in inventory.group_show(group_name):
        r.status, r.msg = "NOTFOUND", \
                          "Host '{}' not in group '{}'".format(host_name,
                                                               group_name)
        return r

    try:
        inventory.host_vars_remove(group_name, host_name)

    except InventoryHostMissing as e:
        r.status, r.msg = "NOTFOUND", \
                          "Host '{}' not in group '{}'".format(host_name,
                                                               group_name)
        return r
    except InventoryGroupMissing as e:
        r.status, r.msg = "NOTFOUND", \
                          "Group '{}' does not exist".format(group_name)
        return r

    r.status, r.msg = "OK", \
                      "Vars removed for '{}' in group '{}'".format(host_name,
                                                                   group_name)
    return r
예제 #5
0
    def test_11_remove_nonempty(self):
        """remove a group with hosts
        """

        inventory = AnsibleInventory(INV_FILENAME, excl=True)
        inventory.group_add('mygroup')
        self.assertIn('mygroup', inventory.groups)

        # two write operations not supported..
        # Needed another AnsibleInventary Object
        inventory = AnsibleInventory(INV_FILENAME, excl=True)
        inventory.host_add('mygroup', 'myhost')
        self.assertIn('myhost', inventory.group_show('mygroup'))

        # two write operations not supported..
        # Needed another AnsibleInventary Object
        inventory = AnsibleInventory(INV_FILENAME, excl=True)
        inventory.group_remove('mygroup')
        self.assertNotIn('mygroup', inventory.groups)
예제 #6
0
파일: hosts.py 프로젝트: lamtov/lamtv10_ops
def remove_host(host_name, group_name):
    r = APIResponse()
    inventory = AnsibleInventory(excl=True)

    if not inventory.loaded:
        r.status, r.msg = "LOCKED", "Unable to lock the inventory file, " \
                                    "try later"
        return r

    if (group_name not in inventory.groups
            or host_name not in inventory.group_show(group_name)):
        # invalid request
        r.status, r.msg = "INVALID", "No such group found in the inventory"
        inventory.unlock()
        return r

    # At this point the removal is ok
    inventory.host_remove(group_name, host_name)
    r.status = "OK"

    return r