Python regex to match a valid IPv4 address
(?:(?:25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])
Python regex to match a valid IPv4 address
(?:(?:25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])
Group table
|
Group membership table
|
<?php
// Create connection to database $conn = pg_connect($conn_string);
// Query the group table that allows building of groups and children
$sql = "SELECT groupname, grouplevel FROM YOUR_TABLE ORDER BY grouplevel ASC";
$groups = pg_query($sql);
// Query the group membership table that allows building of hosts in groups
$sql = "SELECT groupname, hostname FROM YOUR_TABLE ORDER BY grouplevel ASC";
$groupmembers = pg_query($sql);
// Set some helper variables
$response = array();
$parentgroup = "";
$subgroup = "";
$subgrouplevel = 0;
// We loop once over the group list. We create the respective arrays and
// identify the children of each group based on the grouplevel hierarchy.
// The hierarchy is based on a modulo 100 function for major groups and
// modulo 20 for subgroups. Very important to keep in mind that the group
// list is sorted based on grouplevel.
// To avoid conflicts with subgroups we restrict the subgroup range to +20 from
// the subgroup level
// Example of the hierarchy we achieve for groups residing in the range 200-299:
// 200 ( 201, 202, 203, 220, 240), 220 ( 221, 222), 240 ( 241, 242)
while ($row = pg_fetch_array($groups, null, PGSQL_ASSOC)) {
$group = trim($row['groupname']);
$level = trim($row['grouplevel']);
if (!array_key_exists($group,$response)) $response[$group] = array();
// if true we have identified a major group. just store the name
if ($level % 100 == 0) $parentgroup = $group;
else
{
// if true we have identified a subgroup. store the name and the level and continue
if ($level % 20 == 0) {$subgroup = $group; $subgrouplevel = $level;}
// at the next loop if we are within the subgroup limits
// we set current group as subgroup child
if ($level > $subgrouplevel && $level < ($subgrouplevel + 20))
{
if (!array_key_exists('children',$response[$subgroup])) $response[$subgroup]['children'] = array();
array_push($response[$subgroup]['children'],$group);
}
else // otherwise we set current group as parentgroup child
{
if (!array_key_exists('children',$response[$parentgroup])) $response[$parentgroup]['children'] = array();
array_push($response[$parentgroup]['children'],$group);
}
}
}
// We have finished setting children for each group. Time to deal with hosts
// We loop over the group membership list. We identify the hosts and set them
// to their respective groups
while ($row = pg_fetch_array($groupmembers, null, PGSQL_ASSOC)) {
$group = trim($row['groupname']);
$host = trim($row['hostname']);
if (!array_key_exists('hosts',$response[$group])) $response[$group]['hosts'] = array();
array_push($response[$group]['hosts'],$host);
}
// release resources and close connection to database
pg_free_result($groupmembers);
pg_free_result($groups);
pg_close($conn);
// encode the array as json send it back
echo json_encode($response);
?>
{
"group001": {
"hosts": ["host001", "host002"],
"vars": {
"var1": true
},
"children": ["group002"]
},
"group002": {
"hosts": ["host003","host004"],
"vars": {
"var2": 500
},
"children":[]
}
}
Keep in mind that you don't have to return the 'vars' and 'children' sections if you don't actually utilize them. Ansible will accept the data structure, without any complain, even with just the 'hosts' section.