diff --git a/playbooks/deploy_vm_from_cloud_init.yaml b/playbooks/deploy_vm_from_cloud_init.yaml index f432053..f9d87ea 100644 --- a/playbooks/deploy_vm_from_cloud_init.yaml +++ b/playbooks/deploy_vm_from_cloud_init.yaml @@ -1,6 +1,7 @@ --- -- name: Deploy a virtual machine from a cloud init image - hosts: localhost +- name: Deploy VM + hosts: all + remote_user: ansible gather_facts: false vars_files: @@ -8,10 +9,59 @@ vars: ansible_user_passwd_hash: "{{ ansible_password | password_hash('sha512', 's3edscrj45e6r') }}" user_passwd_hash: "{{ user_password | password_hash('sha512', 's3ed6123jhgcr') }}" + vars_prompt: - name: vm_hostname - prompt: "Hostname for the VM" + prompt: "Hostname for the VM (may only contain letters (a-z, A-Z), numbers (0-9), and hyphens)" private: false + - name: vm_ip_address + prompt: "IP address for the VM (in x.x.x.x/x formant)" + private: false + + pre_tasks: + - name: Validate hostname + ansible.builtin.fail: + msg: | + Invalid hostname: "{{ vm_hostname }}" + Hostname may only contain letters (a-z, A-Z), numbers (0-9), and hyphens (-). + when: not vm_hostname is regex('^[a-zA-Z0-9-]+$') + run_once: true + roles: - - ../roles/deploy_vm_on_proxmox + - ../roles/deploy_vm_on_proxmox + + tasks: + - name: Make the prompted hostname available to the whole playbook + ansible.builtin.set_fact: + fact_vm_hostname: "{{ vm_hostname }}" + + - name: Make the prompted IP available to the whole playbook + ansible.builtin.set_fact: + fact_vm_ip_address: "{{ vm_ip_address.split('/') | first }}" + + - name: Add the target host to the inventory + ansible.builtin.add_host: + name: "{{ fact_vm_ip_address }}" + groups: new_host + ansible_user: ansible + ansible_become_password: "{{ ansible_become_passwd }}" + + +- name: Configure VM + hosts: new_host + remote_user: ansible + + vars_files: + ../inventory/group_vars/all/secrets.yaml + vars: + ansible_user_passwd_hash: "{{ ansible_password | password_hash('sha512', 's3edscrj45e6r') }}" + user_passwd_hash: "{{ user_password | password_hash('sha512', 's3ed6123jhgcr') }}" + + roles: + - ../roles/configure_ansible_user + - ../roles/harden_ssh + - ../roles/base_system + - ../roles/human_admin_user + - ../roles/set_locale_and_time +