Skip to content

Instantly share code, notes, and snippets.

@ulexxander
Created December 21, 2022 20:32
Show Gist options
  • Save ulexxander/957fe9080d83d5b70f7cf3896daec711 to your computer and use it in GitHub Desktop.
Save ulexxander/957fe9080d83d5b70f7cf3896daec711 to your computer and use it in GitHub Desktop.
Cloud Init config that setups user, temporary password, SSH server settings, Docker and Node Exporter
#cloud-config
users:
- name: alex
groups: sudo, docker
shell: /bin/bash
lock_passwd: false
ssh_authorized_keys:
- ssh-ed25519 ???
chpasswd:
# Force user to change password on first login.
expire: true
users:
- name: alex
password: ???
type: text
write_files:
- path: /etc/ssh/sshd_config.d/10-security.conf
content: |
PermitRootLogin no
PasswordAuthentication no
PermitEmptyPasswords no
X11Forwarding no
- path: /etc/systemd/system/node-exporter.service
content: |
[Unit]
Description=Node Exporter
After=network.target
[Service]
ExecStart=/usr/local/bin/node_exporter
[Install]
WantedBy=multi-user.target
package_update: true
package_upgrade: true
packages:
- ca-certificates
- curl
- gnupg
- lsb-release
- git
- wget
- tree
- jq
runcmd:
# Install Docker Engine.
- mkdir -p /etc/apt/keyrings
- curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
- echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg]
https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list
- apt update
- apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
# Install Node Exporter.
- wget -O node_exporter.tar.gz
"https://github.com/prometheus/node_exporter/releases/download/v1.5.0/node_exporter-1.5.0.linux-amd64.tar.gz"
- mkdir node_exporter
- tar xvf node_exporter.tar.gz -C node_exporter --strip-components=1
- cp node_exporter/node_exporter /usr/local/bin
- rm -rf node_exporter node_exporter.tar.gz
- systemctl enable node-exporter
- systemctl start node-exporter
# Create standard directory for deploys.
- mkdir /deploy
- chown -R alex:sudo /deploy
- chmod -R 770 /deploy
- chmod g+s /deploy
@ulexxander
Copy link
Author

@ulexxander
Copy link
Author

Can be tested by creating VMs based on Ubuntu Cloud Images locally.

Setup Libvirt, QEMU and KVM on Linux: https://www.n0derunner.com/create-a-linux-vm-with-kvm-in-6-easy-steps

Create VM with Cloud Init config injected. Requires user-data file and jammy-server-cloudimg-amd64.img downloaded Ubuntu image.

cloud-localds user-data.img user-data
qemu-img create -b jammy-server-cloudimg-amd64.img -F qcow2 -f qcow2 my-cloud-vm.qcow2 20G
virt-install \
  --name my-cloud-vm \
  --vcpus 2 --memory 4096 --graphics none --import \
  --disk path=my-cloud-vm.qcow2,device=disk \
  --disk path=user-data.img,format=raw \
  --os-variant ubuntu22.04

Obtain IP assigned to VM. Then you can SSH into it.

virsh net-dhcp-leases default

Remove VM:

virsh destroy my-cloud-vm
virsh undefine my-cloud-vm --remove-all-storage

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