Deano
Deano

Reputation: 12190

Ansible lookup file variable

I'm trying to email a template file that has variable in html such as {{user_name}}, html renders correctly in my mail client, however {{user_name}} doesn't get resolved and displays as string {{user_name}}

- name: Send e-mail to a bunch of users, attaching files
  mail:
    host: mail.server.com
    subtype: html
    subject: email template
    body: '{{ lookup("file", "roles/binding/templates/email.j2") }}'
    to: "{{ user_email }}"

Example output

Hi {{ user_name }} ....

Desired output

Hi John Doe

Any ideas on how I can resolve this issue?

Upvotes: 1

Views: 4647

Answers (2)

techraf
techraf

Reputation: 68479

Simply replace the file lookup plugin with the template lookup plugin in the body parameter:

body: '{{ lookup("template", "roles/binding/templates/email.j2") }}'

Upvotes: 3

ilias-sp
ilias-sp

Reputation: 6685

you can add a template task to process the file, right before the task you have for sending the mail. Example:

- name: prepare mail body from template
  template:
    src: email.j2
    dest: /tmp/email.out

The variable replacement will take place in this task.

Then, you shall send tha mail with the task you have already prepared, but body will have to point to the /tmp/mail.out file.

template module documentation

Upvotes: 1

Related Questions