0

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.

Jan Garaj
  • 25,598
  • 3
  • 38
  • 59
Taavi Ansper
  • 143
  • 9
  • 2
    => `{{ your_registered_var.stdout_lines[1:] | join('\n') }}` – Zeitounator Jun 09 '22 at 13:33
  • Does [Remove first line in 'stdout_lines'](https://stackoverflow.com/a/71249995/6771046) answer your question about [Accessing list elements](https://docs.ansible.com/ansible/latest/user_guide/complex_data_manipulation.html) with [Slice notation](https://stackoverflow.com/questions/509211/) (`[1:]`) ? – U880D Jun 09 '22 at 14:24
  • Thank you, this helped a lot, but now when I try to read it as from_json I get: the JSON object must be str, bytes or bytearray, not list – Taavi Ansper Jun 10 '22 at 08:10
  • nvm I can read the data from it nicely. – Taavi Ansper Jun 10 '22 at 08:12

0 Answers0