Skip to content
Snippets Groups Projects
Commit 127d288b authored by Aditya Kushwaha's avatar Aditya Kushwaha
Browse files

Take it

parent 39ddf228
Branches main
No related tags found
No related merge requests found
- name: Install Required Software
hosts: "{{ target_host }}"
become: yes
gather_facts: yes
tasks:
- name: Install Nginx
yum:
name: nginx
state: present
- name: Install MySQL Server
yum:
name: mysql-server
state: present
- name: Install Git
yum:
name: git
state: present
- name: Install Docker
yum:
name: docker
state: present
- name: Start and enable Nginx
service:
name: nginx
state: started
enabled: yes
- name: Start and enable MySQL
service:
name: mysqld
state: started
enabled: yes
- name: Start and enable Docker
service:
name: docker
state: started
enabled: yes
- name: Clone VM from template and Configure it
hosts: localhost
gather_facts: no
vars:
vm_title: "liveRHEL"
dhcpIP: ""
vm_config_repo: "../vm-config" # Path to VM config repo
software_repo: "../software-packages" # Path to software repo
tasks:
- name: Record start time
command: date +%s
register: start_time_raw
changed_when: false
- name: Clone the VM from the template
community.general.proxmox_kvm:
api_user: "{{ proxmox_api_user }}"
api_password: "{{ ansible_password }}"
api_host: "{{ proxmox_api_host }}"
clone: "Template-RHEL"
name: "{{ vm_title }}"
node: "{{ proxmox_node }}"
storage: "local-lvm"
timeout: 500
- name: wait 5 seconds
ansible.builtin.pause:
seconds: 5
- name: Start VM
community.general.proxmox_kvm:
node: proxmox
api_user: "{{ proxmox_api_user }}"
api_password: "{{ ansible_password }}"
api_host: "{{ proxmox_api_host }}"
name: "{{ vm_title }}"
state: started
- name: wait 30 seconds
ansible.builtin.pause:
seconds: 30
- name: Fetch DHCP allocated IP
command: python3 ../scripts/getLastVMIP.py
register: ip_result
- name: Set DHCP IP variable
set_fact:
dhcpIP: "{{ ip_result.stdout }}"
- name: Debug - Show retrieved DHCP IP
debug:
msg: "The retrieved DHCP IP is {{ dhcpIP }}"
- name: Apply VM Configuration from Repo
ansible.builtin.import_playbook: "{{ vm_config_repo }}/vm_config.yml"
vars:
target_host: "{{ dhcpIP }}"
- name: Install Software from Repo
ansible.builtin.import_playbook: "{{ software_repo }}/install_apps.yml"
vars:
target_host: "{{ dhcpIP }}"
- name: Record end time
command: date +%s
register: end_time_raw
changed_when: false
- name: Calculate total runtime
set_fact:
total_runtime: "{{ end_time_raw.stdout | int - start_time_raw.stdout | int }}"
- name: Display total runtime
debug:
msg: "The script ran for {{ total_runtime }} seconds."
- name: Configure VM Security and Firewall
hosts: "{{ target_host }}"
become: yes
gather_facts: yes
tasks:
- name: Ensure firewalld is installed
yum:
name: firewalld
state: present
- name: Start and enable firewalld
service:
name: firewalld
state: started
enabled: yes
- name: Allow required ports (SSH, HTTP, HTTPS)
firewalld:
port: "{{ item }}"
permanent: yes
state: enabled
with_items:
- 22/tcp
- 80/tcp
- 443/tcp
- name: Reload firewall
command: firewall-cmd --reload
- name: Disable root SSH login
lineinfile:
path: /etc/ssh/sshd_config
regexp: ’^PermitRootLogin’
line: ’PermitRootLogin no’
notify: Restart SSH
handlers:
- name: Restart SSH
service:
name: sshd
state: restarted
[proxmox]
100.79.80.80
[all:vars]
proxmox_api_user="<USER>"
proxmox_api_host="<API_HOST>"
proxmox_node="<NODE>"
ansible_user=root
ansible_password=Root_Password
vm_user=admin
vm_password=Password
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment