How to get the Linux servers not already part of Dynamic CI Group

matiasgatho
Tera Contributor

The main goal is to get all the CIs that do not belong yet to a Dynamic CI group.

The test case is with Linux Servers. Some of them don't have the Managed By Group, Support group and Change group, so we would like to have a Dynamic CI group to have kind of have a "default management" of the CIs not yet assigned to the belong team.

Extra requirement, if the CI is then contained in another Dynamic CI group, the CI should be removed automatically from this Dynamic CI Group of "default management".

 

How could I do this ?

 

I tried several tests in the saved queries of the Query Builder, but I can't manage to accomplish the extra requirement. The groups just swap to the ones of the new Dynamic CI Group and then swap back to the groups of the default management DCIG. It's never getting out of the DCIG of default management.

 

Thanks already for your insights !

2 REPLIES 2

sivasankaris
Tera Guru

Hi @@matiasgath ,

It seems like we can't achieve your extra requirement through query builder. Because query builder which relates the linux server and dynamic ci group with no relation is not working as we expected.

 

The Best Approach: The "Assigned" Flag

Instead of asking the query to check for relationships in real-time, use a flag on the server record. This makes the logic solid and prevents loops.

 

Step 1: Add a "Flag" field

Create a new True/False field on the Server [cmdb_ci_server] table.

  • Label: In Dynamic Group

  • Name: u_in_dynamic_group

  • Default value: false

 

2. The Business Rule Configuration

  • Table: CI Relationship [cmdb_rel_ci]

  • When: async (or after)

  • Insert: Checked

  • Delete: Checked

  • Filter Conditions: * Parent.Class | is | Dynamic CI Group

    • Type | is | Member of::Members

3. The Script

JavaScript
 
(function executeRule(current, previous /*null when async*/) {

    // 1. Identify the CI (the child in the relationship)
    var ciRef = new GlideRecord('cmdb_ci_server');
    
    // Determine if we are adding or removing a relationship
    var ciSysID = (current.operation() == 'delete') ? current.child : current.child;

    if (ciRef.get(ciSysID)) {
        
        // 2. Check if the CI has ANY other active Dynamic CI Group relationships
        var relCheck = new GlideRecord('cmdb_rel_ci');
        relCheck.addQuery('child', ciSysID);
        relCheck.addQuery('parent.sys_class_name', 'cmdb_ci_query_based_service'); // Dynamic CI Group class
        relCheck.addQuery('type.name', 'Member of::Members');
        
        // If deleting, exclude the current record from the count
        if (current.operation() == 'delete') {
            relCheck.addQuery('sys_id', '!=', current.sys_id);
        }
        
        relCheck.query();

        // 3. Update the Flag
        // If we found relationships, set flag to true. Otherwise, false.
        ciRef.u_in_dynamic_group = relCheck.hasNext();
        ciRef.setWorkflow(false); // Prevent other rules from looping
        ciRef.update();
    }

})(current, previous);

.

  •  

Step 3: Update your "Default" Query

Now, your Query Builder query for the "Default Management" group is very simple:

  1. Class: Linux Server.

  2. Filter: In Dynamic Group | is | False.

  3. Filter: Support Group | is empty.

BY Using this business rule and flag field we can achieve your requirement.

 

If you find this helpful... Please Mark it as helpful and please Accept my Solution...

Best Regards

SIVASANKARI S 

matiasgatho
Tera Contributor

Hi @sivasankaris ,

 

Thanks for answering ! This looks like a good solution for this, but isn't there any out-of-the-box solution that could solve this instead ? We are trying to keep our instances as much as possible as out-of-the-box. isn't that possible ?