Skip to content

Instantly share code, notes, and snippets.

@infernix
Created August 1, 2015 13:08
Show Gist options
  • Save infernix/1f2f2f4e60b8cc97f43b to your computer and use it in GitHub Desktop.
Save infernix/1f2f2f4e60b8cc97f43b to your computer and use it in GitHub Desktop.
Send one email with vars from a large play
- name: Generate log file name
hosts: all
connection: local
tasks:
- shell: mktemp
register: mktemp
always_run: yes
run_once: true
- name: Write entries to the log file
hosts: outdated
connection: local
serial: 1
tasks:
- lineinfile: dest={{ mktemp.stdout }} line="Host {{ inventory_hostname }} was updated at {{ ansible_date_time.date }} {{ ansible_date_time.time }} {{ ansible_date_time.tz }}" create=no state=present
always_run: yes
- name: Send email with update log
hosts: outdated
connection: local
tasks:
- stat: path={{ mktemp.stdout }}
register: updatelogcheck
always_run: yes
run_once: yes
- shell: cat {{ mktemp.stdout }}
register: mail_body
when: updatelogcheck.stat.exists == true
always_run: yes
run_once: yes
mail: subject='Update completed' body='{{ mail_body.stdout }}' from='you@yourdomain.com (Your Name)' to='Recipient <some@recipient.com>' cc='Other Recipient <other@recipient.com>'
when: updatelogcheck.stat.exists == true and mail_body.stdout != ""
run_once: yes
- file: path={{ mktemp.stdout }} state=absent
always_run: yes
run_once: yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment