homeserver initial commit

- ansible
- docker-compose
- Kubernetes_deployments
This commit is contained in:
2025-02-12 20:10:56 +02:00
commit e5e8aa6b87
70 changed files with 2860 additions and 0 deletions

1
ansible/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
secrets/

86
ansible/README.md Normal file
View File

@ -0,0 +1,86 @@
# Ansible Playbook for Proxmox VM Management
This Ansible playbook automates the creation, deletion, and configuration of
virtual machines (VMs) on a Proxmox server.
## Prerequisites
- Ansible installed on the local machine
- Ansible community.general.proxmox_kvm module
- Access to a Proxmox server with API access enabled
- Python `proxmoxer` library installed (`pip install proxmoxer`)
## Setup
1. Clone this repository:
```sh
git clone https://github.com/TheTaqiTahmid/proxmox_ansible_automation
```
2. Update the `inventory` file with your Proxmox server details:
```yaml
all:
hosts:
proxmox:
ansible_host: your_proxmox_ip
ansible_user: your_proxmox_user
ansible_password: your_proxmox_password
```
In the current example implementation in `inventories/hosts.yaml`, there are
multiple groups depending on the types of hosts.
3. Add group-related variables to the group file under the `group_vars` directory
and individual host-related variables to the files under the `host_vars`
directory. Ansible will automatically pick up these variables.
## Playbooks
### Create VM
To create the VMs, run the following command:
```sh
ansible-playbook playbooks/create-vms.yaml
```
The playbook can be run against specific Proxmox instance using:
```sh
ansible-playbook playbooks/create-vms.yaml --limit proxmox1
```
### Delete VM
To delete existing VMs, run the following command:
```sh
ansible-playbook playbooks/destroy-vms.yaml
```
Similarly the destory playbook can be run against specific Proxmox instance using:
```sh
ansible-playbook playbooks/destroy-vms.yaml --limit proxmox1
```
### Configure VM
To configure an existing VM, run the following command:
```sh
ansible-playbook playbooks/configure-vms.yaml
```
The configuration can be limited to individual VMs using limits:
```sh
ansible-playbook playbooks/configure-vms.yaml --limit vm6
```
## Variables
The playbooks use the following variables, which can be customized in the
`group_vars/proxmox.yml` file:
- `vm_id`: The ID of the VM
- `vm_name`: The name of the VM
- `vm_memory`: The amount of memory for the VM
- `vm_cores`: The number of CPU cores for the VM
- `vm_disk_size`: The size of the VM disk
## Author
- Taqi Tahmid (mdtaqitahmid@gmail.com)

5
ansible/ansible.cfg Normal file
View File

@ -0,0 +1,5 @@
[defaults]
inventory = ./inventory/hosts.yaml
roles_path = ./roles
host_key_checking = False
vault_password_file = ~/.ansible_vault_pass

View File

@ -0,0 +1,11 @@
# Proxmox access related variables
proxmox_api_url: "192.168.1.121"
# Cloud-init image related variables
image_url: "https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img"
image_dest: "/tmp/cloud-image.img"
image_format: "qcow2"
storage_name: "local"
# ansible venv
ansible_venv: "/home/taqi/.venv/ansible/bin/python"

View File

@ -0,0 +1,4 @@
apt_packages:
- curl
- vim
- htop

View File

@ -0,0 +1,29 @@
# VM related variables
vm_list:
- id: 106
name: "vm6"
memory: 4096
cores: 2
disk_size: 30G
ip: "192.168.1.151/24"
gateway: "192.168.1.1"
nameserver1: "1.1.1.1"
nameserver2: "8.8.8.8"
- id: 107
name: "vm7"
memory: 4096
cores: 2
disk_size: 30G
ip: "192.168.1.152/24"
gateway: "192.168.1.1"
nameserver1: "1.1.1.1"
nameserver2: "8.8.8.8"
# cloud-init variables
node: "homeserver1"
net0: "virtio,bridge=vmbr0"
# disk_name: "local:1000/vm-1000-disk-0.raw,discard=on"
disk_path: "/var/lib/vz/images/1000"
ide2: "local:cloudinit,format=qcow2"
boot_order: "order=scsi0"
scsi_hw: "virtio-scsi-pci"

