首页 文章

Ansible smtp邮件发送

提问于
浏览
-2

我正试图从Ansible发送邮件 .

- hosts: all
  tasks:
  - mail:
      host: smtp.gmail.com

      port: 587

      username: ****@gmail.com

      password: *******

      to: John Smith ***@gmail.com

      subject: 'Ansible-report'

      body: 'System  has been successfully provisioned.'

    delegate_to: localhost

但是我收到了一个错误

致命:[host1 - > localhost]:失败! => {“已更改”:false,“failed”:true,“msg”:“身份验证到smtp.gmail.com:587失败,请检查您的用户名和/或密码”,“rc”:1}

我使用官方网站上的例子http://docs.ansible.com/ansible/latest/mail_module.html

据我了解,源验证模块ANSIBLE中已存在SMTP身份验证 . 在主smtp.login中输入文件“/tmp/ansible_K94pcN/ansible_module_mail.py”,第304行(用户名,密码)

文件“/usr/lib64/python2.7/smtplib.py”,第621行,在登录时引发SMTPAuthenticationError(代码,resp)

致命:[host1 - > localhost]:失败! => {“已更改”:false,

"failed": true,

"invocation": {

    "module_args": {

        "attach": null,
        "bcc": null,
        "body": "System ubuntu has been successfully provisioned.",
        "cc": null,
        "charset": "us-ascii",
        "headers": null,
        "host": "smtp.gmail.com",
        "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
        "port": 587,
        "secure": "starttls",
        "sender": "root",
        "subject": "Ansible-report",
        "subtype": "plain",
        "timeout": 20,
        "to": "Cristiano Ross <cristianooross@gmail.com>",
        "username": "victorradin9@gmail.com"
    }
},
"msg": "Authentication to smtp.gmail.com:587 failed, please check your usern              ame and/or password",
"rc": 1

1 回答

  • 0

    您面临的问题是使用正确的用户名和密码从Gmail帐户发送电子邮件而未分配应用专用密码而导致的身份验证问题 .

    你需要:

    a)为发件人Gmail帐户设置“2步验证” .

    b)然后,您需要在Google帐户设置中使用create an app specific password .

    使用新生成的应用专用密码,您将能够像下面的示例一样发送邮件

    - mail:
         host: smtp.gmail.com
         port: 587
         secure: starttls
         charset: utf-8
         sender: victorradin9@gmail.com
         username: victorradin9@gmail.com
         password: your app password generated by google for you goes here
         to: 'cristianooross@gmail.com'
         subject: Ya u rite
    

相关问题