Work in progress

This commit is contained in:
YuruC3 2024-03-22 13:29:02 +01:00
parent 6f485fa7f9
commit 781dd2e51b
3 changed files with 43 additions and 21 deletions

View File

@ -1,6 +1,6 @@
---
- name: DockerAPI Install
hosts: <CHANGE_ME>
hosts: <CHANGE_HOSTS>
become: yes
tasks:

View File

@ -1,9 +1,20 @@
# Ansible-Deploy-Docker
Simple ansible playbook to deploy latest docker/docker-compose via ansible.
Simple ansible playbook to deploy latest docker and docker-compose via ansible.
Remember to change <CHANGE_ME> in playbook and <YOUR KEY HERE> in bash script.
## Edit configs
Also a bash script that creates a "ansible" user. You'll need to write your public key in script.
### add_ansible_user.sh
It is a good option to create a paswordless ssh key and copy it onto every machine you want to work with.
Bash script is configured to work only with public keys.
__<ANSIBLE_USERNAME>__ -- Set a username for ansible. Example "ansible"
__<YOUR_PUBKEY>__ -- Paste your public key.
__UID_INT__ -- This is optional. By default it is set to 1600. It makes it easier to distinguish ansible user from the rest
### DockerEngineInstall.yml
The only thing to change here is __<CHANGE_HOSTS>__ at line 3. There you need to input IP of computers that ansible will run this playbook on.
## PS
In Proxmox if you have a VM template it is possible to paste multiple pubkeys. Go to template -> Cloud-Init -> SSH public key -> edit -> paste your pubkeys.

View File

@ -1,36 +1,47 @@
#!/bin/bash
# Define the username
USERNAME="ansible"
# Define variables
USERNAME="<ANSIBLE_USERNAME>"
PUBLIC_KEY="<YOUR_PUBKEY>"
UID_INT="1600" # 1600 to easilly distinguish ansible_user
not_found=0
# Define the public key (replace with your actual public key)
PUBLIC_KEY="<YOUR KEY HERE>"
# Check if user exists
IFS=$'\n'
for users in $(cat /etc/passwd)
do
if [[ $(echo "$users" | awk 'BEGIN { FS = ":" } ; { print $1 }') = "$USERNAME" ]]
then
((not_found++))
fi
done
unset IFS
# Create the user if it doesn't already exist
if id "$USERNAME" &>/dev/null; then
echo "User '$USERNAME' already exists."
# Create user with custom UID and GID
if [[ $not_found = 0 ]]; then
echo "Creating user '$USERNAME'..."
sudo useradd -m "$USERNAME" -s "/bin/bash" -u $UID_INT
else
echo "Creating user '$USERNAME'..."
sudo useradd -m "$USERNAME"
echo "User '$USERNAME' already exists."
fi
# Create the .ssh directory in the user's home directory if it doesn't exist
# Create the .ssh directory if it doesn't exists
HOME_DIR="/home/$USERNAME"
SSH_DIR="/home/$USERNAME/.ssh"
if [ ! -d "$SSH_DIR" ]; then
echo "Creating $SSH_DIR directory..."
sudo -u "$USERNAME" mkdir -p "$SSH_DIR"
if [[ ! -d "$SSH_DIR" ]]; then
echo "Creating $SSH_DIR directory..."
sudo -u "$USERNAME" mkdir -p "$SSH_DIR"
fi
# Paste the public key directly into authorized_keys
echo "$PUBLIC_KEY" | sudo -u "$USERNAME" tee "$SSH_DIR/authorized_keys" > /dev/null
echo "$PUBLIC_KEY" | sudo -u "$USERNAME" tee -a "$SSH_DIR/authorized_keys" > /dev/null
# Set correct permissions
echo "Setting permissions..."
sudo -u "$USERNAME" chmod 755 "$HOME_DIR"
sudo -u "$USERNAME" chmod 700 "$SSH_DIR"
sudo -u "$USERNAME" chmod 644 "$SSH_DIR/authorized_keys"
sudo -u "$USERNAME" chown -R "$USERNAME:$USERNAME" "$SSH_DIR"
sudo -u "$USERNAME" chown -R "$USERNAME:" "$SSH_DIR"
# Add the user to the sudoers file with NOPASSWD privileges
echo "$USERNAME ALL=(ALL) NOPASSWD: ALL" | sudo tee -a /etc/sudoers > /dev/null