Error sending email using ansible via sendgrid - email

Through the ansible documentation page, I see ansible supporting sending email via sendgrid. I have tried following the code example but I still failed to send and exit the following message.
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] *********************************************************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************************************************************
ok: [localhost]
TASK [sendgrid] **********************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "parameters are mutually exclusive: api_key, username"}
to retry, use: --limit #/home/valutac/opreker/mail-test.retry
PLAY RECAP ***************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=1
Here I also display the contents of the playbook
---
- hosts: localhost
tasks:
- sendgrid:
username: "mail"
password: "xxx"
api_key: "SG.1414rfdsvefvwev.dsadasvef34r2-3e1qdqecweqc"
from_address: "ansible#mycompany.com"
to_addresses:
- "ops#mycompany.com"
subject: "Deployment success."
body: "The most recent Ansible deployment was successful."

The error msg is clear:
parameters are mutually exclusive: api_key, username
Doc explains:
username for logging into the SendGrid account.
Since 2.2 it is only required if api_key is not supplied.

Related

Ansible Playbooks connection to Rest api with win_uri

I'm trying to authenticate to a REST API with using NTLM authentication.
First I tried uri module but that didn't have support for NTLM. So I installed win_uri because looks like its supporting ntlm https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_uri_module.html#parameter-use_default_credential
However when I use win_uri. Somehow its trying to run commands on localhost using Powershell. I somehow need to seperate connecting to localhost and connecting to REST api part. But not sure how I can do this?
---
# playbooks/pki.yaml
- name: Playbook for cert automation
gather_facts: no
hosts: localhost
tasks:
- name: Get values
win_uri:
url: https://{{ api_url }}/SearchRequests
method: POST
src: body.json
body_format: json
validate_certs: no
url_username: {{ user }}
url_password: {{ password }}
return_content: true
And the result is
PLAY [Playbook for cert automation] ********************************************************************************************************************************
TASK [Get values] **********************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "module_stderr": "/bin/sh: 1: powershell: not found\n", "module_stdout": "", "msg": "The module failed to execute correctly, you probably need to set the interpreter.\nSee stdout/stderr for the exact error", "rc": 127}
PLAY RECAP *********************************************************************************************************************************************************
localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0

Jenkins not sending email "Got bad greeting from SMTP host"

I am trying to send an email from a Jenkins pipeline but I am getting the following error
Adding recipients from trigger recipient list
Successfully created MimeMessage
Sending email to: 1211732#isep.ipp.pt
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: need username and password for authentication
DEBUG SMTP: protocolConnect returning false, host=smtp.gmail.com, user=p-bri, password=<null>
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL false
DEBUG SMTP: EOF: [EOF]
DEBUG SMTP: got bad greeting from host "smtp.gmail.com", port: 465, response: [EOF]
MessagingException message: Got bad greeting from SMTP host: smtp.gmail.com, port: 465, response: [EOF]
I configured the SMTP with the following configurations
This is how I am sending the email
stage('Send confirmation email') {
emailext(
subject: "Pipeline notify - BUILD: '${env.JOB_NAME}#${env.BUILD_NUMBER}'",
body: """The pipeline runned successfully. The following tasks have been made:
- Project built
-Javadoc generation and publish
-Runned integration and mutation tests and published its respective reports
-Deployed the project to Tomcat, running at http://localhost:8888
Build can be found at: ${env.BUILD_URL}""",
recipientProviders: [[$class: 'DevelopersRecipientProvider']],
to: "1211732#isep.ipp.pt"
)
}
Try the following settings. This has worked for me.

KeyCloak fails to send email using SMTP with status 500

I have Keycloak running in a Kubernetes cluster. Authentication works but I need to set up e-mail to be able to send e-mails for verification and password reset.
I have SendGrid set up as an SMTP Relay. These settings (host, port and api key) work when I send mail using the SendGrid java client. However, when pressing Test connection in KeyCloak I get:
[Error] Failed to load resource: the server responded with a status of 500 ()
[Debug] Remove message (services.js, line 14)
[Debug] Added message (services.js, line 15)
[Error] Can't find variable: error
https://<domain>/auth/resources/ong8v/admin/keycloak/js/controllers/realm.js:76 – "Possibly unhandled rejection: {}"
[Debug] Remove message (services.js, line 14)
There isn't much to go on here. I have an e-mail address set up for the currently logged in user. I've also tried resetting the password in case the Test connection functionality was broken but that didn't work either.
The Realm Settings settings user for email are as such:
host: smtp.sendgrid.net
port: 587
from: test#<domain>
Enable StartTLS: true
Username: "apikey"
Password: <api key>
Any idea what can be wrong? Or how to find out? For instance, maybe I can get a more meaningful error message somehow.
Edit:
I got the server logs.
Failed to send email: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.sendgrid.net, 587; timeout 10000;
nested exception is: java.net.SocketTimeoutException: connect timed out
Edit 2:
I've tried sending mail using Telnet using the exact same settings and that works. So apparently it's something with Keycloak or its underlying Java libraries that's causing issues sending e-mail.
Turns out that Keycloak works and that emails were blocked by the hosting provider.

