From a6a1a7266d9bb53a0782e36f508a14551655d436 Mon Sep 17 00:00:00 2001 From: Khue Doan Date: Sun, 2 May 2021 21:58:33 +0700 Subject: [PATCH] Add Ansible provisioner module --- infra/modules/ansible-provisioner/main.tf | 13 ++++++++++ .../modules/ansible-provisioner/variables.tf | 26 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 infra/modules/ansible-provisioner/main.tf create mode 100644 infra/modules/ansible-provisioner/variables.tf diff --git a/infra/modules/ansible-provisioner/main.tf b/infra/modules/ansible-provisioner/main.tf new file mode 100644 index 00000000..622744a2 --- /dev/null +++ b/infra/modules/ansible-provisioner/main.tf @@ -0,0 +1,13 @@ +resource "null_resource" "ansible_provisioner" { + triggers = { + ansible_hash = md5(join("", [for files in fileset("${var.ansible_directory}/", "**") : file("${var.ansible_directory}/${files}")])) + } + + provisioner "local-exec" { + command = "ansible-playbook --user ${var.ansible_user} --inventory ${join(",", var.ansible_inventory)}, --private-key ${var.ansible_private_key} ${path.ansible_directory}/${var.ansible_playbook}" + + environment = { + ANSIBLE_HOST_KEY_CHECKING = "False" + } + } +} diff --git a/infra/modules/ansible-provisioner/variables.tf b/infra/modules/ansible-provisioner/variables.tf new file mode 100644 index 00000000..19d7ef73 --- /dev/null +++ b/infra/modules/ansible-provisioner/variables.tf @@ -0,0 +1,26 @@ +var "ansible_directory" { + description = "Path to Ansible directory" + type = string +} + +var "ansible_playbook" { + description = "Path to Ansible playbook, relative to Ansible directory" + type = string + default = "main.yml" +} + +var "ansible_user" { + description = "User to connect as" + type = string + default = "ubuntu" +} + +var "ansible_inventory" { + description = "List of hosts for Ansible to run against" + type = list(string) +} + +var "ansible_private_key" { + description = "Private key file to authenticate the connection" + type = list(string) +}