homelab/ansible/archive/playbooks/onboarding/generic_host_conversational.yml

107 lines
3.4 KiB
YAML

---
- name: Intelligent VM Provisioner & Onboarder
hosts: localhost
gather_facts: false
vars:
# 1. THE GOLD IMAGE PRESETS
default_spec:
cpu: 2
ram: 4096
disk: "40GB"
os: "ubuntu-22.04"
# 2. THE RESOURCE GUARD LIMITS
max_safe_ram: 16384 # 16GB Safety Ceiling
max_safe_cpu: 8
vars_prompt:
- name: target_host_ip
prompt: "Step 1: Enter the target IP address"
private: false
- name: target_hostname
prompt: "Step 2: Enter the desired Hostname (e.g., prod-web-01)"
private: false
tasks:
# ========================================
# PHASE 1: CONVERSATIONAL OVERRIDES
# ========================================
- name: Display Target Identity
ansible.builtin.debug:
msg: "Preparing to build [ {{ target_hostname }} ] at [ {{ target_host_ip }} ]"
- name: Prompt for Modification
ansible.builtin.pause:
prompt: "Current Default: {{ default_spec.cpu }} CPU / {{ default_spec.ram }}MB RAM. Modify? (y/n)"
register: modify_request
- name: Interactive Override Block
block:
- name: Set New CPU
ansible.builtin.pause:
prompt: "Enter CPU count"
register: user_cpu
- name: Set New RAM
ansible.builtin.pause:
prompt: "Enter RAM in MB"
register: user_ram
- name: Consolidate Final Specs
ansible.builtin.set_fact:
final_spec:
cpu: "{{ user_cpu.user_input | default(default_spec.cpu, true) | int }}"
ram: "{{ user_ram.user_input | default(default_spec.ram, true) | int }}"
when: modify_request.user_input | lower == 'y'
- name: Default Fallback
ansible.builtin.set_fact:
final_spec:
cpu: "{{ default_spec.cpu | int }}"
ram: "{{ default_spec.ram | int }}"
when: modify_request.user_input | lower != 'y'
# ========================================
# PHASE 2: THE RESOURCE GUARD (VALIDATION)
# ========================================
- name: Validate Resource Request
ansible.builtin.assert:
that:
- final_spec.ram <= max_safe_ram
- final_spec.cpu <= max_safe_cpu
fail_msg: "❌ RESOURCE OVERLOAD: Requested {{ final_spec.ram }}MB RAM exceeds the safety limit of {{ max_safe_ram }}MB."
success_msg: "✅ Resource request validated within safety parameters."
- name: Final Provisioning Gate
ansible.builtin.pause:
prompt: |
CONFIRMATION REQUIRED:
ID: {{ target_hostname }} ({{ target_host_ip }})
SPEC: {{ final_spec.cpu }} vCPU / {{ final_spec.ram }} MB RAM
Press ENTER to deploy, or Ctrl+C to cancel.
# ========================================
# PHASE 3: EXECUTION (DELEGATED TO REMOTE)
# ========================================
- name: Apply Identity and Config
delegate_to: "{{ target_host_ip }}"
become: true
block:
- name: Set System Hostname
ansible.builtin.hostname:
name: "{{ target_hostname }}"
- name: Update /etc/hosts
ansible.builtin.lineinfile:
path: /etc/hosts
regexp: '^127.0.1.1'
line: "127.0.1.1 {{ target_hostname }}"
- name: Install Base Packages
ansible.builtin.apt:
name: [python3, curl, git, htop]
state: present
update_cache: yes