Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import a realm in Keycloak and exit

I have followed the Keycloak admin guide to export and import realms using standalone.sh it does work but it starts the server and does not exit.

This is a problem for me because I want to automate this process via executing an Ansible playbook and so I can't because the task never ends.

I found a workaround in Ansible by using async and wait_for but was hoping for a better way that does not require using the Admin REST API.

- name: Stop keycloak
  service:
    name: keycloak
    state: stopped
- name: Import realm into Keycloak
  shell: "{{keycloak_home}}/bin/standalone.sh -Dkeycloak.migration.action=import -Dkeycloak.migration.provider=dir -Dkeycloak.migration.dir=master -Dkeycloak.migration.usersExportStrategy=SAME_FILE -Dkeycloak.migration.realmName=master"
  async: 30
  poll: 0
- name: Wait for Keycloak to be started and listen on port 8080
  wait_for:
    host: 0.0.0.0
    port: 8080
    delay: 10
- name: Restart keycloak
  service:
    name: keycloak
    state: restarted
like image 423
Gaël Marziou Avatar asked Mar 15 '17 18:03

Gaël Marziou


People also ask

How do I import and export realm in Keycloak?

To export a realm, you can use the export command. Your Keycloak server instance must not be started when invoking this command. To export a realm to a directory, you can use the --dir <dir> option. When exporting realms to a directory, the server is going to create separate files for each realm being exported.

How do you remove a realm from a Keycloak?

Create a realm called “help” and Enable it. Now I want to delete that realm right after enabling it. I navigated to master realm and under clients the realm called “help”… Now I hit the deleted button on the far right of client called “help-realm”.


1 Answers

You can do it with Keycloak Admin CLI.

Authenticate once before running any command (see section Authenticating on the link above), for example:

$ kcadm.sh config credentials --server http://localhost:8080/auth --realm demorealm --user admin --password admin

Commands for full export/import example (see section Realm operations > Updating a realm):

$ kcadm.sh get realms/demorealm > demorealm.json
$ vi demorealm.json
$ kcadm.sh update realms/demorealm -f demorealm.json

Can do partial export/import as well using commands like: kcadm.sh create partial-export | partialImport ....

like image 113
cdan Avatar answered Sep 28 '22 09:09

cdan