02 / FILES, ACCOUNTS AND SERVICES · LESSON 08

Propose service changes from an export

Compare host-specific service states and keep missing services out of automatic change proposals.

15 min · learn at your pace

The key includes the host

Two servers can have services with the same name and different requirements. Join on the pair (host, service) rather than service name alone. Normalize both identifiers for this exercise, then compare the supplied status and startup fields.

Build separate indexes for observations and desired policy. Duplicate pairs in either source make the comparison ambiguous, so reject them. Only services named in the desired policy are in scope; an extra observed service is not automatically an error.

A proposal is not a command

For a known service that differs, return a change proposal carrying the before and after state. If no observation exists, return a review proposal with a None before value. Missing evidence does not establish that a service should be created, started or installed.

Omit already compliant pairs and sort the rest by host and service. Keep the two state fields together so an administrator can see whether startup configuration, running state, or both differ. Preserve the input lists for any later comparison.

This browser lesson uses exports and returns data only. It does not call a service manager, simulate a live Windows process table, or send a remote command. Those distinctions matter when taking a reporting tool into an authorised environment: collection, planning and execution are separate responsibilities.

Try an example.py
key = (" SRV-01 ".strip().casefold(), " BackupAgent ".strip().casefold())
observed = {key: {"status":"stopped", "startup":"automatic"}}
print(observed[key])
YOUR TURN

Put it into practice.

Define service_plan(observed, desired). Rows have host,service,status,startup with validated string values. Index exact normalized (host,service) pairs using strip().casefold(); duplicates in either input raise ValueError. For desired pairs only, omit equal status/startup; otherwise return host,service,action,before,after. Before/after contain status and startup. Use action change for an observed mismatch, review with before None for a missing pair. Sort by host then service. Do not mutate inputs or manage services.

See an explained solution
One way to solve it.py
def service_plan(observed, desired):
    def index(rows):
        result = {}
        for row in rows:
            key = (row["host"].strip().casefold(), row["service"].strip().casefold())
            if key in result:
                raise ValueError("Duplicate host/service pair")
            result[key] = {"status":row["status"], "startup":row["startup"]}
        return result
    current, policy = index(observed), index(desired)
    result = []
    for (host, service), after in sorted(policy.items()):
        before = current.get((host, service))
        if before == after:
            continue
        result.append({"host":host, "service":service, "action":"review" if before is None else "change", "before":before, "after":after})
    return result

if __name__ == "__main__":
    print(service_plan([], [{"host":"srv-01", "service":"agent", "status":"running", "startup":"automatic"}]))

The composite key prevents one host’s state leaking into another host’s plan. Missing observations remain reviews, and existing compliant services generate no proposed change.