Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ansible infra setup #32

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions infra/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Ansible .gitignore
################################################################################

.cache/
.venv/
17 changes: 17 additions & 0 deletions infra/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
SHELL = bash
VENV = .venv

all: $(VENV)

install: $(VENV)
$(VENV):
python3 -m venv $(VENV) --upgrade-deps
$(VENV)/bin/pip install wheel
$(VENV)/bin/pip install -r requirements.txt
$(VENV)/bin/ansible-galaxy install -r requirements.yml

lint: $(VENV)
$(VENV)/bin/ansible-lint --force-color

clean:
git clean -xdf -e .vault
26 changes: 26 additions & 0 deletions infra/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Config file for ansible -- https://ansible.com/
# ===============================================

[defaults]
inventory = hcloud.yml, hosts.yml
host_key_checking = False

stdout_callback = community.general.yaml

fact_caching = memory

retry_files_enabled = False

interpreter_python = /usr/bin/python3

ask_vault_pass = False
vault_password_file = tools/vault-password

[inventory]
enable_plugins = yaml, hetzner.hcloud.hcloud

[privilege_escalation]
become_ask_pass = False

[ssh_connection]
pipelining = True
5 changes: 5 additions & 0 deletions infra/hcloud.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
plugin: hetzner.hcloud.hcloud
keyed_groups:
- key: labels
separator: ""
14 changes: 14 additions & 0 deletions infra/host_vars/prod1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
ansible_user: root

# firewall
firewalld_zones:
public:
head: |
<interface name="{{ ansible_default_ipv4.interface }}"/>
<forward/>

<service name="ssh"/>
<service name="dhcpv6-client"/>
<service name="http"/>
<service name="https"/>
8 changes: 8 additions & 0 deletions infra/hosts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
web:
hosts:
prod1:

docker:
hosts:
prod1:
38 changes: 38 additions & 0 deletions infra/infra.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
- name: Setup servers
hosts: localhost
connection: local
vars:
ansible_python_interpreter: .venv/bin/python3
tasks:
- name: Create server
hetzner.hcloud.hcloud_server:
name: prod1
server_type: cx11
image: debian-11
location: fsn1
labels:
production: ""
ssh_keys:
- jooola
state: present

- name: Ensure the server is started
hetzner.hcloud.hcloud_server:
name: prod1
state: started
register: server

- name: Create server domain name
community.general.gandi_livedns:
api_key: "{{ lookup('ansible.builtin.env', 'GANDI_TOKEN') }}"
domain: libretime.org
record: prod1
type: A
values:
- "{{ server.hcloud_server.ipv4_address }}"
ttl: 360
state: present

- name: Refresh inventory
ansible.builtin.meta: refresh_inventory
5 changes: 5 additions & 0 deletions infra/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Ansible
ansible-core>=2.14,<2.15
ansible-lint>=6.14.4,<6.15

hcloud>=1.19.0,<1.20
10 changes: 10 additions & 0 deletions infra/requirements.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
collections:
- name: community.general
source: git+https://github.com/ansible-collections/community.general
type: git
version: 6.5.0

- name: hetzner.hcloud
source: git+https://github.com/ansible-collections/hetzner.hcloud
type: git
version: 1.11.0
4 changes: 4 additions & 0 deletions infra/roles/common/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
lang: en_US.UTF-8
language: en_US:en
timezone: Europe/Berlin
41 changes: 41 additions & 0 deletions infra/roles/common/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
- name: Set timezone
community.general.timezone:
name: "{{ timezone }}"

- name: Install locale
community.general.locale_gen:
name: "{{ lang }}"
state: present

- name: Check default locale
ansible.builtin.lineinfile:
dest: /etc/default/locale
line: "{{ item }}"
check_mode: true
with_items:
- "LANG={{ lang }}"
- "LANGUAGE={{ language }}"
register: default_locale

- name: Set locale # noqa no-handler no-changed-when
when: default_locale is changed
ansible.builtin.command: update-locale LANG="{{ lang }}" LANGUAGE="en_US:en"

- name: Install common packages
ansible.builtin.apt:
state: present
update_cache: true
name:
- apt-transport-https
- bzip2
- curl
- git
- gpg
- rsync
- sudo
- unzip
- vim
- wget
- zip
- zsh
2 changes: 2 additions & 0 deletions infra/roles/docker/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
docker_bridge_ip: 172.17.0.1/16
5 changes: 5 additions & 0 deletions infra/roles/docker/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- name: Restart docker
ansible.builtin.systemd:
name: docker
state: restarted
44 changes: 44 additions & 0 deletions infra/roles/docker/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
- name: Ensure apt keyrings directory exists
ansible.builtin.file:
path: /etc/apt/keyrings
state: directory
owner: root
group: root
mode: "0755"

- name: Deploy apt repository key for docker # noqa command-instead-of-module risky-shell-pipe
ansible.builtin.shell: >
curl -sSL https://download.docker.com/linux/debian/gpg
| gpg --dearmor -o /etc/apt/keyrings/docker-archive-keyring.gpg
args:
creates: /etc/apt/keyrings/docker-archive-keyring.gpg

- name: Deploy apt repository for docker
ansible.builtin.apt_repository:
repo: >
deb
[signed-by=/etc/apt/keyrings/docker-archive-keyring.gpg]
https://download.docker.com/linux/debian {{ ansible_distribution_release }} stable
state: present

- name: Install docker
ansible.builtin.apt:
name: [docker-ce, docker-compose-plugin, docker-buildx-plugin]
state: present

- name: Deploy docker daemon conf
ansible.builtin.template:
src: docker/daemon.json.j2
dest: /etc/docker/daemon.json
owner: root
group: root
mode: "0644"
backup: true
notify: Restart docker

- name: Enable/start docker daemon
ansible.builtin.systemd:
name: docker
state: started
enabled: true
5 changes: 5 additions & 0 deletions infra/roles/docker/templates/docker/daemon.json.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ipv6": false,
"bip": "{{ docker_bridge_ip }}",
"ip": "127.0.0.1"
}
5 changes: 5 additions & 0 deletions infra/roles/firewall/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- name: Restart firewalld
ansible.builtin.systemd:
name: firewalld
state: reloaded
24 changes: 24 additions & 0 deletions infra/roles/firewall/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
- name: Install firewalld
ansible.builtin.apt:
name: firewalld
state: present

- name: Enable/start firewalld
ansible.builtin.systemd:
name: firewalld
state: started
enabled: true

- name: Deploy firewalld conf
ansible.builtin.template:
src: firewalld/zone.xml.j2
dest: /etc/firewalld/zones/{{ zone.key }}.xml
owner: root
group: root
mode: "0644"
with_dict: "{{ firewalld_zones }}"
loop_control:
loop_var: zone
label: "{{ zone.key }}"
notify: Restart firewalld
10 changes: 10 additions & 0 deletions infra/roles/firewall/templates/firewalld/zone.xml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<zone>
{{ zone.value.head | indent(2) }}
{% if zone.value.rules is defined %}
{% for rule in zone.value.rules %}
<!-- {{ rule }} -->
{{ zone.value.rules[rule] | indent(2) }}
{% endfor %}
{% endif %}
</zone>
1 change: 1 addition & 0 deletions infra/roles/root/files/.bash_aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alias dc="docker compose"
Loading