Depending on what you're configuring, you might have to configure YAML Variables inside of text strings that should not be templated at runtime. There's a solution for templates and for tasks.
Let's start with templates, there's '{% raw %}' and '{% endraw %}'. Here's an example:
{% raw %}
a string that won't be templated
and contains a {{ var }}
{% endraw %}
This will result in the following file content:
a string that won't be templated
and contains a {{ var }}
Now for playbooks, you can use '!unsafe' in front of the line of text. This will make ansible ignore the jinja variable definition and treat it as text.
Now to the problem that spawned this post: creating a mail notification with custom messages in AWX.
Here's an example playbook:
---
- hosts: localhost
become: false
delegate_to: localhost
collections:
- awx.awx
tasks:
- name: create mail notification
awx.awx.tower_notification:
name: admin mail notification
state: present
organization: example
notification_type: email
notification_configuration:
sender: ansible@example.com
recipients:
- group1@example.com
- group2@example.com
- user1@example.com
- user2@example.com
username: 'my_secure_username'
password: 'my_secure_password'
host: mail.example.com
port: 587
use_tls: true
use_ssl: false
messages:
started:
message: !unsafe "'{{ job.name }}' - started"
body: !unsafe "'{{ job.name }}' (#{{ job.id }}) has been started.\nMore details can be found here:\n{{ url }}"
success:
message: !unsafe "'{{ job.name }}' - succeded"
body: !unsafe "'{{ job.name }}' (#{{ job.id }}) has succeeded.\nInformation:\njob name: {{ job.name }}\nstarted at: {{ job.started }}\nfinished at: {{ job.finished }}\nstatus: {{ job.status }}\n\nMore details can be found here:\n{{ url }}"
error:
message: !unsafe "'{{ job.name }}' - failed"
body: !unsafe "'{{ job.name }}' (#{{ job.id }}) has failed.\nInformation:\njob name: {{ job.name }}\nstarted at: {{ job.started }}\nfinished at: {{ job.finished }}\nstatus: {{ job.status }}\nMore details can be found here:\n{{ url }}"
tower_config_file: ~/.tower_cli.cfg
...
Feel free to comment and / or suggest a topic.
Comments
Post a Comment