• Recent
  • Unsolved
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Register
  • Login
  • Recent
  • Unsolved
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Register
  • Login

Assign multiple host to a group

Scheduled Pinned Locked Moved Unsolved
FOG Problems
2
10
2.2k
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S
    sapeurorca
    last edited by May 30, 2017, 3:39 PM

    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

    1 Reply Last reply Reply Quote 0
    • T
      Tom Elliott
      last edited by May 30, 2017, 3:47 PM

      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.

      Please help us build the FOG community with everyone involved. It's not just about coding - way more we need people to test things, update documentation and most importantly work on uniting the community of people enjoying and working on FOG! Get in contact with me (chat bubble in the top right corner) if you want to join in.

      Web GUI issue? Please check apache error (debian/ubuntu: /var/log/apache2/error.log, centos/fedora/rhel: /var/log/httpd/error_log) and php-fpm log (/var/log/php*-fpm.log)

      Please support FOG if you like it: https://wiki.fogproject.org/wiki/index.php/Support_FOG

      1 Reply Last reply Reply Quote 0
      • S
        sapeurorca
        last edited by May 30, 2017, 3:55 PM

        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

        T 1 Reply Last reply May 30, 2017, 4:11 PM Reply Quote 0
        • T
          Tom Elliott @sapeurorca
          last edited by May 30, 2017, 4:11 PM

          @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.

          Please help us build the FOG community with everyone involved. It's not just about coding - way more we need people to test things, update documentation and most importantly work on uniting the community of people enjoying and working on FOG! Get in contact with me (chat bubble in the top right corner) if you want to join in.

          Web GUI issue? Please check apache error (debian/ubuntu: /var/log/apache2/error.log, centos/fedora/rhel: /var/log/httpd/error_log) and php-fpm log (/var/log/php*-fpm.log)

          Please support FOG if you like it: https://wiki.fogproject.org/wiki/index.php/Support_FOG

          1 Reply Last reply Reply Quote 0
          • S
            sapeurorca
            last edited by sapeurorca May 30, 2017, 10:30 AM May 30, 2017, 4:21 PM

            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

            T 1 Reply Last reply May 30, 2017, 5:04 PM Reply Quote 0
            • T
              Tom Elliott @sapeurorca
              last edited by Tom Elliott May 31, 2017, 9:16 AM May 30, 2017, 5:04 PM

              @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).

              Please help us build the FOG community with everyone involved. It's not just about coding - way more we need people to test things, update documentation and most importantly work on uniting the community of people enjoying and working on FOG! Get in contact with me (chat bubble in the top right corner) if you want to join in.

              Web GUI issue? Please check apache error (debian/ubuntu: /var/log/apache2/error.log, centos/fedora/rhel: /var/log/httpd/error_log) and php-fpm log (/var/log/php*-fpm.log)

              Please support FOG if you like it: https://wiki.fogproject.org/wiki/index.php/Support_FOG

              1 Reply Last reply Reply Quote 0
              • S
                sapeurorca
                last edited by May 31, 2017, 6:47 AM

                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.

                1 Reply Last reply Reply Quote 0
                • S
                  sapeurorca
                  last edited by May 31, 2017, 3:14 PM

                  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.

                  T 1 Reply Last reply May 31, 2017, 3:16 PM Reply Quote 0
                  • T
                    Tom Elliott @sapeurorca
                    last edited by May 31, 2017, 3:16 PM

                    @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.

                    Please help us build the FOG community with everyone involved. It's not just about coding - way more we need people to test things, update documentation and most importantly work on uniting the community of people enjoying and working on FOG! Get in contact with me (chat bubble in the top right corner) if you want to join in.

                    Web GUI issue? Please check apache error (debian/ubuntu: /var/log/apache2/error.log, centos/fedora/rhel: /var/log/httpd/error_log) and php-fpm log (/var/log/php*-fpm.log)

                    Please support FOG if you like it: https://wiki.fogproject.org/wiki/index.php/Support_FOG

                    1 Reply Last reply Reply Quote 0
                    • S
                      sapeurorca
                      last edited by May 31, 2017, 3:20 PM

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

                      1 Reply Last reply Reply Quote 0
                      • 1 / 1
                      1 / 1
                      • First post
                        1/10
                        Last post

                      184

                      Online

                      12.0k

                      Users

                      17.3k

                      Topics

                      155.2k

                      Posts
                      Copyright © 2012-2024 FOG Project