I cannot figure out how to escape an HTML closing tag, <\b> in this example, when used inside an Ansible Jinja conditional (ansible 2.9.27 in this case). Using an additional backslash <\\b> does not work and neither does putting the backslash between single quotes <'\'b>. Anyone have an idea how I can make this work?
Here is my entire Playbook with credentials and email addresses redacted:
- name: GET ADMIN GROUP USERS AND COMMENT (GECOS)
ansible.builtin.shell: "lslogins --noheadings -g adm -o USER,GECOS|sed s'/$/<br>/'"
register: lslogins_output
#
#
- name: FORMAT LIST FOR HTML EMAIL
ansible.builtin.set_fact:
email_machine_list: |-
{% for host in ansible_play_batch %}
{% if hostvars[host]['lslogins_output']['stdout'] | length > 0 %}
<b>{{ hostvars[host]['ansible_hostname']|upper }}:<\b><br>
{{ hostvars[host]['lslogins_output']['stdout'] }}<br><br>
{% elif hostvars[host]['lslogins_output']['stdout'] | length == 0 %}
<b>{{ hostvars[host]['ansible_hostname']|upper }}:<\b><br>
NO ADMINS FOUND<br><br>
{% endif %}
{% endfor %}
run_once: true
delegate_to: localhost
#
#
- name: EMAIL USER LIST
mail:
to: Me <[email protected]>
from: Ansible <[email protected]>
subject: "Ansible Notification"
subtype: html
body: '<!DOCTYPE html>
<html>
<body>
<h3>{{ email_machine_list }}</h3><br><br>
</body>
</html>'
host: mail.foo.bar
port: 587
secure: starttls
password: mypassword
username: [email protected]
run_once: true
become: false
delegate_to: localhost
The emails I receive from Ansible appear like this. The <\b> should not appear and should be interpreted as a closing HTML tag:
HOST1:<\b>
syslog
user1
HOST2:<\b>
syslog
user1
HOST3:<\b>
syslog
user1