【问题标题】:Ansible lookup file variableAnsible 查找文件变量
【发布时间】:2018-05-07 18:45:37
【问题描述】:

我正在尝试通过电子邮件发送具有 html 变量的模板文件,例如 {{user_name}},html 在我的邮件客户端中正确呈现,但是 {{user_name}} 没有得到解析并显示为字符串 {{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 }}"

示例输出

Hi {{ user_name }} ....

期望的输出

Hi John Doe

关于如何解决此问题的任何想法?

【问题讨论】:

    标签: ansible jinja2


    【解决方案1】:

    只需将file 查找插件替换为body 参数中的template lookup plugin

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

    【讨论】:

      【解决方案2】:

      您可以在发送邮件的任务之前添加template 任务来处理文件。示例:

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

      变量替换将发生在这个任务中。

      然后,您应该发送带有您已经准备好的任务的邮件,但body 必须指向/tmp/mail.out 文件。

      template module documentation

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-11
        • 1970-01-01
        • 2021-07-16
        • 1970-01-01
        相关资源
        最近更新 更多