Create initial admin user in automated script

Hello all,

my team is developing a system that, among other components, contains Keycloak. That system is rolled out on many individual bare-metal Windows servers. To make that manageable, we want to completely automate the rollout process. This process will transfer files from some repository to the target system, make necessary configurations, register Windows services and then start them.

For Keycloak, we need to create an admin user. Based on the Server Administration Guide, this is what we do:

  1. Start Keycloak process with environment variables KEYCLOAK_ADMIN and KEYCLOAK_ADMIN_PASSWORD
  2. Wait until process is running - either by checking log output or by trying to connect with kcadm
  3. Create realms, clients etc. using kcadm
  4. Stop Keycloak process
  5. Wait until process is stopped
  6. Register Keycloak as Windows service
  7. Start Windows service

This approach works, but it really feels clumsy. Especially step 2 (and to a lesser extent 4 and 5) requires some non-trivial coding. I wonder if there is not an easier approach, like a command that starts Keycloak, creates the intial admin account and directly stops Keycloak.

Does anyone of you have some ideas if and how this could be simplified?