I am trying to setup keycloak and add conf options using keycloak kcadm.sh .
The problem is after I create a realm and rerun the playbook the command gives me an error.
Now I am trying to fix it by checking if the realm exists, or whatever conf option for later use case.
- name: Run scripts in the keycloak container
command:
cmd: "/usr/local/bin/docker-compose exec {{ docker_compose_keycloak_container_name }} /opt/keycloak/bin/kcadm.sh {{ command_to_execute.command }} --no-config --server {{ docker_compose_keycloak_server_url }} --realm {{ c ommand_to_execute.realm }} --user {{ docker_compose_keycloak_admin }} --password {{ docker_compose_keycloak_admin_password }}"
chdir: /opt/keycloak
loop: "{{ docker_compose_keycloak_initalization_scripts }}"
loop_control:
loop_var: command_to_execute
Now the first time it creates it nicely.
But the second time I get the following response:
TASK [docker_compose_keycloak : Run scripts in the keycloak container] ***********************************************************************************************************************************************************
failed: [REDACTED] (item={'command': 'create realms -s realm=REDACTED -s enabled=true', 'realm': 'master'}) => {"ansible_loop_var": "command_to_execute", "changed": true, "cmd": ["/usr/local/bin/docker-compose", "exec", "keycloak", "/opt/keycloak/bin/kcadm.sh", "create", "realms", "-s", "realm=REDACTED", "-s", "enabled=true", "--no-config", "--server", "http://localhost:8080", "--realm", "master", "--user", "admin", "--password", "REDACTED"], "command_to_execute": {"command": "create realms -s realm=REDACTED -s enabled=true", "realm": "master"}, "delta": "0:00:02.240996", "end": "2022-06-09 16:02:26.160370", "msg": "non-zero return code", "rc": 1, "start": "2022-06-09 16:02:23.919374", "stderr": "", "stderr_lines": [], "stdout": "Logging into http://localhost:8080 as user admin of realm master\r\nConflict detected. See logs for details", "stdout_lines": ["Logging into http://localhost:8080 as user admin of realm master", "Conflict detected. See logs for details"]}
Now my fix would be to query the realms with get realms
, but that output contains a weird format of stdout.
Logging into http://localhost:8080 as user admin of realm master
{
"id" : "e1f370e3-9d7f-4faf-8847-b4d8e6fc589b",
"realm" : "REDACTED",
...
}
My question is how can I remove the first line of output from the response using ansible?
Then I can try to parse it as json and read the option 'id' and check it against the variables i have set.