#!/bin/bash # Ensure the script is run as root if [ "$(id -u)" != "0" ]; then echo "This script must be run as root" 1>&2 exit 1 fi set -e PUBKEY="ecdsa-sha2-nistp521 AAAAE2VjZHNhLXNoYTItbmlzdHA1MjEAAAAIbmlzdHA1MjEAAACFBAEuwAZEN/DNkr1KkBsHdw1kRV964httL4gqpstBgComJy549miU2Cul0ozyl76qv8L6BNCzQywW86Kbb2h8glufSwCDwbcZpmQUN4lRDctrWHUU8R2mPLwSQfFqc+AVYomotegaVOnSwQzpOkTJIv94LWI3Amsfo0L/2V8JY0cb3WNMIg== root@ansible-host" # Update and Upgrade the System apt update && apt upgrade -y # Install sudo and other necessary packages apt install -y sudo openssh-server curl gnupg2 # Create the ansible user useradd -m -s /bin/bash ansible # Set up SSH for the ansible user mkdir -p /home/ansible/.ssh chmod 700 /home/ansible/.ssh touch /home/ansible/.ssh/authorized_keys chmod 600 /home/ansible/.ssh/authorized_keys # Replace 'your_public_key' with the actual public key echo "$PUBKEY" > /home/ansible/.ssh/authorized_keys # Change ownership of the .ssh directory to the ansible user chown -R ansible:ansible /home/ansible/.ssh # Configure sudo privileges echo 'ansible ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/ansible # Ensure the sudoers file is secure chmod 0440 /etc/sudoers.d/ansible # Install Ansible apt install -y ansible echo "Ansible and user setup complete."