View File

@ -0,0 +1,30 @@
# VM related variables
vm_list:
- id: 206
name: "vm8"
memory: 4096
cores: 2
disk_size: 30G
ip: "192.168.1.161/24"
gateway: "192.168.1.1"
nameserver1: "1.1.1.1"
nameserver2: "8.8.8.8"
- id: 207
name: "vm9"
memory: 4096
cores: 2
disk_size: 30G
ip: "192.168.1.162/24"
gateway: "192.168.1.1"
nameserver1: "1.1.1.1"
nameserver2: "8.8.8.8"
# cloud-init template variables
node: "homeserver2"
net0: "virtio,bridge=vmbr0"
# disk_name: "local:2000/vm-2000-disk-0.raw,discard=on"
disk_path: "/var/lib/vz/images/2000"
ide2: "local:cloudinit,format=qcow2"
boot_order: "order=scsi0"
scsi_hw: "virtio-scsi-pci"

View File

@ -0,0 +1,51 @@
all:
children:
hypervisors:
vms:
hypervisors:
children:
server1:
server2:
server1:
hosts:
proxmox1:
ansible_host: 192.168.1.121
ansible_user: "{{ ansible_proxmox_user }}"
ansible_ssh_private_key_file: "{{ ansible_ssh_private_key_file }}"
server2:
hosts:
proxmox2:
ansible_host: 192.168.1.122
ansible_user: "{{ ansible_proxmox_user }}"
ansible_ssh_private_key_file: "{{ ansible_ssh_private_key_file }}"
vms:
children:
vm_group_1:
vm_group_2:
vm_group_1:
hosts:
vm6:
ansible_host: 192.168.1.151
ansible_user: "{{ ansible_vm_user }}"
ansible_ssh_private_key_file: "{{ ansible_ssh_private_key_file }}"
vm7:
ansible_host: 192.168.1.152
ansible_user: "{{ ansible_vm_user }}"
ansible_ssh_private_key_file: "{{ ansible_ssh_private_key_file }}"
vm_group_2:
hosts:
vm8:
ansible_host: 192.168.1.161
ansible_user: "{{ ansible_vm_user }}"
ansible_ssh_private_key_file: "{{ ansible_ssh_private_key_file }}"
vm9:
ansible_host: 192.168.1.162
ansible_user: "{{ ansible_vm_user }}"
ansible_ssh_private_key_file: "{{ ansible_ssh_private_key_file }}"

View File

@ -0,0 +1,6 @@
- name: Create Proxmox VMs
hosts: vms
vars_files:
- ../secrets/vault.yaml # Load the encrypted vault file
roles:
- configure-vms

View File

@ -0,0 +1,6 @@
- name: Create Proxmox VMs
hosts: hypervisors
vars_files:
- ../secrets/vault.yaml # Load the encrypted vault file
roles:
- create-vms

View File

@ -0,0 +1,6 @@
- name: Destroy Proxmox VMs
hosts: hypervisors
vars_files:
- ../secrets/vault.yaml # Load the encrypted vault file
roles:
- destroy-vms

View File

@ -0,0 +1,11 @@
---
- name: Update apt cache
ansible.builtin.apt:
update_cache: yes
become: true
- name: Install necessary packages
ansible.builtin.apt:
name: "{{ apt_packages }}"
state: present
become: true

View File

