diff --git a/playbooks/debug.yml b/playbooks/debug.yml new file mode 100644 index 0000000..9efe37e --- /dev/null +++ b/playbooks/debug.yml @@ -0,0 +1,35 @@ +- name: Gather facts from host and debug + hosts: all + gather_facts: yes + tasks: + + - name: Print ansible facts + tags: facts + debug: + var: ansible_facts + + - name: Print all variables + tags: vars + debug: + var: vars + + - name: Print variable value + tags: var + when: var_name is defined + debug: + msg: "{{ var_name }}: {{ lookup('vars', var_name, default='undefined') }}" + + - name: Check whether host is alive and reachable + tags: ping + ping: + + - name: Fetch or create passbolt password + tags: passbolt + debug: + msg: "{{ lookup(passbolt, 'test', password=passbolt_password) }}" + vars: + passbolt_password: 'S3cR3tP4$$w0rd' + environment: + PASSBOLT_CREATE_NEW_RESOURCE: true + PASSBOLT_NEW_RESOURCE_PASSWORD_LENGTH: 18 + PASSBOLT_NEW_RESOURCE_PASSWORD_SPECIAL_CHARS: false diff --git a/playbooks/facts.yml b/playbooks/facts.yml deleted file mode 100644 index 0ccd652..0000000 --- a/playbooks/facts.yml +++ /dev/null @@ -1,10 +0,0 @@ -- name: Gather facts from host - hosts: all - gather_facts: yes - tasks: - - name: Print all available facts - debug: - var: ansible_facts - - name: Print variable value - debug: - msg: "Variable: {{ ansible_fqdn }}" diff --git a/playbooks/passbolt.yml b/playbooks/passbolt.yml deleted file mode 100644 index 146a2b5..0000000 --- a/playbooks/passbolt.yml +++ /dev/null @@ -1,12 +0,0 @@ -- name: Fetch or create passbolt password - hosts: all - gather_facts: no - tasks: - - debug: - msg: "{{ lookup(passbolt, 'test', password=passbolt_password) }}" - vars: - passbolt_password: 'S3cR3tP4$$w0rd' - environment: - PASSBOLT_CREATE_NEW_RESOURCE: true - PASSBOLT_NEW_RESOURCE_PASSWORD_LENGTH: 18 - PASSBOLT_NEW_RESOURCE_PASSWORD_SPECIAL_CHARS: false diff --git a/playbooks/ping.yml b/playbooks/ping.yml deleted file mode 100644 index b7061eb..0000000 --- a/playbooks/ping.yml +++ /dev/null @@ -1,6 +0,0 @@ -- name: Check whether host is alive and reachable - hosts: all - gather_facts: no - become: no - tasks: - - ping: \ No newline at end of file diff --git a/roles/db/files/mariabackup/restore-backup.sh b/roles/db/files/mariabackup/restore-backup.sh index f8b612f..0e70424 100644 --- a/roles/db/files/mariabackup/restore-backup.sh +++ b/roles/db/files/mariabackup/restore-backup.sh @@ -6,6 +6,9 @@ myDir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" . "$myDir/config.sh" backupFile=$1 +formatted_date() { + date '+%Y-%m-%d %H:%M:%S' +} if [ -z "$backupFile" ]; then echo "Backup file not defined." @@ -22,6 +25,7 @@ echo "Restoring MySQL data from backup." rm -rf "$restoreDir" mkdir -p "$restoreDir" +echo "$(formatted_date)" echo "Decompresing backup." gzip --decompress --stdout "$backupFile" \ | mbstream -x --directory="$restoreDir" @@ -32,6 +36,7 @@ mariabackup \ --prepare \ --target-dir="$restoreDir" +echo "$(formatted_date)" echo "Stopping service." service mariadb stop if pgrep mariadbd; then pkill -9 mariadbd; fi diff --git a/roles/db/files/scheduler-log.cron b/roles/db/files/scheduler-log.cron new file mode 100644 index 0000000..c11daec --- /dev/null +++ b/roles/db/files/scheduler-log.cron @@ -0,0 +1 @@ +*/30 * * * * root /root/scripts/scheduler-log.sh diff --git a/roles/db/files/scripts/scheduler-log.sh b/roles/db/files/scripts/scheduler-log.sh index 9cb0b37..8cb0db0 100755 --- a/roles/db/files/scripts/scheduler-log.sh +++ b/roles/db/files/scripts/scheduler-log.sh @@ -3,7 +3,9 @@ set -e logFile="/var/log/mysql/error.log" dateFile="/tmp/mysql_scheduler_log-lastdate" -logTable="util.eventLog" +logSchema="util" +logTable="eventLog" +pattern='^\d{4}-\d{2}-\d{2}\s+\d{1,2}:\d{2}:\d{2}\s+\d+\s+\[ERROR\] Event Scheduler:' purgeDays=30 quote() { @@ -17,33 +19,55 @@ if [ "$?" -ne "0" ]; then exit fi -if [ -f "$dateFile" ]; then - fromDate=$(cat "$dateFile") -else - fromDate=0 +tableExists=$(mysql -Ns -e "SHOW TABLES FROM $logSchema LIKE '$logTable'") + +if [ -z "$tableExists" ]; then + mysql <<-EOF + CREATE SCHEMA IF NOT EXISTS $logSchema; + CREATE TABLE $logSchema.$logTable ( + id int(11) NOT NULL AUTO_INCREMENT, + date datetime NOT NULL, + event varchar(512) NOT NULL, + error varchar(1024) NOT NULL, + PRIMARY KEY (id), + KEY date (date) + ) ENGINE=InnoDB COMMENT='Event scheduler error log'; + EOF fi -lastDate=$(tail -n1 "$logFile" | awk '{print $1" "$2}') -toDate=$(date +%s -d "$lastDate") +if [ -f "$dateFile" ]; then + read -r fromDate < "$dateFile" +else + fromDate=$(date -d "-$purgeDays days" +%s) +fi -awk -v fromDate="$fromDate" -v toDate="$toDate" '{ +toDate=$(date +%s) + +grep -P "$pattern" "$logFile" | awk -v fromDate="$fromDate" -v toDate="$toDate" '{ split($1, date, "-"); split($2, time, ":"); timestamp = mktime(date[1]" "date[2]" "date[3]" "time[1]" "time[2]" "time[3]) - if (timestamp >= fromDate && timestamp < toDate && $4" "$5" "$6 == "[ERROR] Event Scheduler:") { + if (timestamp >= fromDate && timestamp < toDate) { printf $1" "$2" "$7; for (i=8; i<=NF; i++) printf FS $i ; print ""; } -}' "$logFile" | \ -\ +}' | \ while read line; do date="$(echo "$line" | cut -d' ' -f1,2)" event="$(echo "$line" | cut -d' ' -f3)" error="$(echo "$line" | cut -d' ' -f4-)" - echo "INSERT INTO $logTable (date, event, error)" \ - "VALUES ($(quote "$date"), $(quote "$event"), $(quote "$error"))" | mysql + + mysql <<-EOF + INSERT INTO $logSchema.$logTable SET + date = $(quote "$date"), + event = $(quote "$event"), + error = $(quote "$error") + EOF done -echo -n "$toDate" > "$dateFile" -echo "DELETE FROM $logTable WHERE date < TIMESTAMPADD(DAY, -$purgeDays, NOW())" | mysql +echo "$toDate" > "$dateFile" +mysql <<-EOF + DELETE FROM $logSchema.$logTable + WHERE date < TIMESTAMPADD(DAY, -$purgeDays, NOW()) +EOF diff --git a/roles/db/tasks/mariadb.yml b/roles/db/tasks/mariadb.yml index 5db0200..6569dac 100644 --- a/roles/db/tasks/mariadb.yml +++ b/roles/db/tasks/mariadb.yml @@ -45,8 +45,16 @@ - name: Set MariaDB Cron to /etc/cron.d template: - src: templates/cron_mariadb - dest: /etc/cron.d/vn + src: check-memory.cron + dest: /etc/cron.d/vn-check-memory + owner: root + group: root + mode: u=rw,g=r,o=r + +- name: Configure MariaDB scheduler log CRON + copy: + src: scheduler-log.cron + dest: /etc/cron.d/vn-scheduler-log owner: root group: root mode: u=rw,g=r,o=r diff --git a/roles/db/templates/cron_mariadb b/roles/db/templates/check-memory.cron similarity index 60% rename from roles/db/templates/cron_mariadb rename to roles/db/templates/check-memory.cron index bc281bd..a6cfa3e 100644 --- a/roles/db/templates/cron_mariadb +++ b/roles/db/templates/check-memory.cron @@ -1,4 +1,3 @@ MAILTO="{{ sysadmin_mail }}" */15 * * * * root /root/scripts/check-memory.sh -*/30 * * * * root /root/scripts/scheduler-log.sh diff --git a/roles/debian/defaults/main.yaml b/roles/debian/defaults/main.yaml index a750a38..740257e 100644 --- a/roles/debian/defaults/main.yaml +++ b/roles/debian/defaults/main.yaml @@ -1,6 +1,7 @@ vn_env: lab vn_first_time: false vn_witness_checked: false +send_test_email: true deb_packages: - https://apt.verdnatura.es/pool/main/v/vn-host/vn-apt-source_3.0.1_all.deb - https://apt.verdnatura.es/pool/main/v/vn-host/vn-host_3.0.1_all.deb diff --git a/roles/debian/files/profile.sh b/roles/debian/files/profile.sh index 5c2fc14..874e3f6 100644 --- a/roles/debian/files/profile.sh +++ b/roles/debian/files/profile.sh @@ -11,6 +11,8 @@ if [ -f "/etc/vn/env" ]; then fi read -r VN_ENV < /etc/vn/env + ENV_TEXT="$VN_ENV" + case "$VN_ENV" in lab) ENV_COLOR="\033[01;32m" @@ -26,17 +28,16 @@ if [ -f "/etc/vn/env" ]; then ;; *) ENV_COLOR="\033[01;36m" + ENV_TEXT="${VN_ENV:0:3}" ;; esac - ENV_TEXT=${VN_ENV^^} - if [ -z "$ENV_TEXT" ]; then ENV_TEXT="???" ENV_COLOR="\033[01;37m" fi - ENV_TEXT="\[${ENV_COLOR}\]${ENV_TEXT}\[\033[00m\]" + ENV_TEXT="\[${ENV_COLOR}\]${ENV_TEXT^^}\[\033[00m\]" PS1="\u@$SHORT_HOST[$ENV_TEXT]:\w" if [ "$(id -u)" -eq 0 ]; then diff --git a/roles/debian/tasks/relayhost.yml b/roles/debian/tasks/relayhost.yml index f912812..1aeaebd 100644 --- a/roles/debian/tasks/relayhost.yml +++ b/roles/debian/tasks/relayhost.yml @@ -27,7 +27,11 @@ - name: Force execution of handlers immediately meta: flush_handlers - name: Sending mail to verify relay host configuration works + when: > + exim_config.changed + and send_test_email + and awx_user_email is defined + and awx_user_email | length > 0 shell: > sleep 2; echo "If you see this message, relayhost on {{ ansible_fqdn }} has been configured correctly." \ - | mailx -s "Relayhost test for {{ ansible_fqdn }}" "{{ sysadmin_mail }}" - when: exim_config.changed + | mailx -s "Relayhost test for {{ ansible_fqdn }}" "{{ awx_user_email }}"