diff --git a/vm_automation/alpine/plans/create_vm.yaml b/vm_automation/alpine/plans/create_vm.yaml index e9c8e65..307f97c 100644 --- a/vm_automation/alpine/plans/create_vm.yaml +++ b/vm_automation/alpine/plans/create_vm.yaml @@ -161,6 +161,13 @@ steps: parameters: staging_ip: $staging_ip + - name: set_root_password + description: Set password for root user + task: alpine::set_root_password + targets: localhost + parameters: + staging_ip: $staging_ip + - name: system_setup task: alpine::system_setup targets: localhost diff --git a/vm_automation/alpine/tasks/post_set_moeny_password.sh b/vm_automation/alpine/tasks/post_set_moeny_password.sh new file mode 100644 index 0000000..440412b --- /dev/null +++ b/vm_automation/alpine/tasks/post_set_moeny_password.sh @@ -0,0 +1,23 @@ +#!/bin/bash +# Task to set password for moeny user (alpine::post_set_moeny_password) + +# Function to check last command status +check_status() { + if [ $? -ne 0 ]; then + echo '{"status": "error", "message": "'"$1"'"}' + exit 1 + fi +} + +# Generate a random 15-character password locally +echo "Generating password..." +MOENY_PASSWORD=$(head -c 30 /dev/urandom | base64 | tr -dc 'a-zA-Z0-9' | head -c 15) +echo "Debug - Generated password: ${MOENY_PASSWORD}" +check_status "Failed to generate password" + +# Set the password for moeny user +echo "moeny:${MOENY_PASSWORD}" | sudo chpasswd +check_status "Failed to set password for moeny user" + +echo '{"status": "success", "message": "Moeny user password set to: '"${MOENY_PASSWORD}"'"}' +exit 0 \ No newline at end of file diff --git a/vm_automation/alpine/tasks/post_set_root_password.sh b/vm_automation/alpine/tasks/post_set_root_password.sh new file mode 100644 index 0000000..1bfc53f --- /dev/null +++ b/vm_automation/alpine/tasks/post_set_root_password.sh @@ -0,0 +1,23 @@ +#!/bin/bash +# Task to set password for root user (alpine::post_set_root_password) + +# Function to check last command status +check_status() { + if [ $? -ne 0 ]; then + echo '{"status": "error", "message": "'"$1"'"}' + exit 1 + fi +} + +# Generate a random 15-character password locally +echo "Generating password..." +ROOT_PASSWORD=$(head -c 30 /dev/urandom | base64 | tr -dc 'a-zA-Z0-9' | head -c 15) +echo "Debug - Generated password: ${ROOT_PASSWORD}" +check_status "Failed to generate password" + +# Set the password for root user +echo "root:${ROOT_PASSWORD}" | sudo chpasswd +check_status "Failed to set password for root user" + +echo '{"status": "success", "message": "Root password set to: '"${ROOT_PASSWORD}"'"}' +exit 0 \ No newline at end of file diff --git a/vm_automation/alpine/tasks/set_root_password.sh b/vm_automation/alpine/tasks/set_root_password.sh new file mode 100644 index 0000000..8ee2062 --- /dev/null +++ b/vm_automation/alpine/tasks/set_root_password.sh @@ -0,0 +1,26 @@ +#!/bin/bash +# Task to set password for root user (alpine::set_root_password) + +# Input Variables +STAGING_IP="${PT_staging_ip}" + +# Function to check last command status +check_status() { + if [ $? -ne 0 ]; then + echo '{"status": "error", "message": "'"$1"'"}' + exit 1 + fi +} + +# Generate a random 15-character password locally +echo "Generating password..." +ROOT_PASSWORD=$(head -c 30 /dev/urandom | base64 | tr -dc 'a-zA-Z0-9' | head -c 15) +echo "Debug - Generated password: ${ROOT_PASSWORD}" +check_status "Failed to generate password" + +# Set the password for root user +ssh -o StrictHostKeyChecking=no root@${STAGING_IP} "echo 'root:${ROOT_PASSWORD}' | chpasswd" +check_status "Failed to set password for root user" + +echo '{"status": "success", "message": "Root password set to: '"${ROOT_PASSWORD}"'"}' +exit 0 \ No newline at end of file diff --git a/vm_automation/ubuntu/plans/create_vm.yaml b/vm_automation/ubuntu/plans/create_vm.yaml index 71c8996..4aa041b 100644 --- a/vm_automation/ubuntu/plans/create_vm.yaml +++ b/vm_automation/ubuntu/plans/create_vm.yaml @@ -140,6 +140,11 @@ steps: task: ubuntu::set_moeny_password targets: $staging_ip + - name: set_root_password + description: Set password for root user + task: ubuntu::set_root_password + targets: $staging_ip + - name: system_setup task: ubuntu::system_setup targets: $staging_ip diff --git a/vm_automation/ubuntu/tasks/set_root_password.sh b/vm_automation/ubuntu/tasks/set_root_password.sh new file mode 100644 index 0000000..be6064b --- /dev/null +++ b/vm_automation/ubuntu/tasks/set_root_password.sh @@ -0,0 +1,21 @@ +#!/bin/bash +# Task to set password for root user (ubuntu::set_root_password) + +# Function to check last command status +check_status() { + if [ $? -ne 0 ]; then + echo '{"status": "error", "message": "'"$1"'"}' + exit 1 + fi +} + +# Generate a random 15-character password (alphanumeric only) +ROOT_PASSWORD=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 15 | head -n 1) +check_status "Failed to generate password" + +# Set the password for root user +echo "root:${ROOT_PASSWORD}" | sudo chpasswd +check_status "Failed to set password for root user" + +echo '{"status": "success", "message": "Root password set to: '"${ROOT_PASSWORD}"'"}' +exit 0 \ No newline at end of file