How Can I Escape Backspace Using Set_Fact In Ansible With Jinja Conditional



  • I cannot figure out how to escape an HTML closing tag, in this example, when used inside an Ansible Jinja conditional (ansible 2.9.27 in this case). Using an additional backslash does not work and neither does putting the backslash between single quotes . 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'/$/
    /'" 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 %} {{ hostvars[host]['ansible_hostname']|upper }}:
    {{ hostvars[host]['lslogins_output']['stdout'] }}

    {% elif hostvars[host]['lslogins_output']['stdout'] | length == 0 %} {{ hostvars[host]['ansible_hostname']|upper }}:
    NO ADMINS FOUND

    {% endif %} {% endfor %} run_once: true delegate_to: localhost # # - name: EMAIL USER LIST mail: to: Me from: Ansible subject: "Ansible Notification" subtype: html body: '

    {{ email_machine_list }}



    ' host: mail.foo.bar port: 587 secure: starttls password: mypassword username: me@foo.bar run_once: true become: false delegate_to: localhost

    The emails I receive from Ansible appear like this. The should not appear and should be interpreted as a closing HTML tag:

    HOST1:
    syslog
    user1
    

    HOST2:<\b>
    syslog
    user1

    HOST3:<\b>
    syslog
    user1



  • Put the template into a https://yaml.org/spec/1.2.2/#chapter-8-block-style-productions . For example,

        - set_fact:
            email_machine_list: |-
              {% for host in ansible_play_batch %}
              {{ hostvars[host]['ansible_hostname']|upper }}:
    {% endfor %}

    Example of a complete playbook for testing

    Given the inventory

    shell> cat hosts
    host_1 ansible_hostname=10.1.0.11
    host_2 ansible_hostname=10.1.0.12
    host_3 ansible_hostname=10.1.0.13
    

    The playbook

    - hosts: host_1,host_2,host_3
      gather_facts: false
      tasks:
        - set_fact:
            email_machine_list: |-
              {% for host in ansible_play_batch %}
              {{ hostvars[host]['ansible_hostname']|upper }}:
    {% endfor %} run_once: true - debug: var: test run_once: true

    gives (abridged)

    TASK [debug] *************************************************
    ok: [host_1] => 
      email_machine_list: |-
        10.1.0.11:
    10.1.0.12:
    10.1.0.13:

    Email

    Put the body of the email into the block as well. For example,

        - name: email the list
          mail:
            to: Me 
            from: Ansible 
            subject: "Ansible Notification"
            subtype: html
            body: |-
              
              
              
              

    {{ email_machine_list }}



    host: localhost port: 25 run_once: true delegate_to: localhost

    The email was delivered as expected. I had to turn off the rendering of HTML to text in the email client.

    Return-Path: 
    X-Original-To: admin@localhost
    Delivered-To: admin@localhost
    Received: from [127.0.0.1] (localhost [127.0.0.1])
     by test.example.org (Postfix) with ESMTPS id 3664C180493
     for ; Thu,  8 Sep 2022 23:48:00 +0200 (CEST)
    Content-Type: multipart/mixed; -charset="utf-8"; boundary="===============4465080980945319992=="
    MIME-Version: 1.0
    From: Ansible 
    Date: Thu, 08 Sep 2022 23:48:00 +0200
    Subject: Ansible Notification
    X-Mailer: Ansible mail module
    To: Me 
    Cc: 
    Message-Id: 
    
    
    
    

    10.1.0.11:
    10.1.0.12:
    10.1.0.13:



    Enabled rendering of HTML to text works as well

    10.1.0.11:
    10.1.0.12:
    10.1.0.13:
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2