Working VPN module and Ansible

This commit is contained in:
Khue Doan 2021-05-01 11:50:21 +07:00
parent 51c725edc0
commit d9cadb36bb
4 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,4 @@
- hosts: all
tasks:
- name: Ping
ansible.builtin.ping:

View File

@ -0,0 +1,4 @@
#cloud-config
ssh_authorized_keys:
- ${ssh_public_key}

View File

@ -1,5 +1,51 @@
resource "tls_private_key" "ssh" {
algorithm = "ECDSA"
ecdsa_curve = "P256"
}
resource "local_file" "ssh_private_key" {
content = tls_private_key.ssh.private_key_pem
filename = "${path.module}/private.pem"
file_permission = "0600"
}
resource "lxd_container" "vpn" { resource "lxd_container" "vpn" {
name = "vpn" name = "vpn"
image = "ubuntu:20.04" image = "ubuntu:20.04"
ephemeral = false ephemeral = false
config = {
"limits.cpu" = 1
"limits.memory" = "256MiB"
"user.user-data" = templatefile(
"${path.module}/cloud-init.yaml.tpl",
{
ssh_public_key = tls_private_key.ssh.public_key_openssh
}
)
}
device {
name = "eth0"
type = "nic"
properties = {
nictype = "macvlan"
parent = "eno1"
}
}
}
resource "null_resource" "ansible" {
triggers = {
ansible_hash = md5(join("", [for f in fileset("${path.module}/ansible/", "**") : file("${path.module}/ansible/${f}")]))
}
provisioner "local-exec" {
command = "ansible-playbook -u ubuntu -i ${lxd_container.vpn.ip_address}, --private-key ${local_file.ssh_private_key.filename} ${path.module}/ansible/main.yml"
environment = {
ANSIBLE_HOST_KEY_CHECKING = "False"
}
}
} }

View File

@ -0,0 +1,8 @@
terraform {
required_providers {
lxd = {
source = "terraform-lxd/lxd"
version = "1.5.0"
}
}
}