Первая версия

This commit is contained in:
B4D_US3R 2025-12-24 10:19:12 +05:00
commit 0da0cfd3f9
9 changed files with 164 additions and 0 deletions

25
README.md Normal file
View file

@ -0,0 +1,25 @@
# Factorio Terraform Beget
Terraform-сценарий, создающий VPS-сервер в инфраструктуре Beget. Для запуска необходимо для начала создать файл `.auto.tfvars` с переменной `beget_token`,
в которой указан JWT-токен:
```
beget_token = "eyJhbG...gQg"
```
Создать его можно на странице https://developer.beget.com/#post-/v1/auth
После этого запустить создание VPS-сервера с помощью команды:
```
terraform apply
```
Если используется [OpenTofu](https://opentofu.org/docs/intro/install/) (HashiCorp блокирует российские IP-адреса), то для запуска используется следующая команда:
```
tofu apply
```
Для удаления сервера используется команда `terraform destroy` или `tofu destroy`
---
## Зависимости
- [Terraform](https://developer.hashicorp.com/terraform/install) или [OpenTofu](https://opentofu.org/docs/intro/install/)
- [Ansible](https://docs.ansible.com/projects/ansible/latest/installation_guide/intro_installation.html#pipx-install)

7
ansible.cfg Executable file
View file

@ -0,0 +1,7 @@
[defaults]
remote_user=root
roles_path=./roles
host_key_checking = False
[privilege_escalation]
become=True

2
inventory/factorio.ini Executable file
View file

@ -0,0 +1,2 @@
[factorio]
your.server

52
main.tf Executable file
View file

@ -0,0 +1,52 @@
terraform {
required_providers {
beget = {
source = "tf.beget.com/beget/beget"
}
}
}
variable "beget_token" {
sensitive = true
type = string
description = "JWT токен для облачной инфраструктуры Beget"
}
provider "beget" {
token = var.beget_token
}
resource "beget_ssh_key" "devops" {
name = "Terraform key"
public_key = file("~/.ssh/id_rsa.pub")
}
data "beget_software" "ubuntu" {
slug = "ubuntu-24-04"
}
resource "beget_compute_instance" "test-server" {
name = "Test Server"
description = "Тестовый TF сервер"
hostname = "debian-test"
region = "ru1"
configuration = {
cpu = 1
ram_mb = 1*1024
disk_mb = 10 * 1024
cpu_class = "normal_cpu"
}
image = {
software = {
id = data.beget_software.ubuntu.id
}
}
access = {
ssh_keys = [beget_ssh_key.devops.id]
}
provisioner "local-exec" {
command = "sleep 15; ansible-playbook -i '${self.ip_address},' --private-key ~/.ssh/id_rsa playbook.yml"
}
}

8
playbook.yml Executable file
View file

@ -0,0 +1,8 @@
---
- name: Install Factorio
hosts: all
roles:
- base
- docker
- factorio

7
roles/base/tasks/main.yml Executable file
View file

@ -0,0 +1,7 @@
- name: Update cache
ansible.builtin.apt:
update_cache: true
- name: Upgrade ubuntu
ansible.builtin.apt:
upgrade: "full"

33
roles/docker/tasks/main.yml Executable file
View file

@ -0,0 +1,33 @@
- name: Install ca-certificates and curl
ansible.builtin.apt:
package:
- ca-certificates
- curl
- python3-requests
- name: Get keys
ansible.builtin.get_url:
dest: /etc/apt/keyrings/docker.asc
url: https://download.docker.com/linux/ubuntu/gpg
mode: 644
- name: Add docker repository
ansible.builtin.apt_repository:
repo: "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
- name: Install docker
ansible.builtin.apt:
package:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin
- name: SystemD enable docker
ansible.builtin.systemd_service:
name: docker
enabled: true
state: "started"

17
roles/factorio/tasks/main.yml Executable file
View file

@ -0,0 +1,17 @@
- name: Create /opt/beget/factorio
ansible.builtin.file:
path: /opt/beget/factorio/factorio_data
state: directory
mode: "700"
recurse: true
- name: Copy template to server
ansible.builtin.template:
src: docker-compose.yml.j2
dest: /opt/beget/factorio/docker-compose.yml
mode: "600"
- name: Start factorio server
community.docker.docker_compose_v2:
project_src: /opt/beget/factorio
state: present

View file

@ -0,0 +1,13 @@
version: "3.8"
services:
factorio:
image: factoriotools/factorio:latest
restart: always
environment:
DLC_SPACE_AGE: "false"
ports:
- "34197:34197/udp"
- "27015:27015/tcp"
volumes:
- ./factorio_data:/factorio