Matrix Docker Ansible eploy
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

82 linhas
4.0 KiB

  1. # SPDX-FileCopyrightText: 2026 Slavi Pantaleev
  2. #
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. ---
  5. # The management API endpoints are idempotent PUTs, so re-running is safe.
  6. # Meowlnir's own record of this bot, as fetched before the loop started.
  7. # Empty when the bot does not exist yet.
  8. - name: Look up what Meowlnir already knows about the bot - {{ bot.username | quote }}
  9. ansible.builtin.set_fact:
  10. matrix_bot_meowlnir_bot_live: >-
  11. {{
  12. (matrix_bot_meowlnir_live_bots | selectattr('username', 'equalto', bot.username) | list | first)
  13. | default({}, true)
  14. }}
  15. # `PUT /_meowlnir/v1/bot/…` answers 200 whether it created the bot, updated it, or did nothing at all, so the response cannot tell us whether anything changed.
  16. # Comparing against what Meowlnir already holds lets the request be skipped when it would be a no-op, which both avoids pointless calls and lets the task report an honest changed state.
  17. #
  18. # Note that this trusts Meowlnir's own record.
  19. # Changing a bot's profile directly through a Matrix client goes behind its back and will not be corrected here.
  20. - name: Determine whether the Meowlnir bot needs creating or updating - {{ bot.username | quote }}
  21. ansible.builtin.set_fact:
  22. matrix_bot_meowlnir_bot_needs_update: >-
  23. {{
  24. not matrix_bot_meowlnir_bot_live
  25. or (matrix_bot_meowlnir_bot_live.displayname | default('', true)) != bot.displayname
  26. or (matrix_bot_meowlnir_bot_live.avatar_url | default('', true)) != bot.avatar_url
  27. }}
  28. matrix_bot_meowlnir_bot_body: >-
  29. {{
  30. {
  31. 'displayname': bot.displayname,
  32. 'avatar_url': bot.avatar_url,
  33. }
  34. }}
  35. - name: Ensure Meowlnir bot exists and is up to date - {{ bot.username | quote }}
  36. when: matrix_bot_meowlnir_bot_needs_update | bool
  37. ansible.builtin.command:
  38. cmd: >-
  39. {{ matrix_bot_meowlnir_bin_path }}/meowlnir-api
  40. PUT /_meowlnir/v1/bot/{{ bot.username }}
  41. {{ matrix_bot_meowlnir_bot_body | to_json | quote }}
  42. register: matrix_bot_meowlnir_bot_create_result
  43. # The task only runs when something needs changing, so a successful call is reported as a change.
  44. # Deriving this from the status keeps a failed call from also claiming to have changed anything.
  45. # Meowlnir answers 200 even when it could not apply the displayname or avatar (it only logs that), so this is what was asked for, not proof of what landed. A later run retries, since its stored record is unchanged.
  46. changed_when: "matrix_bot_meowlnir_bot_create_result.stdout_lines | default([]) | last | default('') == '200'"
  47. failed_when: false
  48. - name: Fail if the Meowlnir bot could not be created - {{ bot.username | quote }}
  49. ansible.builtin.fail:
  50. msg: >-
  51. Creating the Meowlnir bot `{{ bot.username }}` failed.
  52. Meowlnir said: {{ matrix_bot_meowlnir_bot_create_result.stdout | default('') | trim }}
  53. {{ matrix_bot_meowlnir_bot_create_result.stderr | default('') | trim }}
  54. when: >-
  55. matrix_bot_meowlnir_bot_needs_update | bool
  56. and (
  57. matrix_bot_meowlnir_bot_create_result.rc | default(1) != 0
  58. or (matrix_bot_meowlnir_bot_create_result.stdout_lines | default([]) | length == 0)
  59. or (matrix_bot_meowlnir_bot_create_result.stdout_lines | last != '200')
  60. )
  61. - name: Determine which management rooms Meowlnir already has for this bot - {{ bot.username | quote }}
  62. ansible.builtin.set_fact:
  63. matrix_bot_meowlnir_bot_live_rooms: >-
  64. {{ matrix_bot_meowlnir_bot_live.management_rooms | default([], true) | map(attribute='room_id') | list }}
  65. - name: Ensure Meowlnir management room created - {{ bot.username | quote }}
  66. when: "bot.management_room_auto_create | bool and matrix_bot_meowlnir_bot_live_rooms | length == 0"
  67. ansible.builtin.include_tasks: "{{ role_path }}/tasks/util/ensure_management_room_created.yml"
  68. - name: Ensure declared Meowlnir management rooms registered - {{ bot.username | quote }}
  69. ansible.builtin.include_tasks: "{{ role_path }}/tasks/util/ensure_management_room_registered.yml"
  70. with_items: "{{ bot.management_rooms }}"
  71. loop_control:
  72. loop_var: management_room