Assign multiple host to a group



  • Server
    • FOG Version: 1.3.3
    • OS: Ubuntu 16.04
    Client
    • Service Version: 0.11.8
    • OS: Windows 10
    Description

    Hello,
    I am looking for a solution to assign more than 300 hosts to a group easily and quickly. Because I should update this group about once a month.

    I’m looking for a script, an import or other solutions …

    Thank you for your help



  • It does not matter, I fully understand. But it helped me a lot, thanks again and congratulations to all the FOG team


  • Senior Developer

    @sapeurorca Updated with the changes, sorry I missed them. Like I said, I just wrote that up as I was writing, nothing to really base it or test it off of.



  • To make a return from my experience.
    After some small modification, the script works super well, I have to integrate my 300 PC in a group in a single manipulation.

    To help some, I correct two small mistake:

    $listOfNames = array_filter(array_unique(explode(',', $hostnames)));
    
    $hostIDs = FOGCore::getSubObjectIDs(
        'Host', array('name' => $listOfNames)
    );
    
    

    Thank you Tom Elliott for your help and your script.



  • Thank you very much for your answer and your script.
    I will look at all these in great detail, all this information will be very useful.

    I think now I should be able to get through.

    Thanks again.


  • Senior Developer

    @sapeurorca You can find host ID’s by name and parse it how ever you like. The api system handles things at the simplest level, which is by id’s (most API’s follow this same type of approach).

    This was not implemented with 1.3.3 no so you would have to script it entirely by yourself as needed.

    You can write a php script to do what you need.

    Something like:

    <?php
    require('/var/www/fog/commons/base.inc.php');
    /**
     * Could be handled more dynamically by making this a get variable.
     * This would allow you to call <scriptname>.php?groupname=SomeGroupName
     */
    //$groupname = filter_input(INPUT_GET, 'groupname');
    $groupname = 'groupnamehere';
    /**
     * Get the group object.
     */
    $group = FOGCore::getClass('Group')
        ->set('name', $groupname)
        ->load('name');
    /**
     * If the group isn't valid or doesn't exist, make it.
     */
    if (!$group->isValid()) {
        $group->save();
    }
    /**
     * Get the groups ID.
     */
    $groupID = $group->get('id');
    /**
     * The file must be in hostname1,hostname2 format (or csv if you will), You could modify to
     * process as csv where new hostnames are on new lines too. Something like:
     * hostname1
     * hostname2
     *
     * This would be handled by explode("\n", $hostnames) <Use of double quotes important>
     */
    $hostnames = file_get_contents('/path/to/csv/of/justhostnames.csv');
    /**
     * Just filters and makes the list unique (less work to try to process later on).
     */
    $listOfNames = array_filter(array_unique(explode(',', $hostnames)));
    /**
     * Just get's the hostID's that are already present. The hosts must already exist.
     * This will only return hostID's for valid hosts.
     */
    $hostIDs = FOGCore::getSubObjectIDs(
        'Host',
        array('name' => $listOfNames)
    );
    /**
     * The group association fields to be inserted.
     * GroupID is the group you're associating with gotten earlier.
     * HostID is the list of host IDs from earlier.
     */
    $fieldstogroupinsert = array(
        'groupID',
        'hostID'
    );
    /**
     * Starts our array of items to be inserted.
     */
    $items = array();
    /**
     * Loops each of the host ID's to build our query appropriately.
     */
    foreach ($hostIDs as &$hostID) {
        $items[] = array(
            $groupID,
            $hostID
        );
    }
    /**
     * Inserts all of the new associations.
     */
    FOGCore::getClass('GroupAssociationManager')
        ->insertBatch(
            $fieldstogroupinsert,
            $items
        );
    /**
     * Just let's you know it's finished.
     * NOTE: You could add error checking here if you want.
     */
    echo _('All done');
    ?>
    

    Hopefully all of that makes some sort of sense, if it were me I’d say do the PHP option and stick the file in /var/www/html/.

    Then you just call the script with: http://<fogip>/groupinsert.php (if groupinsert.php was the filename you called it.)

    Update the csv file when you need it and rerun the url call. You can script the call to the url with a curl request on a cron schedule if you needed to.

    Mind you I give no guarantees and I have not tested what I wrote (I just wrote it right now to give an example of how you could approach this). I suggest putting the file in /var/www/html so if you do update you don’t lose your script during upgrades, and the functionality should remain regardless of if you update or not.

    Take account you may need to add your own error handling to this script. I’m just giving a base suggestion, how you approach it is totally up to you.

    The csv file would just be a csv of hostnames, no headers or other information. e.g.:
    hostname1,hostname2,hostname3

    The script I wrote does not contain any space handling/trimming, nor does it take into account special characters. So you need to make sure your CSV is safe. Notice I’m not allowing any options, so you don’t have to worry about another person “exploiting” but security is up to you.

    If you want to semi automate it, the groupname line (line number three of the script) you could change it to:

    $groupname = filter_input(INPUT_GET, 'groupname');
    

    This would allow you to call the script appended with: ?groupname=SomeGroupNameHere. (Not a huge threat).

    You can also change the “file-get-contents” to be automated in a similar fashion though i’d highly recommend against it. But if you “must” you would specify the file name similarly. (Could be a huge threat).



  • OK thank you for the information. Is it possible to do this with the hosts name rather than with the hosts ID?

    And on 1.3.3, there is no solution? Because it was not yet planned for me to pass it in 1.4.0. I had seen this topic, but the script was not given.
    https://forums.fogproject.org/topic/7918/creating-group-from-file

    thank you


  • Senior Developer

    @sapeurorca Then update to 1.4.0 where there’s an API system that can do this for you.

    You would do:

    http://fogserver/group/<idofgrouptoedit>/edit

    To update the hosts you pass in the body json:

    {
        "hosts" : [ 1, 2, 3, 4 ]
    }
    

    Where 1, 2, 3, 4 are the ID’s of the hosts you need to add to the group.



  • Hello,

    Thanks for your reply, but I know how to create a group and add hosts manually.

    I am looking for a quick way to add these 300 hots to a group, knowing that I will update this group once a month.

    I put 300 hots together in a CSV file, if I had to use this CSV to update the group in FOG it would be perfect. Either with a script, or with an import, etc.

    thank you


  • Senior Developer

    Right from the GUI.

    Create your group.

    Edit the group.

    Choose “Membership”

    Check the box for hosts not in the group and you can filter using the table to get specific names, mac’s etc…

    Check box has deselect/select all and can be clicked. Choose the hosts you want and click “Add hosts to group” and you’re done.


Log in to reply
 

780
Online

39.3k
Users

11.0k
Topics

104.4k
Posts

Looks like your connection to FOG Project was lost, please wait while we try to reconnect.