Ansible mail module not working with office 365

I am trying to send send an email from ansible
If I try it with gmail it works perfectly, However If I try with office 365 its not working.
Below is my Playbook.
---
- name: Mail Sendig using Ansible
hosts: localhost
tasks:
- name: Mail sending using Mail Module
mail:
host: "smtp.office365.com"
port: 587
username: "dcalert#mycompany.com"
password: "mypasswd"
to: "Jon Snow <jon.snow#mycompany.com>"
subject: "Ansible"
body: "Hello from Ansible"
secure: starttls
I am getting below error
ASK [Send email]
*******************************************************************
An exception occurred during task execution. To see the full
traceback, use -vvv. The error was: SMTPSenderRefused: (501, '5.1.7
Invalid address', 'root')
fatal: [localhost -> localhost]: FAILED! => {"changed": false,
"failed": true, "msg": "Failed to send mail to
jon.snow#mycompany.com: (501, '5.1.7 Invalid address',
'root')", "rc": 1}
You're missing fromparameter...
Take a look here: Ansible Mail Module
It says the parameter from defaults to root. Since you're not setting it, mail server says its invalid. Probably gmail doesnt handle it the same way as office365.
Give this a try...
---
- name: Mail Sendig using Ansible
hosts: localhost
tasks:
- name: Mail sending using Mail Module
mail:
host: "smtp.office365.com"
port: 587
username: "dcalert#mycompany.com"
password: "mypasswd"
from: "dcalert#mycompany.com"
to: "Jon Snow <jon.snow#mycompany.com>"
subject: "Ansible"
body: "Hello from Ansible"
secure: starttls

Grails mail Exchange server configuration

I'm trying to configure mail with an Exchange server.
This is my configuration
grails {
mail {
host = "mail.xxx.xx.xx"
port = 443
username = "username"
password = "password"
props = [ "mail.smtp.auth":"true",
"mail.smtp.port":"443",
"mail.smtp.ssl.enable" :"true"
]
}
}
I'm getting the following error
Message: org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Exception reading response;
nested exception is:
java.net.SocketException: Connection reset. Failed messages: javax.mail.MessagingException: Exception reading response;
I know it's an issue with the props that I have to change, since this configuration works on my mail app.
I know that the port is correct, it uses SSL.
I also tried using these props
props = ["mail.smtp.timeout" : "100000",
"mail.smtp.starttls.enable" : "true",
"mail.smtp.EnableSSL.enable" : "true",
"mail.transport.protocol" : "smtps",
"mail.smtp.socketFactory.port" : "443",
"mail.smtp.auth" : "true",
"mail.smtp.socketFactory.class" : "javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback": "false",
"mail.debug" : "true"]
The debug result it this :
DEBUG: JavaMail version 1.5.1
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "xxx.xxx.xx.xx", port 443, isSSL true
It looks to me as a certificate problem. If you have access to a server not using SSL, I would test that first, otherwise you should:
Find the certificate issuer (If you don't know it, you could use openssl verify http://www.herongyang.com/Cryptography/OpenSSL-Certificate-Path-Validation-Tests.html)
Look a CA in your java keystore
Locate Either the default java keystore (How do I find out what keystore my JVM is using?) or the one you are using
List the keystore and look for a corresponding CA
If there is a CA then the problem is not likely to be this
If there is no corresponding CA, then obtain the CA certificate and import it to you keystore, restart the server, and should work again.
I had the very same issue a while back. If you are trying to access an exchange server within a network (organization), I believe the port number is the issue here. It could have been changed by the network admin.
Please contact the network administrator and explain to him that you are trying to access the Exchange Server and require the username, password and the relevant port number. Authentication could be optional if the app is run in-house, i.e.
props = ["mail.smtp.auth":"false"]
I hope this helps.