Skip to content

Commit cd08974

Browse files
Merge pull request #104 from socraticDevBlog/20250502-ansibleforvps
Ajout de la configuration Ansible pour le déploiement de FastAPI et P…
2 parents 4c83f12 + 320f02d commit cd08974

File tree

13 files changed

+344
-0
lines changed

13 files changed

+344
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
inventory.ini
2+
13
# Byte-compiled / optimized / DLL files
24
__pycache__/
35
*.py[cod]

backend/ansible/README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
![Ansible](https://img.shields.io/badge/ansible-%231A1918.svg?style=for-the-badge&logo=ansible&logoColor=white)![Postgres](https://img.shields.io/badge/postgres-%23316192.svg?style=for-the-badge&logo=postgresql&logoColor=white)![Debian](https://img.shields.io/badge/Debian-D70A53?style=for-the-badge&logo=debian&logoColor=white)
2+
3+
# infra/vps
4+
5+
## requirements
6+
7+
- a linux server (debian) with ssh enabled and you have sudo privilege
8+
- have Ansible installed on local machine
9+
- macOS: `brew install ansible`
10+
11+
## `inventory.ini` file
12+
13+
create a `inventory.ini` file at the root of this directory
14+
and fill it out with your server's information:
15+
16+
- server url
17+
- linux user's username
18+
19+
__don't commit this file with your private information__
20+
21+
``ìni
22+
[linux_vps]
23+
<my_server.com> ansible_user=<my_server_username>
24+
25+
```
26+
27+
## ansible update debian server
28+
29+
play only the check tasks to list what packages are getting upgraded
30+
31+
```bash
32+
ansible-playbook --tags check --ask-become-pass update_debian.yml
33+
```
34+
35+
perform the upgrade
36+
37+
```bash
38+
ansible-playbook --ask-become-pass update_debian.yml
39+
```
40+
41+
## ansible installs and configure postgresql database on Debian server
42+
43+
### test connection to vps first
44+
45+
```bash
46+
ansible -i inventory.ini linux_vps -m ping
47+
48+
# expect success
49+
```
50+
51+
### sensitive value in versionned file
52+
53+
we are using `ansible-vault` command-line tool to create, encrypt, decrypt, and
54+
safely use versionned secrets in files
55+
56+
```bash
57+
# create a new encrypted secrets file
58+
ansible-vault create | edit <file_name>.yml
59+
60+
# will open a vi editor where you you can edit key-value (ini style) secrets
61+
```
62+
63+
### run the playbook with the Vault password
64+
65+
Runs the playbook
66+
67+
- will prompt `BECOME` for your server 'sudo' password
68+
- will prompt `Vault password` for your secret file (ansible-vault) password
69+
70+
```bash
71+
ansible-playbook \
72+
--ask-vault-pass \
73+
--ask-become-pass \
74+
install_postgresql.yml
75+
```
76+
77+
## from local machine
78+
79+
postgresql database is only available from server's localhost network
80+
81+
### ssh tunnel from local machine to server
82+
83+
in order to connect to it from an IDE like DBeaver, establish an ssh tunnel
84+
(leave the terminal open to keep connection open)
85+
86+
```bash
87+
ssh -L 5432:localhost:5432 [email protected]
88+
```
89+
90+
### DBeaver IDE
91+
92+
| host | Database | authentitification | username | password |
93+
| --------- | -------- | ------------------ | -------- | ------------------- |
94+
| localhost | devdb | Database native | devuser | `<db_password.yml>` |

backend/ansible/ansible.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[defaults]
2+
ask_sudo_pass = True
3+
inventory = inventory.ini
4+
roles_path = ./roles

backend/ansible/db_password.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
$ANSIBLE_VAULT;1.1;AES256
2+
66626138373030663431653437323164376233303533333236353632366535393263396365626339
3+
3236376335326437663337303063656332353961633630610a626637333964346138633365306235
4+
65343131323336393862643239363739353037613339633737623962646561326261346439613337
5+
3631376636373363660a373136626437666630643165656135666633303566306464653063366432
6+
39656264626664613137356264613838363365393830343838313864363665643631

backend/ansible/install_app.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
- name: Set up Fastapi app
3+
hosts: all
4+
become: true
5+
roles:
6+
- role: fastapi_app
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
- name: Set up PostgreSQL Database
3+
hosts: all
4+
become: true
5+
vars_files:
6+
- db_password.yml
7+
roles:
8+
- role: postgresql
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
- name: Verify Python version
2+
command: python3 --version
3+
register: python_version_output
4+
failed_when: python_version_output.rc != 0
5+
listen: Verify Python version
6+
7+
- name: Debug Python version
8+
debug:
9+
msg: "Python version: {{ python_version_output.stdout }}"
10+
listen: Verify Python version
11+
12+
- name: Verify pipenv installation
13+
command: pipenv --version
14+
register: pipenv_version_output
15+
failed_when: pipenv_version_output.rc != 0
16+
listen: Verify pipenv installation
17+
18+
- name: Debug pipenv version
19+
debug:
20+
msg: "Pipenv version: {{ pipenv_version_output.stdout }}"
21+
listen: Verify pipenv installation
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
- name: Install required Python packages
2+
apt:
3+
name:
4+
- python3-pip
5+
state: present
6+
notify: Verify Python version
7+
8+
- name: Install pipenv using pip
9+
command: pip3 install pipenv --break-system-packages
10+
notify: Verify pipenv installation

backend/ansible/roles/fastapi_app/vars/main.yml

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
- name: restart postgresql
2+
service:
3+
name: postgresql
4+
state: restarted
5+
6+
- name: reload postgresql
7+
service:
8+
name: postgresql
9+
state: reloaded

0 commit comments

Comments
 (0)