Exemple #1
0
    def test_06_host_add_invalid(self):
        """add a host to a non-existent group
        """

        inventory = AnsibleInventory(INV_FILENAME, excl=True)
        with self.assertRaises(InventoryGroupMissing):
            inventory.host_add('mygroup', 'myhost')
Exemple #2
0
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
Exemple #3
0
    def test_08_host_add(self):
        """add a host to 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'))
Exemple #4
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)