This commit is contained in:
pavel.muhortov 2023-02-18 09:22:41 +03:00
parent 50575f632e
commit b7e56c407c
16 changed files with 312 additions and 0 deletions

View File

@ -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*
```

1
defaults/main.yml Normal file
View File

@ -0,0 +1 @@
---

12
meta/main.yml Normal file
View File

@ -0,0 +1,12 @@
galaxy_info:
description: Install E-Mail/Telegram notification
platforms:
- name: Debian
versions:
- 11
- name: CentOS
versions:
- 7
galaxy_tags: []
dependencies: []

View File

@ -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

42
tasks/centos_units.yml Normal file
View File

@ -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

View File

@ -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"

View File

@ -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

20
tasks/debian_units.yml Normal file
View File

@ -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"

14
tasks/main.yml Normal file
View File

@ -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

View File

@ -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 &
)

View File

@ -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

View File

@ -0,0 +1,5 @@
smtp={{sendmail_smtp}}
port={{sendmail_port}}
from={{sendmail_from}}
pass={{sendmail_pass}}
dest={{sendmail_dest}}

View File

@ -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 &
)

View File

@ -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

View File

@ -0,0 +1,3 @@
APIKEY={{sendtelegram_apikey}}
APIURL=https://api.telegram.org/bot{{sendtelegram_apikey}}/sendMessage
CHATID={{sendtelegram_chatid}}

8
vars/main.yml Normal file
View File

@ -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