notify-discord.yaml 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. ---
  2. # This Ansible playbook demonstrates how to send Discord notifications
  3. # using the `community.general.discord` module.
  4. # https://docs.ansible.com/ansible/latest/collections/community/general/discord_module.html
  5. #
  6. # If you need guidance how to create your own Discord server, see
  7. # https://support.discord.com/hc/en-us/articles/204849977-How-do-I-create-a-server
  8. #
  9. # In order to generate a webhook, please see
  10. # https://support.discord.com/hc/en-us/articles/360045093012-Server-Integrations-Page
  11. - name: notify discord
  12. hosts: "{{ my_hosts | d([]) }}"
  13. vars:
  14. # The name that will be shown as sender of the notification. Note
  15. # that some usernames are blocked by Discord, for example it must
  16. # not contain the word `discord`.
  17. notify_discord_username: Ansible
  18. # Your Discord webhook URL should have following format. Please
  19. # extract following segments of the URL path and set it as value of
  20. # the following variables:
  21. #
  22. # https://discord.com/api/webhooks/nnnnnnnnnn/xxxxxxxxxxxxxxxxxxxxxxxxxxx
  23. # | | | |
  24. # notify_discord_webhook_id <----'--------' | |
  25. # | |
  26. # notify_discord_webhook_token <------------'-------------------------'
  27. #
  28. # Security advise: if you commit this data to a repository it is
  29. # strongly recommended to encrypt `notify_discord_webhook_token` using
  30. # Ansible Vault.
  31. notify_discord_webhook_id: ''
  32. notify_discord_webhook_token: ''
  33. # Do not modify following regular expressions unless you know what
  34. # you're doing. Those are to ensure that whatever you've set as
  35. # `notify_discord_webhook_id` and `notify_discord_webhook_token`
  36. # complies with the Discord API Specification (as of 2024-02-25).
  37. #
  38. # https://github.com/discord/discord-api-spec/blob/fe9917381e47285b56d98cb72ae3cfe7db9ea19c/specs/openapi.json#L7524-L7531
  39. # https://github.com/discord/discord-api-spec/blob/fe9917381e47285b56d98cb72ae3cfe7db9ea19c/specs/openapi.json#L24817-L24821
  40. notify_discord_webhook_id_regex: '^0|[1-9][0-9]*$'
  41. # https://github.com/discord/discord-api-spec/blob/fe9917381e47285b56d98cb72ae3cfe7db9ea19c/specs/openapi.json#L7532-L7541
  42. notify_discord_webhook_token_regex: '^[a-zA-Z0-9_-]+$'
  43. # The content of the notification
  44. notify_discord_webhook_content: |-
  45. **Message from `{{ inventory_hostname }}` by *Ansible* ** :tada:
  46. Just a test, adjust it to your liking.
  47. You can use any Markdown formatting here [supported by Discord](
  48. https://support.discord.com/hc/en-us/articles/210298617-Markdown-Text-101-Chat-Formatting-Bold-Italic-Underline).
  49. # Delegate the sending of the Dicord notification to following host
  50. # which must be able to access the public internet on destination
  51. # port 443/tcp. When `localhost` is specified, this is sent from
  52. # the Ansible Controller, but you can pick any host listed in the
  53. # Ansible inventory.
  54. notify_discord_send_from_host: localhost
  55. tasks:
  56. - name: send discord message
  57. community.general.discord:
  58. username: "{{ notify_discord_username }}"
  59. webhook_id: "{{ notify_discord_webhook_id }}"
  60. webhook_token: "{{ notify_discord_webhook_token }}"
  61. content: "{{ notify_discord_webhook_content }}"
  62. delegate_to: "{{ notify_discord_send_from_host }}"
  63. when:
  64. - notify_discord_webhook_id is match(notify_discord_webhook_id_regex)
  65. - notify_discord_webhook_token is match(notify_discord_webhook_token_regex)
  66. - notify_discord_webhook_content | length > 0
  67. - notify_discord_send_from_host is in (['localhost'] + groups['all'])