diff --git a/README.md b/README.md index 193b8df..cc97b07 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,45 @@ # notification +Install E-Mail/Telegram notifications + +## Dependencies + +> - Python 3.9 or above on inventory hosts (in repo on Debian | from sources in /usr/local/bin on CentOS) + +## Download a Role +```bash +wget -qO- https://git.hmp.today/pavel.muhortov/notification/archive/master.tar.gz | tar -xvz -C . +``` + +## Edit Role Variables +```yaml +# nano ./notification/vars/main.yml +sendmail_smtp: smtp.host.zone +sendmail_port: 587 +sendmail_from: user@host.zone +sendmail_pass: pass +sendmail_dest: user@host.zone +sendtelegram_apikey: YOURAPIKEY +sendtelegram_chatid: 123456789 +``` + +## Create Playbook Example +```yaml +tee ./notification.yml > /dev/null <<'EOF' +- name: Install E-Mail/Telegram notification + hosts: all + become: yes + roles: + - notification +EOF +``` + +## Run Playbook +```bash +ansible-playbook ./notification.yml +``` + +## Clean +```bash +rm -rf ./notification* +``` diff --git a/defaults/main.yml b/defaults/main.yml new file mode 100644 index 0000000..73b314f --- /dev/null +++ b/defaults/main.yml @@ -0,0 +1 @@ +--- \ No newline at end of file diff --git a/meta/main.yml b/meta/main.yml new file mode 100644 index 0000000..273c988 --- /dev/null +++ b/meta/main.yml @@ -0,0 +1,12 @@ +galaxy_info: + description: Install E-Mail/Telegram notification + platforms: + - name: Debian + versions: + - 11 + - name: CentOS + versions: + - 7 + galaxy_tags: [] + +dependencies: [] \ No newline at end of file diff --git a/tasks/centos_dependencies.yml b/tasks/centos_dependencies.yml new file mode 100644 index 0000000..fc8752f --- /dev/null +++ b/tasks/centos_dependencies.yml @@ -0,0 +1,11 @@ +--- +- name: update repo + shell: yum check-update + args: + warn: false + +- name: install or update apps + shell: + cmd: "yum install -y wget curl python3" + args: + warn: false \ No newline at end of file diff --git a/tasks/centos_units.yml b/tasks/centos_units.yml new file mode 100644 index 0000000..459e461 --- /dev/null +++ b/tasks/centos_units.yml @@ -0,0 +1,42 @@ +--- +- name: create E-Mail reboot script + template: + src: templates/sendmail-reboot.sh + dest: /etc/init.d/sendmail-reboot.sh + mode: u=rwx,g=rx,o=rx + +- name: install E-Mail init script link + shell: + cmd: "chkconfig --add /etc/init.d/sendmail-reboot.sh" + +- name: create Telegram reboot script + template: + src: templates/sendtelegram-reboot.sh + dest: /etc/init.d/sendtelegram-reboot.sh + mode: u=rwx,g=rx,o=rx + +- name: install Telegram init script link + shell: + cmd: "chkconfig --add /etc/init.d/sendtelegram-reboot.sh" + +- name: check that the sendmail-reboot.sh exists + stat: + path: /etc/init.d/sendmail-reboot.sh + register: stat_result + +- name: replace python interpriter, if sendmail-reboot.sh exist + shell: sed -i "s/python3/\/usr\/local\/bin\/python3.9/" /etc/init.d/sendmail-reboot.sh + when: stat_result.stat.exists + args: + warn: false + +- name: check that the sendtelegram-login.sh exists + stat: + path: /etc/profile.d/sendtelegram-login.sh + register: stat_result + +- name: replace python interpriter, if sendtelegram-login.sh exist + shell: sed -i "s/python3/\/usr\/local\/bin\/python3.9/" /etc/profile.d/sendtelegram-login.sh + when: stat_result.stat.exists + args: + warn: false \ No newline at end of file diff --git a/tasks/configs-and-scripts.yml b/tasks/configs-and-scripts.yml new file mode 100644 index 0000000..cb571a6 --- /dev/null +++ b/tasks/configs-and-scripts.yml @@ -0,0 +1,34 @@ +--- +- name: download MTA + get_url: + url: https://git.hmp.today/pavel.muhortov/utils/raw/branch/master/sendmail.py + dest: /usr/local/bin/sendmail.py + mode: u=rwx,g=rx,o=rx + +- name: create config for MTA + template: + src: templates/sendmail.config + dest: /usr/local/bin/sendmail.config + +- name: check if Zimbra is installed + package_facts: + manager: "auto" + +- name: "'zimbra-core' check result" + debug: + msg: "'zimbra-core' found, logon-scripts will not copy" + when: "'zimbra-core' in ansible_facts.packages" + +- name: copy E-Mail logon script + template: + src: templates/sendmail-login.sh + dest: /etc/profile.d/sendmail-login.sh + mode: u=rwx,g=rx,o=rx + when: "'zimbra-core' not in ansible_facts.packages" + +- name: copy Telegram logon script + template: + src: templates/sendtelegram-login.sh + dest: /etc/profile.d/sendtelegram-login.sh + mode: u=rwx,g=rx,o=rx + when: "'zimbra-core' not in ansible_facts.packages" \ No newline at end of file diff --git a/tasks/debian_dependencies.yml b/tasks/debian_dependencies.yml new file mode 100644 index 0000000..919cdd0 --- /dev/null +++ b/tasks/debian_dependencies.yml @@ -0,0 +1,11 @@ +--- +- name: update repo + shell: apt update + args: + warn: false + +- name: install or update apps + shell: + cmd: "apt install -y wget curl python3" + args: + warn: false \ No newline at end of file diff --git a/tasks/debian_units.yml b/tasks/debian_units.yml new file mode 100644 index 0000000..e91a89a --- /dev/null +++ b/tasks/debian_units.yml @@ -0,0 +1,20 @@ +--- +- name: create E-Mail reboot script + template: + src: templates/sendmail-reboot.sh + dest: /etc/init.d/sendmail-reboot.sh + mode: u=rwx,g=rx,o=rx + +- name: install E-Mail init script link + shell: + cmd: "update-rc.d sendmail-reboot.sh defaults" + +- name: create Telegram reboot script + template: + src: templates/sendtelegram-reboot.sh + dest: /etc/init.d/sendtelegram-reboot.sh + mode: u=rwx,g=rx,o=rx + +- name: install Telegram init script link + shell: + cmd: "update-rc.d sendtelegram-reboot.sh defaults" \ No newline at end of file diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..16e1eb0 --- /dev/null +++ b/tasks/main.yml @@ -0,0 +1,14 @@ +--- +- include_tasks: centos_dependencies.yml + when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux' + +- include_tasks: centos_units.yml + when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux' + +- include_tasks: debian_dependencies.yml + when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu' + +- include_tasks: debian_units.yml + when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu' + +- include_tasks: configs-and-scripts.yml \ No newline at end of file diff --git a/templates/sendmail-login.sh b/templates/sendmail-login.sh new file mode 100644 index 0000000..433f0eb --- /dev/null +++ b/templates/sendmail-login.sh @@ -0,0 +1,14 @@ +#! /bin/sh + +( + python3 /usr/local/bin/sendmail.py \ + -u "$(cat /usr/local/bin/sendmail.config | grep "from=" | cut -d= -f2)" \ + -p "$(cat /usr/local/bin/sendmail.config | grep "pass=" | cut -d= -f2)" \ + -d "$(cat /usr/local/bin/sendmail.config | grep "dest=" | cut -d= -f2)" \ + --smtp "$(cat /usr/local/bin/sendmail.config | grep "smtp=" | cut -d= -f2)" \ + --port "$(cat /usr/local/bin/sendmail.config | grep "port=" | cut -d= -f2)" \ + --stls "True" \ + --subj "[Authorization] $(cat /etc/hostname): user logged in system" \ + --text "$(w)" \ + >> /dev/null 2>&1 & +) diff --git a/templates/sendmail-reboot.sh b/templates/sendmail-reboot.sh new file mode 100644 index 0000000..97eca9f --- /dev/null +++ b/templates/sendmail-reboot.sh @@ -0,0 +1,44 @@ +#! /bin/sh +# chkconfig: 2345 20 80 + +### BEGIN INIT INFO +# Provides: sendmail-reboot +# Required-Start: $all +# Required-Stop: +# Default-Start: 2 3 4 5 +# Default-Stop: +# Short-Description: Sending a message on startup +### END INIT INFO + +start() { + sleep 30 + python3 /usr/local/bin/sendmail.py \ + --smtp "$(cat /usr/local/bin/sendmail.config | grep "smtp=" | cut -d= -f2)" \ + -u "$(cat /usr/local/bin/sendmail.config | grep "from=" | cut -d= -f2)" \ + -p "$(cat /usr/local/bin/sendmail.config | grep "pass=" | cut -d= -f2)" \ + -d "$(cat /usr/local/bin/sendmail.config | grep "dest=" | cut -d= -f2)" \ + --subj "[System States] $(cat /etc/hostname): computer has been rebooted" \ + --text "$(w)" \ + >> /dev/null 2>&1 & +} + +stop() { + pkill -f /usr/local/bin/sendmail.py +} + +case "$1" in + start) + start + ;; + stop) + stop + ;; + restart) + stop + start + ;; + *) + echo "Usage: $0 {start|stop|restart}" +esac + +exit 0 \ No newline at end of file diff --git a/templates/sendmail.config b/templates/sendmail.config new file mode 100644 index 0000000..986c549 --- /dev/null +++ b/templates/sendmail.config @@ -0,0 +1,5 @@ +smtp={{sendmail_smtp}} +port={{sendmail_port}} +from={{sendmail_from}} +pass={{sendmail_pass}} +dest={{sendmail_dest}} \ No newline at end of file diff --git a/templates/sendtelegram-login.sh b/templates/sendtelegram-login.sh new file mode 100644 index 0000000..09cf560 --- /dev/null +++ b/templates/sendtelegram-login.sh @@ -0,0 +1,9 @@ +#! /bin/sh +APIURL="$(cat /usr/local/bin/sendtelegram.config | grep "APIURL=" | cut -d= -f2)" +CHATID="$(cat /usr/local/bin/sendtelegram.config | grep "CHATID=" | cut -d= -f2)" +TXTMSG="[Authorization] $(cat /etc/hostname): +$(w -h)" + +( + curl -s -X POST $APIURL -d chat_id=$CHATID -d text="$TXTMSG" >> /dev/null 2>&1 & +) diff --git a/templates/sendtelegram-reboot.sh b/templates/sendtelegram-reboot.sh new file mode 100644 index 0000000..7cf71b5 --- /dev/null +++ b/templates/sendtelegram-reboot.sh @@ -0,0 +1,41 @@ +#! /bin/sh +# chkconfig: 2345 20 80 + +### BEGIN INIT INFO +# Provides: sendtelegram-reboot +# Required-Start: $all +# Required-Stop: +# Default-Start: 2 3 4 5 +# Default-Stop: +# Short-Description: Sending a message on startup +### END INIT INFO + +start() { + APIURL="$(cat /usr/local/bin/sendtelegram.config | grep "APIURL=" | cut -d= -f2)" + CHATID="$(cat /usr/local/bin/sendtelegram.config | grep "CHATID=" | cut -d= -f2)" + TXTMSG="[System States] $(cat /etc/hostname): computer has been rebooted" + + sleep 15 + curl -s -X POST $APIURL -d chat_id=$CHATID -d text="$TXTMSG" >> /dev/null 2>&1 & +} + +stop() { + pkill -f curl -s -X POST $APIURL +} + +case "$1" in + start) + start + ;; + stop) + stop + ;; + restart) + stop + start + ;; + *) + echo "Usage: $0 {start|stop|restart}" +esac + +exit 0 diff --git a/templates/sendtelegram.config b/templates/sendtelegram.config new file mode 100644 index 0000000..6370ef7 --- /dev/null +++ b/templates/sendtelegram.config @@ -0,0 +1,3 @@ +APIKEY={{sendtelegram_apikey}} +APIURL=https://api.telegram.org/bot{{sendtelegram_apikey}}/sendMessage +CHATID={{sendtelegram_chatid}} \ No newline at end of file diff --git a/vars/main.yml b/vars/main.yml new file mode 100644 index 0000000..5d9e950 --- /dev/null +++ b/vars/main.yml @@ -0,0 +1,8 @@ +--- +sendmail_smtp: smtp.host.zone +sendmail_port: 587 +sendmail_from: user@host.zone +sendmail_pass: pass +sendmail_dest: user@host.zone +sendtelegram_apikey: YOURAPIKEY +sendtelegram_chatid: 123456789 \ No newline at end of file