从ansible发送电子邮件,邮件不是Play的有效属性

时间:2019-05-13 22:40:18

标签: ansible

我正在尝试遵循本教程:

https://www.infinitypp.com/ansible/email-notifications-with-examples

我从中构建了以下名为test.yml的剧本,其中包含以下代码:

---
  - name: sending an email
    hosts: localhost
    tasks:
     - name: send email
       local_action: mail
   subject="ansible sent this"
       to="my name <myemail.example.com>"
       body="this is the body"

但是我收到此错误:

ERROR! 'mail' is not a valid attribute for a Play

The error appears to have been in '/path/test.yml': line 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:


- name: Send email
  ^ here

我想知道我在做什么错

2 个答案:

答案 0 :(得分:0)

您的Yaml格式错误,正确的剧本是:

---
  - name: sending an email
    hosts: localhost
    tasks:
     - name: send email
       local_action:
          module: mail
          subject: "ansible sent this"
          to: "my name <myemail.example.com>"
          body: "this is the body"

使用local_action时,应使用键module指定模块。另外,对于键/值对,您应该使用:,而不是=

答案 1 :(得分:0)

  1. Your yaml is not correct。更具体地说,您有缩进问题。
  2. 您错误地将旧样式(在字符串中带有“ =”的参数)和当前样式(完整yaml)模块声明混合在一起。我强烈建议您停止使用旧样式声明,而仅使用yaml语法。您将节省时间和精力。
  3. 尽管我在尝试运行您的代码时遇到了完全不同的错误,但仅前两点可能解释了您所得到的错误。为了将来,您应该始终在yamllint中运行剧本,并在搜索实施问题之前更正报告的错误。
  4. 您正在将local_action用于播放目标为localhost的主机,这是多余的。这不是错误,而只会增加混乱。如果您确实需要localhost委派(因为您打算在播放过程中以其他主机作为目标,同时仍从ansible控件发送邮件),请查看delegate_to选项,该选项会产生更加易读的IMO任务(即{{1 }})

这是一本更正后的剧本,可让您步入正轨。我保留了本地代理的示例。如果您不使用它,只需删除它。

delegate_to: localhost