Skip to content

Commit 74cba72

Browse files
committed
nginx and docker playbook
0 parents  commit 74cba72

File tree

15 files changed

+271
-0
lines changed

15 files changed

+271
-0
lines changed

.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Ignore Ansible Vault files
2+
*.vault
3+
4+
# Ignore temporary files
5+
*.retry
6+
*.log
7+
8+
# Ignore other common files
9+
*.swp
10+
*.swo
11+
.DS_Store
12+
Thumbs.db
13+
14+
.env
15+
16+
credentials/*
17+
!credentials/README.md

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://chatgpt.com/c/66ed8251-8394-8002-9b4a-029f4255770c

ansi.sh

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
# Load environment variables from .env file
4+
if [ -f .env ]; then
5+
export $(grep -v '^#' .env | xargs)
6+
else
7+
echo ".env file not found!"
8+
exit 1
9+
fi
10+
11+
# Run Ansible playbook with the loaded variables
12+
ansible-playbook ./playbook/docker-playbook.yml \
13+
--extra-vars "the_ip=${MACHINE_IP} the_user=${MACHINE_USER} the_key_path=${SSH_PRIVATE_KEY_PATH}"

ansible.cfg

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[defaults]
2+
inventory = ./hosts/inventory.ini
3+
roles_path = ./roles

credentials/README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
```bash
2+
cd credentials
3+
ssh-keygen -t rsa -b 2048 -f "$(pwd)/ansible_hub_key"
4+
```
5+
6+
```bash
7+
ssh -i ./ansible_hub_key [email protected]
8+
```

hosts/inventory.ini

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[web_servers]
2+
web_server ansible_host={{the_ip}}
3+
4+
[web_servers:vars]
5+
ansible_user={{the_user}}
6+
ansible_ssh_private_key_file={{the_key_path}}
7+

playbook/docker-playbook.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
- name: Configure Docker on Web Servers
3+
hosts: web_servers
4+
become: yes
5+
roles:
6+
- docker

playbook/nginx-playbook.yml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
- name: Configure Nginx on web servers
3+
hosts: web_servers
4+
become: yes
5+
roles:
6+
- nginx

roles/docker/tasks/main.yml

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
- name: Install prerequisites for Docker repository
2+
become: true
3+
apt:
4+
name:
5+
- apt-transport-https
6+
- ca-certificates
7+
- curl
8+
- gnupg
9+
- software-properties-common
10+
force_apt_get: True
11+
update_cache: yes
12+
13+
- name: Add Docker GPG key
14+
become: true
15+
apt_key:
16+
url: https://download.docker.com/linux/ubuntu/gpg
17+
18+
- name: Add Docker APT repository.
19+
become: true
20+
apt_repository:
21+
#repo: deb [arch=amd64] https://download.docker.com/{{ ansible_system | lower }}/{{ ansible_distribution | lower }} {{ ansible_distribution_release }} stable
22+
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable
23+
24+
- name: Install Docker CE.
25+
become: true
26+
apt:
27+
name:
28+
- docker-ce
29+
- docker-ce-cli
30+
- containerd.io
31+
force_apt_get: True
32+
update_cache: yes
33+
allow_unauthenticated: yes
34+
35+
- name: Install Docker-compose.
36+
become: true
37+
get_url:
38+
url: "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-{{ansible_system}}-{{ansible_architecture}}"
39+
dest: /usr/local/bin/docker-compose
40+
mode: +x
41+
42+
- name: Changing permission of "/var/run/docker.sock", adding "777"
43+
become: true
44+
file:
45+
dest: /var/run/docker.sock
46+
mode: 0777
47+
48+
- name: Pull Docker image
49+
docker_image:
50+
name: hiremostafa/express-demo:1.0.1
51+
source: pull
52+
53+
- name: Run Docker container
54+
docker_container:
55+
name: express_demo
56+
image: hiremostafa/express-demo:1.0.1
57+
state: started
58+
published_ports:
59+
- "3006:3006"
60+
env:
61+
name: kamal
62+
63+

roles/nginx/defaults/main.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
---

roles/nginx/files/index.html

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<!DOCTYPE html>
2+
<html lang="en" class="h-full">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Ansible Nginx Installation</title>
7+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
8+
</head>
9+
<body class="flex flex-col min-h-full bg-gray-100 font-sans">
10+
11+
<header class="bg-green-600 text-white py-6">
12+
<div class="container mx-auto text-center">
13+
<h1 class="text-4xl font-bold">Ansible Nginx Installation</h1>
14+
<p class="mt-2 text-xl">Automated Deployment Success!</p>
15+
</div>
16+
</header>
17+
18+
<main class="flex-grow container mx-auto mt-8 px-4 mb-8">
19+
<div class="bg-white shadow-md rounded-lg p-6 mb-8">
20+
<h2 class="text-2xl font-semibold mb-4 text-green-600">About This Deployment</h2>
21+
<p class="text-gray-700 mb-4">
22+
This page is served by Nginx, which was automatically installed and configured using Ansible.
23+
This demonstrates the power of automation in deploying web servers quickly and consistently.
24+
</p>
25+
<div class="mt-6">
26+
<a href="https://docs.ansible.com" target="_blank" class="bg-green-600 text-white py-2 px-4 rounded hover:bg-green-500 mr-4">Ansible Docs</a>
27+
<a href="https://nginx.org/en/docs/" target="_blank" class="bg-blue-600 text-white py-2 px-4 rounded hover:bg-blue-500">Nginx Docs</a>
28+
</div>
29+
</div>
30+
31+
<div class="grid md:grid-cols-2 gap-8">
32+
<div class="bg-white shadow-md rounded-lg p-6">
33+
<h3 class="text-xl font-semibold mb-4 text-green-600">Ansible Benefits</h3>
34+
<ul class="list-disc list-inside text-gray-700">
35+
<li>Automated configuration management</li>
36+
<li>Consistent deployments across environments</li>
37+
<li>Scalable infrastructure management</li>
38+
<li>Reduced human error in deployments</li>
39+
</ul>
40+
</div>
41+
<div class="bg-white shadow-md rounded-lg p-6">
42+
<h3 class="text-xl font-semibold mb-4 text-blue-600">Nginx Features</h3>
43+
<ul class="list-disc list-inside text-gray-700">
44+
<li>High performance web server</li>
45+
<li>Reverse proxy capabilities</li>
46+
<li>Load balancing</li>
47+
<li>HTTP caching</li>
48+
</ul>
49+
</div>
50+
</div>
51+
</main>
52+
53+
<footer class="bg-gray-800 text-white py-4 mt-auto">
54+
<div class="container mx-auto text-center">
55+
<p>&copy; 2024 Ansible Nginx Deployment. Created with automation.</p>
56+
</div>
57+
</footer>
58+
59+
</body>
60+
</html>

roles/nginx/handlers/main.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
- name: Restart Nginx
3+
service:
4+
name: nginx
5+
state: restarted
6+
7+
- name: Reload Nginx
8+
service:
9+
name: nginx
10+
state: reloaded

roles/nginx/tasks/main.yml

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
- name: Install Nginx
3+
become: true
4+
apt:
5+
name: nginx
6+
force_apt_get: true
7+
update_cache: yes
8+
9+
- name: Ensure Nginx sites-available directory exists
10+
file:
11+
path: /etc/nginx/sites-available
12+
state: directory
13+
mode: '0755'
14+
15+
- name: Ensure Nginx sites-enabled directory exists
16+
file:
17+
path: /etc/nginx/sites-enabled
18+
state: directory
19+
mode: '0755'
20+
21+
- name: Ensure website directory exists
22+
file:
23+
path: "/var/www/{{ server_name }}"
24+
state: directory
25+
owner: www-data
26+
group: www-data
27+
mode: '0755'
28+
29+
- name: Deploy Nginx site configuration
30+
template:
31+
src: "nginx.conf.j2"
32+
dest: "/etc/nginx/sites-available/{{ server_name }}.conf"
33+
notify: Reload Nginx
34+
35+
- name: Enable Nginx site configuration
36+
file:
37+
src: "/etc/nginx/sites-available/{{ server_name }}.conf"
38+
dest: "/etc/nginx/sites-enabled/{{ server_name }}.conf"
39+
state: link
40+
notify: Reload Nginx
41+
42+
- name: Remove default Nginx site configuration
43+
file:
44+
path: /etc/nginx/sites-enabled/default
45+
state: absent
46+
notify: Reload Nginx
47+
48+
- name: Copy index.html to Nginx root
49+
copy:
50+
src: index.html
51+
dest: "/var/www/{{ server_name }}/index.html"
52+
owner: www-data
53+
group: www-data
54+
mode: '0644'
55+
56+
- name: Ensure Nginx is started and enabled
57+
service:
58+
name: nginx
59+
state: started
60+
enabled: yes

roles/nginx/templates/nginx.conf.j2

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
server {
2+
listen 80;
3+
listen [::]:80;
4+
5+
root /var/www/{{ server_name }};
6+
index index.html index.htm index.nginx-debian.html;
7+
8+
server_name {{ server_name }};
9+
10+
location / {
11+
try_files $uri $uri/ =404;
12+
}
13+
}

roles/nginx/vars/main.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
nginx_port: 80
3+
server_name: localhost

0 commit comments

Comments
 (0)