Commit 4aa9f8b9 authored by Mohamad Bashar Desoki's avatar Mohamad Bashar Desoki

add ansible lab files

parents
# Ansible open-source automation tool
This project contains Ansible playbooks to automate infrastructure deployments and manage various services.
## Table Of Contents
- [Inventory](#inventory)
- [Playbooks](#playbooks)
- [Roles](#roles)
- [Configurations](#configurations)
- [How To Use](#how-to-use)
## Inventory
The `inventory.ini` file contains the list of hosts organized in 'webservers' and 'databases' groups.
Ansible uses SSH to connect to these host machines with an Ansible user (such as 'ubuntu').
```
[webservers]
192.168.0.10 ansible_user=ubuntu
[databases]
db-server01 ansible_user=ubuntu
```
## Playbooks
Playbook files are the major entry points of an Ansible automation project and contain one or more plays. Our playbooks include:
- `playbook.yml`: Updates all packages on the managed nodes before executing other tasks.
- `install_nginx.yaml`: Installs and configures Nginx to serve webpages using a custom configuration template.
## Roles
Role directories contain sets of tasks, files, templates or modules that define the setup for certain services e.g., 'nginx'. They can also be reused in different playbooks thereby minimizing code duplication and improving organization:
- `roles/system_info`: Gathers and displays system information such as RAM usage, disk space, CPU utilization, etc.
- `roles/system_update`: Updates all packages on the node.
## Configurations
Configuration files include templates for services like Nginx (`nginx.conf.j2`) with placeholder variables for configuration customization during playbook execution.
## How To Use
To get started, you will need to have Ansible installed in your environment. Make updates or customizations to the `inventory.ini`, roles and configurations as needed for your specific use case.
Follow these steps:
- Clone this repository in your terminal to your desired location using the `git clone` command:
```bash
git clone https://git.hiast.edu.sy/mohamadbashar.disoki/ansible-open-source-automation-tool.git
```
- Modify inventory file (if necessary).
- Run a playbook with 'ansible-playbook' command:
```bash
ansible-playbook -i inventories/inventory.ini path/to/your_playbook.yml
```
Note that for the security reasons, it is a common practice to keep sensitive data like host credentials outside of version control and use encrypted vaults for storing them. However, this project does not include encryption as the provided code doesn't mention using it.
\ No newline at end of file
[webservers]
web1 ansible_host=192.168.159.130 ansible_user=ubuntu
[databases]
db1 ansible_host=192.168.159.141 ansible_user=ubuntu
---
- name: Configure Web Servers
hosts: webservers
become: yes
roles:
- webserver
- name: Configure Database Servers
hosts: databases
become: yes
roles:
- database
---
- name: Install MySQL
apt:
name: mysql-server
state: present
- name: Ensure MySQL is running
service:
name: mysql
state: started
---
- name: Install Nginx
apt:
name: nginx
state: present
- name: Deploy index.html
template:
src: index.html.j2
dest: /var/www/html/index.html
- name: Ensure Nginx is running
service:
name: nginx
state: started
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to My Web Server</title>
</head>
<body>
<h1>welcome to my nginx sevrer</h1>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome to My Web Server</title>
</head>
<body>
<h1>Hello from ansible lab</h1>
</body>
</html>
---
- name: Install Nginx
hosts: web_servers
become: yes
vars:
nginx_port: 8085
tasks:
- name: Ensure Nginx is installed
apt:
name: nginx
state: present
- name: Copy Nginx configuration
template:
src: nginx.conf.j2
dest: /etc/nginx/sites-available/default
- name: Copy index.html to web root
copy:
src: index.html
dest: /var/www/html/index.html
owner: www-data
group: www-data
mode: '0644'
- name: Start Nginx service
service:
name: nginx
state: started
server {
listen {{ nginx_port }};
server_name localhost;
location / {
root /var/www/html;
index index.html index.htm;
}
}
# inventory.ini
[db]
db1 ansible_host=192.168.159.130 ansible_user=root
[all:vars]
postgres_password="postgres"
db_name="ansible_db"
db_user="ansible"
db_user_password= "ansible"
- name: apply db role on db group
hosts: db
become: true
become_method: sudo
roles:
- db
- name: Install Python3 and pip (for Python 3)
apt:
name:
- python3
- python3-pip
state: present
- name: Install psycopg2 (PostgreSQL adapter for Python)
pip:
name: psycopg2-binary
state: present
- name: Install PostgreSQL
apt:
name: postgresql
state: present
- name: Ensure PostgreSQL is running
service:
name: postgresql
state: started
enabled: yes
- name: Set password for PostgreSQL user "postgres"
postgresql_user:
name: postgres
password: "{{ postgres_password }}"
state: present
become: yes
become_user: postgres
- name: Create PostgreSQL database
postgresql_db:
name: "{{ db_name }}"
state: present
login_user: postgres
login_password: "{{ postgres_password }}"
become: yes
become_user: postgres
- name: Create PostgreSQL user
postgresql_user:
name: "{{ db_user }}"
password: "{{ db_user_password }}"
db: "{{ db_name }}"
priv: "ALL"
state: present
login_user: postgres
login_password: "{{ postgres_password }}"
become: yes
become_user: postgres
- name: Create messages table
postgresql_query:
login_db: "{{ db_name }}"
login_user: postgres
login_password: "{{ postgres_password }}"
query: |
CREATE TABLE IF NOT EXISTS messages (
id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
message VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
become: yes
become_user: postgres
- name: Reload PostgreSQL
service:
name: postgresql
state: reloaded
become: yes
- name: Grant all privileges on database to ansible user
postgresql_privs:
database: "{{ db_name }}"
roles: "{{ db_user }}"
privs: ALL
type: database
state: present
become: yes
become_user: postgres
- name: Grant all privileges on messages table to ansible user
postgresql_privs:
database: "{{ db_name }}"
roles: "{{ db_user }}"
privs: ALL
type: table
objs: messages
state: present
become: yes
become_user: postgres
#- name: Grant all privileges on messages_id_seq sequence to ansible user
# postgresql_privs:
# database: "{{ db_name }}"
# roles: "{{ db_user }}"
# privs: ALL
# type: sequence
# objs: messages_id_seq
# state: present
# login_user: postgres
# login_password: "{{ postgres_password }}"
# become: yes
# become_user: postgres
[servers]
web1 ansible_host=192.168.159.130 ansible_user=ubuntu
db1 ansible_host=192.168.159.141 ansible_user=ubuntu
---
- name: Gather System Information
hosts: servers
become: yes
roles:
- system_info
- system_update
---
- name: Gather RAM information
command: free -h
register: ram_info
- name: Display RAM information
debug:
var: ram_info.stdout
- name: Gather Disk usage information
command: df -h
register: disk_info
- name: Display Disk usage information
debug:
var: disk_info.stdout
- name: Gather CPU utilization
shell: top -bn1 | grep "Cpu(s)"
register: cpu_info
- name: Display CPU utilization
debug:
var: cpu_info.stdout
---
- name: Update all packages
ansible.builtin.apt:
update_cache: yes
when: ansible_os_family == "Debian"
- name: Update all packages
yum:
name: '*'
state: latest
when: ansible_os_family == "RedHat"
---
- name: Update all packages on Debian-based and Red Hat-based systems
hosts: all
become: yes
tasks:
- name: Ping the host to check connectivity
ping:
- name: Update the package list on Debian-based systems
apt:
update_cache: yes
when: ansible_os_family == "Debian"
- name: Update all packages on Red Hat-based systems
yum:
name: '*'
state: latest
when: ansible_os_family == "RedHat"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment