使用community.general.mail Ansible模块发送邮件通知报错求助
解决Ansible mail模块Kerberos认证失败问题
出现kerberos: authGSSClientStep() failed错误,是因为community.general.mail模块默认尝试使用Kerberos(GSSAPI)认证SMTP服务器,但环境配置不全或不需要该认证方式导致失败。以下是具体解决方法:
方法1:禁用Kerberos,使用普通SMTP认证
直接在mail模块中配置SMTP服务器的基础认证参数,强制使用plain/login认证方式,绕过Kerberos:
修改后的Playbook示例
--- - hosts: win_server connection: local tasks: - name: Create txt file win_copy: content: "hello, i am user 1, i am trying to create a new text file" dest: C:\Users\username\test.txt - name: send mail community.general.mail: subject: email notification. sender: your-sender@example.com # 发件人邮箱 to: target-user@example.com # 收件人邮箱 body: "Test email sent from Ansible playbook" # 邮件内容 smtp_server: smtp.example.com # 你的SMTP服务器地址 smtp_port: 587 # SMTP端口(通常587对应starttls,465对应ssl) smtp_auth: true smtp_secure: starttls # 根据SMTP服务器要求选starttls或ssl username: your-smtp-account@example.com # SMTP认证账号 password: your-smtp-password # SMTP认证密码 auth_type: plain # 指定认证方式为plain,避免触发Kerberos delegate_to: localhost
方法2:配置Kerberos认证(若必须使用)
如果你的SMTP服务器强制要求Kerberos认证,需完成以下配置:
安装Kerberos依赖(以Linux Ansible节点为例):
sudo apt-get install krb5-user libkrb5-dev python3-kerberos # Debian/Ubuntu sudo yum install krb5-workstation krb5-devel python3-kerberos # RHEL/CentOS配置krb5.conf:
编辑/etc/krb5.conf,设置正确的KDC和Realm信息,示例:[libdefaults] default_realm = EXAMPLE.COM dns_lookup_kdc = true dns_lookup_realm = true [realms] EXAMPLE.COM = { kdc = kdc.example.com admin_server = kdc.example.com }获取Kerberos票据:
在Ansible节点执行命令获取有效票据:kinit your-kerberos-account@EXAMPLE.COM修改mail模块参数:
在Playbook的mail模块中指定auth_type: gssapi,并确保SMTP服务器支持Kerberos认证:- name: send mail community.general.mail: subject: email notification. sender: your-sender@example.com to: target-user@example.com smtp_server: smtp.example.com auth_type: gssapi delegate_to: localhost
额外检查
- 确保已安装
community.general集合:ansible-galaxy collection install community.general - 验证SMTP服务器的端口、认证方式是否正确,可通过telnet或nc测试连通性:
nc -zv smtp.example.com 587
内容的提问来源于stack exchange,提问作者Jahanvi J
相关产品推荐
相关产品推荐

