Hi I have ansible playbook which sends email with attachment. but email body does not come in format all sentences comes in single line and very small font size.
Here is playbook -
- name: Sending an e-mail using the remote machine
mail:
host: localhost
port: 25
from:
to:
subject: Reports
body: Hi,
Hope you are doing well.
Thanks,
Megha
attach:
- file.csv
- file.scv
This gives me output -
Hi,Hope you are doing well.Thanks,Megha
How can I have email body with line breaks and big font size?
You can use html using the parameter subtype: html and your body must have html
- name: Sending an e-mail using the remote machine
mail:
host: localhost
port: 25
from:
to:
subject: Reports
subtype: html
body:
<h1>Hi,</h1>
<p style="color:red">Hope you are doing well.</p>
<strong>Thanks,</strong>
<p style="color:green">Megha</p>
attach:
- file.csv
- file.scv
Related
I am trying to attach multiple files in single mail. I have to attach my host logs within this mail, which gets generated dynamically. As off now I have 2 hosts. Dynamic files are generated in /ansible_log/10.0.0.1_log.txt & /ansible_log/10.0.0.2_log.txt (and so on). Here I can send mail, below is the script:
Inventory File:
[logs]
x.x.x.1
x.x.x.2
- name: Send e-mail to users, attaching report
mail:
host: x.x.x.x
port: xx
to: "{{ mailid }}"
subject: Server Logs
body: Please find attached logs.
attach:
- /ansible_log/{{ item }}_log.txt
delegate_to: localhost
with_items: "{{ inventory_hostname }}"
run_once: True
tags: send_mail
Here I want to send a mail which attaches both log files in a single mail. If I remove run_once: True then it sends two seperate mails with 2 host log files. If the inventory list grows, then these log files mails will bombard user mail box. To avoid this I want to consolidate all the log files in a single mail and to the recipient as a bunch.
You cannot use a loop there. You must give it a prebuild list of files.
You can use a setfact task to build a list of files before running this task.
Regarding monthly patching, we currently have an pre_task mail notification sent out prior to the updates being installed but due to the {{ inventory_hostname }} being included in the body of the email, this is sent out on a per server basis.
Is there a way to replace the inventory_hostname in the body to reference all servers within the hosts group (in this case groupOne) so that one email is sent out with all hostnames rather than individual emails?
hosts: groupOne
become: true
any_errors_fatal: true
pre_tasks:
name: Notification email of patching beginning
mail:
host: XXXXXXXXXXXXXXXXXXXXX.com
port: XXXXXX
to: hello#gmail.com
sender: patching#gmail.com
subject: Patching_Notification
body: "Monthly patching is about to commence for {{ inventory_hostname }}. A further email will be sent on completion"
You can use the group_names variable to get the group name(s) of your host.
https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html#magic
You probably want to run your pre_tasks notification only once with run_once: yes.
so I'm using this simple playbook as an example:
- name: Show interfaces
junos_command:
commands:
- show interfaces
display: text
register: json_response
And I need to save json_response to a file:
- name: Saving logs to output
copy:
content: "{{ json_response.stdout }}"
dest: "./output.txt"
I know that json_response.stdout_lines has the real organized json, but when I save it, it comes ALL unindented, and if I use "json_response.stdout" it comes "indented" but he doesn't recognize '\n' as a breakline character, so I execute another task to replace \n to breakline. My problem here is, anyway I can save json_response variable in a correctly way? When I execute the playbook the debug var prints perfectly indented on my shell, but doesn't work for my output file.
Thanks.
You can try one of the formatting filters, e.g. like this:
- name: Saving logs to output
copy:
content: "{{ json_response.stdout | to_nice_json(indent=4) }}"
dest: "./output.txt"
More details and examples can be found in the Ansible Documentation.
I'm trying to email a template file that has variable in html such as {{user_name}}, html renders correctly in my mail client, however {{user_name}} doesn't get resolved and displays as string {{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 }}"
Example output
Hi {{ user_name }} ....
Desired output
Hi John Doe
Any ideas on how I can resolve this issue?
Simply replace the file lookup plugin with the template lookup plugin in the body parameter:
body: '{{ lookup("template", "roles/binding/templates/email.j2") }}'
you can add a template task to process the file, right before the task you have for sending the mail. Example:
- name: prepare mail body from template
template:
src: email.j2
dest: /tmp/email.out
The variable replacement will take place in this task.
Then, you shall send tha mail with the task you have already prepared, but body will have to point to the /tmp/mail.out file.
template module documentation
I have to do a POST request with ansible.
My hosts.ini file is:
[workers]
worker1 ansible_host=111.111.111.111
worker2 ansible_host=222.222.222.222
The url I have to connect to needs the ip address of worker1, so I wrote my playbook as:
- hosts: worker1
tasks:
- name: inizialize worker
uri:
url: "http://{{ worker1 }}:8080/xxx/yyy"
method: POST
user: admin
password: password
force_basic_auth: yes
return_content: yes
body: "field=myfield"
But running it, I get:
the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'worker1' is undefined
Where is the problem?
You don't have a variable named worker1 defined.
If you wanted to replace the value with 111.111.111.111, you should use magic variables:
url: "http://{{ hostvars['worker1']['ansible_host'] }}:8080/xxx/yyy"
but considering your whole play, you might as well wanted:
url: "http://{{ ansible_host }}:8080/xxx/yyy"
Mind that your inventory file defines a host group named workers and your play refers to a host group named worker1 -- this makes no sense altogether.