diff --git a/ansible/hetzner.example b/ansible/hetzner.example new file mode 100644 index 0000000..4af05bb --- /dev/null +++ b/ansible/hetzner.example @@ -0,0 +1 @@ +localhost ansible_python_interpreter=/usr/bin/python3 diff --git a/ansible/host_vars/localhost/localhost_vars.yaml.example b/ansible/host_vars/localhost/localhost_vars.yaml.example new file mode 100644 index 0000000..ff4b19d --- /dev/null +++ b/ansible/host_vars/localhost/localhost_vars.yaml.example @@ -0,0 +1,6 @@ +--- +hcloud_api_token: someAPIToken +admin_user: nameForNewAdminUser +ssh_key_admin_user: publicKeyForNewAdminUser +ssh_key_admin_user_name: nameForSSHKeyForNewAdminUser +server_name: nameOfTheNewServer \ No newline at end of file diff --git a/ansible/roles/server-setup/defaults/main.yaml b/ansible/roles/server-setup/defaults/main.yaml new file mode 100644 index 0000000..7023c5a --- /dev/null +++ b/ansible/roles/server-setup/defaults/main.yaml @@ -0,0 +1,6 @@ +--- +basic_firewall: basic-firewall +server_image: ubuntu-22.04 +server_location: fsn1 +server_placement_group: default +server_type: cax11 \ No newline at end of file diff --git a/ansible/roles/server-setup/tasks/create-server.yaml b/ansible/roles/server-setup/tasks/create-server.yaml new file mode 100644 index 0000000..f22ad0b --- /dev/null +++ b/ansible/roles/server-setup/tasks/create-server.yaml @@ -0,0 +1,22 @@ +--- +- name: create SSH key + hetzner.hcloud.hcloud_ssh_key: + api_token: "{{ hcloud_api_token }}" + public_key: "{{ ssh_key_admin_user }}" + name: "{{ ssh_key_admin_user_name }}" + state: present +- name: create server via HCloud + hetzner.hcloud.hcloud_server: + api_token: "{{ hcloud_api_token }}" + firewalls: + - "{{ basic_firewall }}" + image: "{{ server_image }}" + location: "{{ server_location }}" + name: "{{ server_name }}" + placement_group: "{{ server_placement_group }}" + server_type: "{{ server_type }}" + ssh_keys: + - "{{ ssh_key_admin_user_name }}" + user_data: "{{ lookup('ansible.builtin.template', 'basic-ssh-hardening.yaml.j2', convert_data=false) }}" + state: present + register: server \ No newline at end of file diff --git a/ansible/roles/server-setup/tasks/main.yaml b/ansible/roles/server-setup/tasks/main.yaml new file mode 100644 index 0000000..476b218 --- /dev/null +++ b/ansible/roles/server-setup/tasks/main.yaml @@ -0,0 +1,2 @@ +--- +- import_tasks: create-server.yaml \ No newline at end of file diff --git a/cloud-init-scripts/basic-ssh-hardening.yaml b/ansible/roles/server-setup/templates/basic-ssh-hardening.yaml.j2 similarity index 88% rename from cloud-init-scripts/basic-ssh-hardening.yaml rename to ansible/roles/server-setup/templates/basic-ssh-hardening.yaml.j2 index c2c51f5..7d90d7a 100644 --- a/cloud-init-scripts/basic-ssh-hardening.yaml +++ b/ansible/roles/server-setup/templates/basic-ssh-hardening.yaml.j2 @@ -1,11 +1,11 @@ #cloud-config users: - - name: holu + - name: {{ admin_user }} groups: users, admin sudo: ALL=(ALL) NOPASSWD:ALL shell: /bin/bash ssh_authorized_keys: - - + - {{ ssh_key_admin_user }} packages: - fail2ban - ufw @@ -23,5 +23,5 @@ runcmd: - sed -i -e '/^\(#\|\)AllowTcpForwarding/s/^.*$/AllowTcpForwarding no/' /etc/ssh/sshd_config - sed -i -e '/^\(#\|\)AllowAgentForwarding/s/^.*$/AllowAgentForwarding no/' /etc/ssh/sshd_config - sed -i -e '/^\(#\|\)AuthorizedKeysFile/s/^.*$/AuthorizedKeysFile .ssh\/authorized_keys/' /etc/ssh/sshd_config - - sed -i '$a AllowUsers holu' /etc/ssh/sshd_config + - sed -i '$a AllowUsers {{ admin_user }}' /etc/ssh/sshd_config - reboot \ No newline at end of file diff --git a/ansible/setup.yaml b/ansible/setup.yaml new file mode 100644 index 0000000..5ab8d79 --- /dev/null +++ b/ansible/setup.yaml @@ -0,0 +1,6 @@ +--- +- name: Create server + hosts: localhost + connection: local + roles: + - server-setup diff --git a/ansible/site.yaml b/ansible/site.yaml new file mode 100644 index 0000000..e78e4eb --- /dev/null +++ b/ansible/site.yaml @@ -0,0 +1,2 @@ +--- +- include: setup.yaml \ No newline at end of file