boilerplate IaC code

This repo contains boilerplate and starting points for a tour through infrastructure as code.

quick references

terraform

Doku

declaration language basic

providers
provider "hcloud" {
  token = var.hcloud_token
}
resources
resource "hcloud_server" "super_duper_server" {
  name = "super-duper-server-1"
  ...
}
variables
variable "image-name" {
  type    = string
  default = "debian-12"
}
outputs
output "instance_ip" {
  value = hcloud_server.super_duper_server.public_ip
}

command line usage

  • terraform init
  • terraform plan
  • terraform apply
  • terraform destroy

ansible

inventory

---
# inventory.yaml
web:
  hosts:
    super-duper-server-1.hetzner.com:

playbook

---
# install_nginx.yaml
- name: Install and Configure Nginx
  hosts: web
  tasks:
    - name: Install Nginx
      ansible.builtin.apt:
        name: nginx
        state: present
    - name: Ensure Nginx is running
      ansible.builtin.service:
        name: nginx
        state: started

variables & templates

---
- name: Use Variables
  hosts: web
  vars:
    nginx_port: 80
  tasks:
    - name: Install Nginx
      ansible.builtin.apt:
        name: nginx
        state: present
    - name: Configure Nginx
      ansible.builtin.template:
        src: nginx.conf.j2
        dest: /etc/nginx/nginx.conf

loops & conditionals

---
- name: Use Loops and Conditionals
  hosts: web
  tasks:
    - name: Create Users
      ansible.builtin.user:
        name: "{{ item }}"
        state: present
      loop:
        - user1
        - user2
      when: ansible_os_family == "Debian"

handlers

---
- name: Restart Nginx if Config Changed
  hosts: web
  tasks:
    - name: Update Nginx Config
      ansible.builtin.template:
        src: nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      notify:
        - restart nginx

handlers:
  - name: restart nginx
    ansible.builtin.service:
      name: nginx
      state: restarted

command line usage

  • ansible-playbook -i inventory.yaml install_nginx.yml
  • ansible-galaxy collection install hetzner.hcloud