Skip to content

Commit

Permalink
add ansible config
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriiKhomik committed Nov 19, 2024
1 parent e974077 commit 7aa6573
Show file tree
Hide file tree
Showing 17 changed files with 529 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,5 @@ package-lock.json
# Ignore built assets generated by Tailwind CSS during compilation process
/app/assets/builds/*
!/app/assets/builds/.keep

secrets.yml
40 changes: 40 additions & 0 deletions ansible/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## Documentation: Using Ansible to Configure Monit on a Server

## Introduction

This documentation provides a guide on configuring Monit on a server using Ansible. Monit is a process supervision tool that monitors services and restarts them if they fail. Ansible automates the setup of Monit configurations, ensuring consistency across environments.

## Prerequisites

- Ansible installed on your local machine
- SSH access to the target server

## Ansible Playbook Breakdown

The playbook for Monit setup typically consists of the following tasks:

- Install Monit: Installs the Monit package on the server.
- Copy Monit Configuration Templates: Places your custom Monit configuration files in the appropriate directory.
- Start and Enable Monit: Ensures Monit is started and set to start on boot.

## Running the Ansible Playbook

Run the playbook from the Ansible directory with the following command for production environment:

```
ansible-playbook -i ./inventory-prod.yml ./monit-role.yml
```

or

```
ansible-playbook -i ./inventory-dev.yml ./monit-role.yml
```

for development environment

File secrets.yml is under gitignore but during execution ansible commands it should be present in vars folder

## Accessing to monit

Monit is accessible on the port `2812`
7 changes: 7 additions & 0 deletions ansible/inventory-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
webservers:
hosts:
monit:
ansible_host: 34.133.234.147
ansible_user: andrii_khomik
ansible_ssh_private_key: ~/.ssh/gcp

6 changes: 6 additions & 0 deletions ansible/inventory-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
webservers:
hosts:
monit:
ansible_host: 34.136.62.183
ansible_user: andrii_khomik
ansible_ssh_private_key: ~/.ssh/gcp
8 changes: 8 additions & 0 deletions ansible/monit-role.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- name: use monit role playbook
hosts: monit
become: true

roles:
- role: monit
system_manager: [email protected]
38 changes: 38 additions & 0 deletions ansible/monit/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Role Name
=========

A brief description of the role goes here.

Requirements
------------

Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.

Role Variables
--------------

A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the global scope (ie. hostvars, group vars, etc.) should be mentioned here as well.

Dependencies
------------

A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.

Example Playbook
----------------

Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:

- hosts: servers
roles:
- { role: username.rolename, x: 42 }

License
-------

BSD

Author Information
------------------

An optional section for the role authors to include contact information, or a website (HTML is not allowed).
2 changes: 2 additions & 0 deletions ansible/monit/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# defaults file for monit
5 changes: 5 additions & 0 deletions ansible/monit/files/nginx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
check process nginx with pidfile /run/nginx.pid
start program = "/usr/sbin/service nginx start" with timeout 60 seconds
stop program = "/usr/sbin/service nginx stop"
if failed host 127.0.0.1 port 443 protocol https for 3 cycles then alert
if failed host 127.0.0.1 port 443 protocol https for 3 cycles then restart
4 changes: 4 additions & 0 deletions ansible/monit/files/postgresql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
check process postgresql with pidfile /var/run/postgresql/12-main.pid
start program = "/bin/systemctl postgresql start"
stop program = "/bin/systemctl postgresql stop"
if failed port 5432 then alert
8 changes: 8 additions & 0 deletions ansible/monit/files/redis
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
check process redis with pidfile /var/run/redis/redis-server.pid
start program = "/usr/sbin/service redis-server start"
stop program = "/usr/sbin/service redis-server stop"

if failed host 127.0.0.1 port 6379
then restart
if 5 restarts within 5 cycles
then alert
5 changes: 5 additions & 0 deletions ansible/monit/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- name: Restart monit
service:
name: monit
state: restarted
34 changes: 34 additions & 0 deletions ansible/monit/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
galaxy_info:
author: your name
description: your role description
company: your company (optional)

# If the issue tracker for your role is not on github, uncomment the
# next line and provide a value
# issue_tracker_url: http://example.com/issue/tracker

# Choose a valid license ID from https://spdx.org - some suggested licenses:
# - BSD-3-Clause (default)
# - MIT
# - GPL-2.0-or-later
# - GPL-3.0-only
# - Apache-2.0
# - CC-BY-4.0
license: license (GPL-2.0-or-later, MIT, etc)

min_ansible_version: 2.16.3

# If this a Container Enabled role, provide the minimum Ansible Container version.
# min_ansible_container_version:

galaxy_tags: []
# List tags for your role here, one per line. A tag is a keyword that describes
# and categorizes the role. Users find roles by searching for tags. Be sure to
# remove the '[]' above, if you add tags to this list.
#
# NOTE: A tag is limited to a single word comprised of alphanumeric characters.
# Maximum 20 tags per role.

dependencies: []
# List your role dependencies here, one per line. Be sure to remove the '[]' above,
# if you add dependencies to this list.
40 changes: 40 additions & 0 deletions ansible/monit/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
- name: Install Monit
apt:
name: monit
state: present

- name: Ensure Monit is enabled and started
systemd:
name: monit
enabled: yes
state: started

- name: Copy Redis Monit config
copy:
dest: /etc/monit/conf.d/redis
src: files/redis

- name: Copy Postgres Monit config
copy:
dest: /etc/monit/conf.d/postgresql
src: files/postgresql

- name: Copy Redis Nginx config
copy:
dest: /etc/monit/conf.d/nginx
src: files/nginx

- name: Include encrypted secrets
include_vars:
file: vars/secrets.yml

- name: Copy monit template config files
template:
src: "templates/monitrc.j2"
dest: "/etc/monit/monitrc"

- name: Reload monit to apply configuration
command: monit reload
notify:
- Restart monit
Loading

0 comments on commit 7aa6573

Please sign in to comment.