@ -0,0 +1,70 @@
---
- name: Download cloud image
get_url:
url: "{{ image_url }}"
dest: "{{ image_dest }}"
use_netrc: yes
- name: create VMs
delegate_to: localhost
vars:
ansible_python_interpreter: /home/taqi/.venv/ansible/bin/python
community.general.proxmox_kvm:
api_host: "{{ proxmox_api_url }}"
api_user: "{{ proxmox_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token }}"
node: "{{ node }}"
vmid: "{{ item.id }}"
name: "{{ item.name }}"
memory: "{{ item.memory }}"
cores: "{{ item.cores }}"
scsihw: "{{ scsi_hw }}"
boot: "{{ boot_order }}"
net:
net0: "{{ net0 }}"
ipconfig:
ipconfig0: "ip={{ item.ip }},gw={{ item.gateway }}"
ide:
ide2: "{{ ide2 }}"
nameservers: "{{ item.nameserver1 }},{{ item.nameserver2 }}"
ciuser: "{{ ciuser }}"
cipassword: "{{ cipassword }}"
sshkeys: "{{ lookup('file', '/home/taqi/.ssh/homeserver.pub') }}"
loop: "{{ vm_list }}"
- name: Import disk image
ansible.builtin.shell: |
qm importdisk "{{ item.id }}" "{{ image_dest }}" "{{ storage_name }}" --format "{{ image_format }}"
loop: "{{ vm_list }}"
- name: Attach disk to VM
ansible.builtin.shell: |
qm set "{{ item.id }}" --scsi0 "{{ storage_name }}:{{ item.id }}/vm-{{ item.id }}-disk-0.{{ image_format }},discard=on"
loop: "{{ vm_list }}"
- name: Resize disk
ansible.builtin.shell: |
qm resize {{ item.id }} scsi0 {{ item.disk_size }}
loop: "{{ vm_list }}"
- name: Start VMs
delegate_to: localhost
vars:
ansible_python_interpreter: /home/taqi/.venv/ansible/bin/python
community.general.proxmox_kvm:
api_host: "{{ proxmox_api_url }}"
api_user: "{{ proxmox_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token }}"
node: "{{ node }}"
name: "{{ item.name }}"
state: started
loop: "{{ vm_list }}"
tags:
- start_vms
- name: Clean up downloaded image
file:
path: "{{ image_dest }}"
state: absent

View File

@ -0,0 +1,59 @@
---
- name: Get VM current state
delegate_to: localhost
vars:
ansible_python_interpreter: "{{ ansible_venv }}"
community.general.proxmox_kvm:
api_host: "{{ proxmox_api_url }}"
api_user: "{{ proxmox_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token }}"
name: "{{ item.name }}"
node: "{{ node }}"
state: current
register: vm_state
ignore_errors: yes
loop: "{{ vm_list }}"
tags:
- vm_delete
- name: Debug VM state
debug:
msg: "VM state: {{ vm_state.results[0].status }}"
when: vm_state is succeeded
loop: "{{ vm_list }}"
- name: Stop VM
delegate_to: localhost
vars:
ansible_python_interpreter: "{{ ansible_venv }}"
community.general.proxmox_kvm:
api_host: "{{ proxmox_api_url }}"
api_user: "{{ proxmox_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token }}"
name: "{{ item.name }}"
node: "{{ node }}"
state: stopped
force: true
when: vm_state is succeeded and vm_state.results[0].status != 'absent'
loop: "{{ vm_list }}"
tags:
- vm_delete
- name: Delete VM
delegate_to: localhost
vars:
ansible_python_interpreter: "{{ ansible_venv }}"
community.general.proxmox_kvm:
api_host: "{{ proxmox_api_url }}"
api_user: "{{ proxmox_user }}"
api_token_id: "{{ proxmox_api_token_id }}"
api_token_secret: "{{ proxmox_api_token }}"
name: "{{ item.name }}"
node: "{{ node }}"
state: absent
when: vm_state is succeeded and vm_state.results[0].status != 'absent'
loop: "{{ vm_list }}"
tags:
- vm_delete