token.yml 1.1 KB

123456789101112131415161718192021222324252627282930
  1. ---
  2. # Required variables:
  3. # rhbk_fqdn the FQDN of the Keycloak server (XXX will blow up without it)
  4. # rhbk.admin.username admin user (default "rhbk")
  5. # rhbk.admin.password admin password (default "secret")
  6. #
  7. # Registers (or refreshes) a fact called admin_token which you can use for auth.
  8. #
  9. - name: Get an auth token from Keycloak
  10. ansible.builtin.uri:
  11. method: POST
  12. return_content: yes
  13. validate_certs: no
  14. url: "https://{{ rhbk_fqdn }}/realms/master/protocol/openid-connect/token"
  15. headers:
  16. Accept: application/json
  17. body: "client_id=admin-cli&username={{ rhbk.admin.username | default('rhbk') }}&password={{ rhbk.admin.password | default('secret') }}&grant_type=password"
  18. register: sso_token_rsp
  19. - name: Verify that the token is usable.
  20. ansible.builtin.assert:
  21. that: sso_token_rsp.json is defined and sso_token_rsp.json.access_token is defined
  22. fail_msg: "ERROR: Failed to obtain authentication token from Keycloak."
  23. success_msg: "OK: got authentication token."
  24. - name: Store the token as a fact
  25. ansible.builtin.set_fact:
  26. admin_token: "{{ sso_token_rsp.json.access_token }}"
  27. ...