Skip to content

Commit

Permalink
Better variable handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Respheal committed Nov 19, 2020
1 parent 2820b92 commit fad9eea
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 34 deletions.
10 changes: 5 additions & 5 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

monit_config_dir: "/etc/monit.d/"
monitored_services:
- httpd
- mariadb
- nginx
- php-fpm
- redis
- { var_name: 'apache_daemon', daemon_name: 'httpd' }
- { var_name: 'mysql_daemon', daemon_name: 'mariadb' }
- { var_name: 'nginx_daemon', daemon_name: 'nginx' }
- { var_name: 'php_fpm_daemon', daemon_name: 'php-fpm' }
- { var_name: 'redis_daemon', daemon_name: 'redis' }
11 changes: 3 additions & 8 deletions tasks/facts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,11 @@
or monitored_services == 0
# Remove service from list of monitored_services if not part of playbook
- name: Check monitored services
- name: Remove nonexistent services
set_fact:
monitored_services: "{{ monitored_services | difference([item.daemon_name]) }}"
monitored_services: "{{ monitored_services | difference([item]) }}"
when: >-
item.var_name not in vars
or lookup('vars', item.var_name) is not string
or lookup('vars', item.var_name) == 0
loop:
- { var_name: 'apache_daemon', daemon_name: 'httpd' }
- { var_name: 'mysql_daemon', daemon_name: 'mariadb' }
- { var_name: 'nginx_daemon', daemon_name: 'nginx' }
- { var_name: 'php_fpm_daemon', daemon_name: 'php-fpm' }
- { var_name: 'redis_daemon', daemon_name: 'redis' }
loop: "{{ monitored_services }}"
4 changes: 2 additions & 2 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@

- name: Install service configs
template:
src: services/{{ item }}
dest: "{{ monit_config_dir }}/{{ item }}"
src: services/{{ item.daemon_name }}
dest: "{{ monit_config_dir }}/{{ item.daemon_name }}"
owner: root
group: root
mode: "0644"
Expand Down
10 changes: 5 additions & 5 deletions templates/services/httpd
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# {{ template_destpath }}
# {{ ansible_managed }}

check process apache with pidfile /var/run/httpd/httpd.pid
check process apache with pidfile /var/run/{{ apache_daemon }}/{{ apache_daemon }}.pid
group root
start program = "/bin/systemctl start httpd"
stop program = "/bin/systemctl stop httpd"
if failed port 8080 for 2 cycles then restart
if failed port 8443 for 2 cycles then restart
start program = "/bin/systemctl start {{ apache_daemon }}"
stop program = "/bin/systemctl stop {{ apache_daemon }}"
if failed port {{ apache_port_http }} for 2 cycles then restart
if failed port {{ apache_port_https }} for 2 cycles then restart
6 changes: 3 additions & 3 deletions templates/services/mariadb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# {{ template_destpath }}
# {{ ansible_managed }}

check process mariadb with pidfile /var/run/mariadb/mariadb.pid
check process mariadb with pidfile /var/run/{{ mysql_daemon }}//{{ mysql_daemon }}.pid
group mysql
start program = "/bin/systemctl start mariadb"
stop program = "/bin/systemctl stop mariadb"
start program = "/bin/systemctl start {{ mysql_daemon }}"
stop program = "/bin/systemctl stop {{ mysql_daemon }}"
if failed port 3306 protocol mysql timeout 15 seconds then restart
6 changes: 3 additions & 3 deletions templates/services/nginx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# {{ template_destpath }}
# {{ ansible_managed }}

check process nginx with pidfile /var/run/nginx.pid
check process nginx with pidfile /var/run/{{ nginx_daemon }}.pid
group nginx
start program = "/bin/systemctl start nginx"
stop program = "/bin/systemctl start nginx"
start program = "/bin/systemctl start {{ nginx_daemon }}"
stop program = "/bin/systemctl start {{ nginx_daemon }}"
if failed port 80 for 2 cycles then restart
if failed port 443 for 2 cycles then restart
8 changes: 4 additions & 4 deletions templates/services/php-fpm
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# {{ template_destpath }}
# {{ ansible_managed }}

check process php-fpm with pidfile /run/php-fpm/php-fpm.pid
check process php-fpm with pidfile /run/{{ php_fpm_daemon }}/{{ php_fpm_daemon }}.pid
group root
start program = "/bin/systemctl start php-fpm"
stop program = "/bin/systemctl stop php-fpm"
if failed unixsocket /var/run/php-fpm/wordpress.sock for 2 cycles then restart
start program = "/bin/systemctl start {{ php_fpm_daemon }}"
stop program = "/bin/systemctl stop {{ php_fpm_daemon }}"
if failed unixsocket /var/run/{{ php_fpm_daemon }}/wordpress.sock for 2 cycles then restart
8 changes: 4 additions & 4 deletions templates/services/redis
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# {{ template_destpath }}
# {{ ansible_managed }}

check process redis with pidfile /var/run/redis/redis.pid
check process redis with pidfile /var/run/{{ redis_daemon }}/{{ redis_daemon }}.pid
group redis
start program = "/bin/systemctl start redis"
stop program = "/bin/systemctl stop redis"
if failed port 6379 protocol redis then restart
start program = "/bin/systemctl start {{ redis_daemon }}"
stop program = "/bin/systemctl stop {{ redis_daemon }}"
if failed port 6379 protocol {{ redis_daemon }} then restart

0 comments on commit fad9eea

Please sign in to comment.