From 3ff2020d2c12453f8c1870743c174a30d86dc041 Mon Sep 17 00:00:00 2001 From: Tim Gerla Date: Sun, 7 Apr 2013 20:14:57 -0700 Subject: [PATCH] remove some redundancy, split up nagios config files --- lamp_haproxy/add_webservers.yml | 10 -- lamp_haproxy/nagios.yml | 1 + lamp_haproxy/remove_webservers.yml | 10 -- .../nagios/files/ansible-managed-services.cfg | 39 ++++++ lamp_haproxy/roles/nagios/files/nagios.cfg | 1 + lamp_haproxy/roles/nagios/tasks/main.yml | 19 ++- .../roles/nagios/templates/dbservers.cfg.j2 | 25 ++++ .../roles/nagios/templates/lbservers.cfg.j2 | 22 ++++ .../roles/nagios/templates/servers.cfg.j2 | 112 ------------------ .../roles/nagios/templates/webservers.cfg.j2 | 25 ++++ lamp_haproxy/roles/web/handlers/main.yml | 5 - lamp_haproxy/roles/web/tasks/add_to_lb.yml | 15 --- lamp_haproxy/roles/web/tasks/copy_code.yml | 8 -- .../roles/web/tasks/install_httpd.yml | 11 -- lamp_haproxy/roles/web/tasks/main.yml | 17 +++ .../roles/web/tasks/remove_from_lb.yml | 23 ---- 16 files changed, 139 insertions(+), 204 deletions(-) delete mode 100644 lamp_haproxy/add_webservers.yml delete mode 100644 lamp_haproxy/remove_webservers.yml create mode 100644 lamp_haproxy/roles/nagios/files/ansible-managed-services.cfg create mode 100644 lamp_haproxy/roles/nagios/templates/dbservers.cfg.j2 create mode 100644 lamp_haproxy/roles/nagios/templates/lbservers.cfg.j2 delete mode 100644 lamp_haproxy/roles/nagios/templates/servers.cfg.j2 create mode 100644 lamp_haproxy/roles/nagios/templates/webservers.cfg.j2 delete mode 100644 lamp_haproxy/roles/web/handlers/main.yml delete mode 100644 lamp_haproxy/roles/web/tasks/add_to_lb.yml delete mode 100644 lamp_haproxy/roles/web/tasks/copy_code.yml delete mode 100644 lamp_haproxy/roles/web/tasks/install_httpd.yml create mode 100644 lamp_haproxy/roles/web/tasks/main.yml delete mode 100644 lamp_haproxy/roles/web/tasks/remove_from_lb.yml diff --git a/lamp_haproxy/add_webservers.yml b/lamp_haproxy/add_webservers.yml deleted file mode 100644 index 3cd66ca..0000000 --- a/lamp_haproxy/add_webservers.yml +++ /dev/null @@ -1,10 +0,0 @@ ---- -# This Playbook adds a webserver into the the web cluster - -- hosts: webservers - user: root - serial: 1 - tasks: - - include: roles/web/tasks/install_httpd.yml - - include: roles/web/tasks/copy_code.yml - - include: roles/web/tasks/add_to_lb.yml diff --git a/lamp_haproxy/nagios.yml b/lamp_haproxy/nagios.yml index 4639bb9..3016617 100644 --- a/lamp_haproxy/nagios.yml +++ b/lamp_haproxy/nagios.yml @@ -1,6 +1,7 @@ --- # This playbook configures the monitoring node +# trigger fact-gathering for all hosts - hosts: all tasks: diff --git a/lamp_haproxy/remove_webservers.yml b/lamp_haproxy/remove_webservers.yml deleted file mode 100644 index 564b3be..0000000 --- a/lamp_haproxy/remove_webservers.yml +++ /dev/null @@ -1,10 +0,0 @@ ---- -# This playbook removes a webserver from the pool serially. -# Change the value of serial: to adjust the number of servers -# to be removed at a time. - -- hosts: webservers - user: root - serial: 1 - tasks: - - include: ../roles/web/tasks/remove_from_lb.yml diff --git a/lamp_haproxy/roles/nagios/files/ansible-managed-services.cfg b/lamp_haproxy/roles/nagios/files/ansible-managed-services.cfg new file mode 100644 index 0000000..20eb56d --- /dev/null +++ b/lamp_haproxy/roles/nagios/files/ansible-managed-services.cfg @@ -0,0 +1,39 @@ +# {{ ansible_managed }} + +# service checks to be applied to all hosts + +define service { + use local-service + host_name localhost + service_description Root Partition + check_command check_local_disk!20%!10%!/ +} + +define service { + use local-service + host_name * + service_description Current Users + check_command check_local_users!20!50 +} + + +define service { + use local-service + host_name * + service_description Total Processes + check_command check_local_procs!250!400!RSZDT +} + +define service { + use local-service + host_name * + service_description Current Load + check_command check_local_load!5.0,4.0,3.0!10.0,6.0,4.0 +} + +define service { + use local-service + host_name * + service_description Swap Usage + check_command check_local_swap!20!10 +} diff --git a/lamp_haproxy/roles/nagios/files/nagios.cfg b/lamp_haproxy/roles/nagios/files/nagios.cfg index 30fcf8c..dce3495 100644 --- a/lamp_haproxy/roles/nagios/files/nagios.cfg +++ b/lamp_haproxy/roles/nagios/files/nagios.cfg @@ -35,6 +35,7 @@ cfg_file=/etc/nagios/objects/templates.cfg # Definitions for monitoring the local (Linux) host cfg_file=/etc/nagios/objects/localhost.cfg +cfg_file=/etc/nagios/ansible-managed-services.cfg cfg_dir=/etc/nagios/ansible-managed diff --git a/lamp_haproxy/roles/nagios/tasks/main.yml b/lamp_haproxy/roles/nagios/tasks/main.yml index 2834dff..263f54a 100644 --- a/lamp_haproxy/roles/nagios/tasks/main.yml +++ b/lamp_haproxy/roles/nagios/tasks/main.yml @@ -16,26 +16,25 @@ - name: create nagios config dir file: path=/etc/nagios/ansible-managed state=directory - tags: - - nagios-setup - name: configure nagios copy: src=nagios.cfg dest=/etc/nagios/nagios.cfg - tags: - - nagios-setup notify: restart nagios - name: configure localhost monitoring copy: src=localhost.cfg dest=/etc/nagios/objects/localhost.cfg - tags: - - nagios-setup notify: restart nagios +- name: configure nagios services + copy: src=ansible-managed-services.cfg dest=/etc/nagios/ + - name: create the nagios object files - template: src=servers.cfg.j2 - dest=/etc/nagios/ansible-managed/servers.cfg - tags: - - nagios-setup + template: src={{ item + ".j2" }} + dest=/etc/nagios/ansible-managed/{{ item }} + with_items: + - webservers.cfg + - dbservers.cfg + - lbservers.cfg notify: restart nagios - name: start nagios diff --git a/lamp_haproxy/roles/nagios/templates/dbservers.cfg.j2 b/lamp_haproxy/roles/nagios/templates/dbservers.cfg.j2 new file mode 100644 index 0000000..3ba6ff0 --- /dev/null +++ b/lamp_haproxy/roles/nagios/templates/dbservers.cfg.j2 @@ -0,0 +1,25 @@ +# {{ ansible_managed }} + +define hostgroup { + hostgroup_name dbservers + alias Database Servers +} + +{% for host in groups['dbservers'] %} + define host { + use linux-server + host_name {{ host }} + alias {{ host }} + address {{ hostvars[host].ansible_default_ipv4.address }} + hostgroups dbservers + } +{% endfor %} + +#define service { +# use local-service +# hostgroup_name dbservers +# service_description MySQL Database Server +# check_command check_mysql +# notifications_enabled 0 +#} + diff --git a/lamp_haproxy/roles/nagios/templates/lbservers.cfg.j2 b/lamp_haproxy/roles/nagios/templates/lbservers.cfg.j2 new file mode 100644 index 0000000..39909f5 --- /dev/null +++ b/lamp_haproxy/roles/nagios/templates/lbservers.cfg.j2 @@ -0,0 +1,22 @@ +# {{ ansible_managed }} + +define hostgroup { + hostgroup_name loadbalancers + alias Load Balancers +} + +{% for host in groups['lbservers'] %} +define host { + use linux-server + host_name {{ host }} + alias {{ host }} + address {{ hostvars[host].ansible_default_ipv4.address }} + hostgroups loadbalancers +} +define service { + use local-service + host_name {{ host }} + service_description HAProxy Load Balancer + check_command check_http!-p{{ hostvars[host].listenport }} +} +{% endfor %} diff --git a/lamp_haproxy/roles/nagios/templates/servers.cfg.j2 b/lamp_haproxy/roles/nagios/templates/servers.cfg.j2 deleted file mode 100644 index b017d81..0000000 --- a/lamp_haproxy/roles/nagios/templates/servers.cfg.j2 +++ /dev/null @@ -1,112 +0,0 @@ -# {{ ansible_managed }} - -define hostgroup { - hostgroup_name webservers - alias Web Servers -} - -define hostgroup { - hostgroup_name loadbalancers - alias Load Balancers -} - -define hostgroup { - hostgroup_name dbservers - alias Database Servers -} - - -{% for host in groups['webservers'] %} -define host { - use linux-server - host_name {{ host }} - alias {{ host }} - address {{ hostvars[host].ansible_default_ipv4.address }} - hostgroups webservers -} -{% endfor %} - -{% for host in groups['lbservers'] %} -define host { - use linux-server - host_name {{ host }} - alias {{ host }} - address {{ hostvars[host].ansible_default_ipv4.address }} - hostgroups loadbalancers -} -define service { - use local-service - host_name {{ host }} - service_description HAProxy Load Balancer - check_command check_http!-p{{ hostvars[host].listenport }} -} -{% endfor %} - -{% for host in groups['dbservers'] %} - define host { - use linux-server - host_name {{ host }} - alias {{ host }} - address {{ hostvars[host].ansible_default_ipv4.address }} - hostgroups dbservers - } -{% endfor %} - -# service checks to be applied to all hosts - -define service { - use local-service - host_name localhost - service_description Root Partition - check_command check_local_disk!20%!10%!/ -} - -define service { - use local-service - host_name * - service_description Current Users - check_command check_local_users!20!50 -} - - -define service { - use local-service - host_name * - service_description Total Processes - check_command check_local_procs!250!400!RSZDT -} - -define service { - use local-service - host_name * - service_description Current Load - check_command check_local_load!5.0,4.0,3.0!10.0,6.0,4.0 -} - -define service { - use local-service - host_name * - service_description Swap Usage - check_command check_local_swap!20!10 -} - -# service checks to be applied to the web server - -define service { - use local-service - hostgroup_name webservers - service_description webserver - check_command check_http - notifications_enabled 0 -} - -# service checks to be applied to the database server - -#define service { -# use local-service -# hostgroup_name dbservers -# service_description MySQL Database Server -# check_command check_mysql -# notifications_enabled 0 -#} - diff --git a/lamp_haproxy/roles/nagios/templates/webservers.cfg.j2 b/lamp_haproxy/roles/nagios/templates/webservers.cfg.j2 new file mode 100644 index 0000000..d4b79dd --- /dev/null +++ b/lamp_haproxy/roles/nagios/templates/webservers.cfg.j2 @@ -0,0 +1,25 @@ +# {{ ansible_managed }} + +define hostgroup { + hostgroup_name webservers + alias Web Servers +} + +{% for host in groups['webservers'] %} +define host { + use linux-server + host_name {{ host }} + alias {{ host }} + address {{ hostvars[host].ansible_default_ipv4.address }} + hostgroups webservers +} +{% endfor %} + +# service checks to be applied to the web server +define service { + use local-service + hostgroup_name webservers + service_description webserver + check_command check_http + notifications_enabled 0 +} diff --git a/lamp_haproxy/roles/web/handlers/main.yml b/lamp_haproxy/roles/web/handlers/main.yml deleted file mode 100644 index 41cd34f..0000000 --- a/lamp_haproxy/roles/web/handlers/main.yml +++ /dev/null @@ -1,5 +0,0 @@ ---- -# Handler for the web tier - -- name: restart iptables - service: name=iptables state=restarted diff --git a/lamp_haproxy/roles/web/tasks/add_to_lb.yml b/lamp_haproxy/roles/web/tasks/add_to_lb.yml deleted file mode 100644 index 15d00b3..0000000 --- a/lamp_haproxy/roles/web/tasks/add_to_lb.yml +++ /dev/null @@ -1,15 +0,0 @@ ---- -# This Playbook handles the addition of a web server to the pool. - -- name: Add server to LB - lineinfile: dest=/etc/haproxy/haproxy.cfg state=present regexp="${ansible_hostname}" line="server ${ansible_hostname} ${hostvars.{$inventory_hostname}.ansible_$iface.ipv4.address}:${httpd_port}" - delegate_to: $item - with_items: ${groups.lbservers} - register: last_run - -- name: Reload the haproxy - service: name=haproxy state=reloaded - delegate_to: $item - with_items: ${groups.lbservers} - only_if: ${last_run.changed} - diff --git a/lamp_haproxy/roles/web/tasks/copy_code.yml b/lamp_haproxy/roles/web/tasks/copy_code.yml deleted file mode 100644 index 1467cf8..0000000 --- a/lamp_haproxy/roles/web/tasks/copy_code.yml +++ /dev/null @@ -1,8 +0,0 @@ ---- -# This Playbook is responsible for copying the latest dev/production code from the version control system. - -- name: Copy the code from repository - git: repo=${repository} dest=/var/www/html/ - -- name: Create the index.php file - template: src=index.php.j2 dest=/var/www/html/index.php diff --git a/lamp_haproxy/roles/web/tasks/install_httpd.yml b/lamp_haproxy/roles/web/tasks/install_httpd.yml deleted file mode 100644 index be44e66..0000000 --- a/lamp_haproxy/roles/web/tasks/install_httpd.yml +++ /dev/null @@ -1,11 +0,0 @@ ---- -# This playbook installs the php modules. - -- name: Install http and php etc - action: yum name=$item state=installed - with_items: - - php - - php-mysql - -- name: Configure SELinux to allow httpd to connect to remote database - seboolean: name=httpd_can_network_connect_db state=true persistent=yes diff --git a/lamp_haproxy/roles/web/tasks/main.yml b/lamp_haproxy/roles/web/tasks/main.yml new file mode 100644 index 0000000..73e77a9 --- /dev/null +++ b/lamp_haproxy/roles/web/tasks/main.yml @@ -0,0 +1,17 @@ +--- + +# httpd is handled by the base-apache role upstream +- name: Install php + action: yum name=$item state=installed + with_items: + - php + - php-mysql + +- name: Configure SELinux to allow httpd to connect to remote database + seboolean: name=httpd_can_network_connect_db state=true persistent=yes + +- name: Copy the code from repository + git: repo=${repository} dest=/var/www/html/ + +- name: Create the index.php file + template: src=index.php.j2 dest=/var/www/html/index.php diff --git a/lamp_haproxy/roles/web/tasks/remove_from_lb.yml b/lamp_haproxy/roles/web/tasks/remove_from_lb.yml deleted file mode 100644 index a3ed52f..0000000 --- a/lamp_haproxy/roles/web/tasks/remove_from_lb.yml +++ /dev/null @@ -1,23 +0,0 @@ ---- -# This playbook handles the removal of a webserver from the pool. - -- name: Remove the code from server - command: rm -rf /var/www/html/* - -- name: Remove server from LB - lineinfile: dest=/etc/haproxy/haproxy.cfg state=absent regexp="${ansible_hostname}" - delegate_to: $item - with_items: ${groups.lbservers} - register: last_run - -- name: disable the server in haproxy - shell: echo "disable server myapplb/${ansible_hostname}" | socat stdio /var/lib/haproxy/stats - delegate_to: $item - with_items: ${groups.lbservers} - -- name: Remove the httpd package - yum: name=httpd state=absent - - - -