Skip to content

Instantly share code, notes, and snippets.

@psct
Created February 26, 2021 16:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save psct/ed50d734aa5fdb668ff592f76f7e386a to your computer and use it in GitHub Desktop.
Save psct/ed50d734aa5fdb668ff592f76f7e386a to your computer and use it in GitHub Desktop.
Prepare fresh Debian for ansible management by created user "ansible" in sudoers (needs public ssh-Key in PUBKEY)
#!/bin/bash
# enable host for ansible
set -e
PUBKEY=""
id -u ansible > /dev/null 2>&1 || \
adduser ansible --disabled-password \
--gecos "" --quiet
mkdir -p /home/ansible/.ssh
echo "$PUBKEY" \
> /home/ansible/.ssh/authorized_keys
chown -R ansible /home/ansible/.ssh
apt-get update
apt-get install sudo
grep -q ansible /etc/sudoers || \
echo "ansible ALL = (ALL) \
NOPASSWD: ALL" >> /etc/sudoers
@asc4asc
Copy link

asc4asc commented Mar 17, 2021

echo "ansible ALL = (ALL) NOPASSWD: ALL" >> /etc/sudoers.d/ansible

is better. :-)

@Sternagfonkel
Copy link

But then please also adapt the path in the grep command one line above ;)

grep -q ansible /etc/sudoers.d/ansible || \
echo "ansible ALL = (ALL) NOPASSWD: ALL" >> /etc/sudoers.d/ansible

Even better: Use a variable for the path to avoid such mistakes...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment