diff mbox series

[v3,1/6] terraform: Replace scripts/*_terraform.sh with an Ansible playbook

Message ID 20250224191215.637818-2-cel@kernel.org (mailing list archive)
State New
Headers show
Series Replace terraform update_ssh_config module | expand

Commit Message

Chuck Lever Feb. 24, 2025, 7:12 p.m. UTC
From: Chuck Lever <chuck.lever@oracle.com>

Refactor: Combine separate terraform scripts into one playbook,
adopting declarative infrastructure-as-code mechanisms rather than
shell scripts. This also makes it simple to iterate over all defined
target nodes -- that's Ansible's bread and butter.

Terraform-specific Ansible-based ssh configuration can now easily
be introduced.

Note that the terraform actions are now silent unless an error
occurs.

Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
---
 playbooks/roles/terraform/tasks/main.yml | 18 ++++++++++++++++++
 playbooks/terraform.yml                  |  5 +++++
 scripts/bringup_terraform.sh             | 12 ------------
 scripts/destroy_terraform.sh             | 10 ----------
 scripts/terraform.Makefile               | 12 ++++++++++--
 5 files changed, 33 insertions(+), 24 deletions(-)
 create mode 100644 playbooks/roles/terraform/tasks/main.yml
 create mode 100644 playbooks/terraform.yml
 delete mode 100755 scripts/bringup_terraform.sh
 delete mode 100755 scripts/destroy_terraform.sh
diff mbox series

Patch

diff --git a/playbooks/roles/terraform/tasks/main.yml b/playbooks/roles/terraform/tasks/main.yml
new file mode 100644
index 000000000000..e328ea7bc0b2
--- /dev/null
+++ b/playbooks/roles/terraform/tasks/main.yml
@@ -0,0 +1,18 @@ 
+---
+- name: Bring up terraform resources
+  community.general.terraform:
+    project_path: "{{ topdir_path }}/terraform/{{ kdevops_terraform_provider }}"
+    state: present
+    force_init: true
+  tags:
+    - bringup
+
+- name: Destroy terraform resources
+  delegate_to: localhost
+  run_once: true
+  community.general.terraform:
+    project_path: "{{ topdir_path }}/terraform/{{ kdevops_terraform_provider }}"
+    state: absent
+    force_init: true
+  tags:
+    - destroy
diff --git a/playbooks/terraform.yml b/playbooks/terraform.yml
new file mode 100644
index 000000000000..374a76fb0ae7
--- /dev/null
+++ b/playbooks/terraform.yml
@@ -0,0 +1,5 @@ 
+---
+- hosts: all
+  gather_facts: false
+  roles:
+    - role: terraform
diff --git a/scripts/bringup_terraform.sh b/scripts/bringup_terraform.sh
deleted file mode 100755
index 427962ac6eb9..000000000000
--- a/scripts/bringup_terraform.sh
+++ /dev/null
@@ -1,12 +0,0 @@ 
-#!/bin/bash
-# SPDX-License-Identifier: copyleft-next-0.3.1
-
-set -e
-
-source ${TOPDIR}/.config
-source ${TOPDIR}/scripts/lib.sh
-
-cd terraform/${KDEVOPS_CLOUD_PROVIDER}
-terraform init
-terraform plan
-terraform apply -auto-approve
diff --git a/scripts/destroy_terraform.sh b/scripts/destroy_terraform.sh
deleted file mode 100755
index 58d467a40c07..000000000000
--- a/scripts/destroy_terraform.sh
+++ /dev/null
@@ -1,10 +0,0 @@ 
-#!/bin/bash
-# SPDX-License-Identifier: copyleft-next-0.3.1
-
-set -e
-
-source ${TOPDIR}/.config
-source ${TOPDIR}/scripts/lib.sh
-
-cd terraform/${KDEVOPS_CLOUD_PROVIDER}
-terraform destroy -auto-approve
diff --git a/scripts/terraform.Makefile b/scripts/terraform.Makefile
index 888d3af88e3e..2436571a4aac 100644
--- a/scripts/terraform.Makefile
+++ b/scripts/terraform.Makefile
@@ -164,10 +164,18 @@  endif # CONFIG_TERRAFORM_SSH_CONFIG_GENKEY
 ANSIBLE_EXTRA_ARGS += $(TERRAFORM_EXTRA_VARS)
 
 bringup_terraform:
-	$(Q)$(TOPDIR)/scripts/bringup_terraform.sh
+	$(Q)ansible-playbook $(ANSIBLE_VERBOSE) \
+		--connection=local --inventory localhost, \
+		playbooks/terraform.yml --tags bringup \
+		--extra-vars=@./extra_vars.yaml \
+		-e 'ansible_python_interpreter=/usr/bin/python3'
 
 destroy_terraform:
-	$(Q)$(TOPDIR)/scripts/destroy_terraform.sh
+	$(Q)ansible-playbook $(ANSIBLE_VERBOSE) \
+		--connection=local -i $(KDEVOPS_HOSTFILE) \
+		playbooks/terraform.yml --tags destroy \
+		--extra-vars=@./extra_vars.yaml \
+		-e 'ansible_python_interpreter=/usr/bin/python3'
 	$(Q)rm -f $(KDEVOPS_PROVISIONED_DEVCONFIG)
 
 $(KDEVOPS_TFVARS): $(KDEVOPS_TFVARS_TEMPLATE) .config