Skip to content

Commit 884379d

Browse files
authored
Merge pull request #66 from linux-kdevops/cel/opentofu
Add ability to select opentofu instead of terraform
2 parents 2674eee + 7f46a4a commit 884379d

File tree

10 files changed

+141
-1
lines changed

10 files changed

+141
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
# This will be set from extra_vars.yaml based on Kconfig selection
3+
terraform_binary_path: "/usr/local/bin/terraform"

playbooks/roles/extra_volumes/tasks/providers/aws.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
delegate_to: localhost
1818
run_once: true
1919
cloud.terraform.terraform_output:
20+
binary_path: "{{ terraform_binary_path }}"
2021
format: json
2122
name: "extra_volumes_map"
2223
project_path: "{{ topdir_path }}/terraform/aws"

playbooks/roles/gen_tfvars/tasks/main.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,37 @@
2828
path: "{{ topdir_path }}/{{ kdevops_terraform_tfvars }}"
2929
register: kdevops_tfvars_dest
3030

31+
- name: Find dynamic Kconfig files for {{ kdevops_terraform_provider }}
32+
ansible.builtin.find:
33+
paths: "{{ topdir_path }}/terraform/{{ kdevops_terraform_provider }}/kconfigs"
34+
patterns: "Kconfig*.generated"
35+
register: provider_kconfig_files
36+
when:
37+
- kdevops_enable_terraform
38+
39+
- name: Check if any dynamic Kconfig files are empty
40+
ansible.builtin.fail:
41+
msg: |
42+
ERROR: Dynamic cloud configuration not generated or incomplete.
43+
44+
Found {{ provider_kconfig_files.matched }} dynamic Kconfig file(s) for {{ kdevops_terraform_provider }},
45+
but at least one is empty ({{ item.path }}).
46+
47+
Before running 'make' or 'make bringup', you must generate the
48+
dynamic cloud configuration by running:
49+
50+
make cloud-config
51+
52+
For more information, run 'make help' and see the cloud-config
53+
targets section.
54+
when:
55+
- kdevops_enable_terraform
56+
- provider_kconfig_files.matched > 0
57+
- item.size == 0
58+
loop: "{{ provider_kconfig_files.files }}"
59+
loop_control:
60+
label: "{{ item.path }}"
61+
3162
- name: Ensure proper permission on {{ kdevops_terraform_tfvars }}
3263
become: true
3364
become_flags: "su - -c"

playbooks/roles/install_terraform/defaults/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
# You can override these
55
terraform_version: "1.2.3"
6+
opentofu_version: "1.6.0"
67
force_install_if_present: false
78

89
# Ignores using distro packages and installs from zip file instead

playbooks/roles/install_terraform/tasks/install-deps/debian/main.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
register: terraform_present
55
changed_when: terraform_present.rc == 1
66
failed_when: terraform_present.rc != 0 and terraform_present.rc != 1
7+
when: terraform_use_terraform|bool
8+
tags: ["terraform", "verify"]
9+
10+
- name: Verify OpenTofu installation
11+
ansible.builtin.command: "which tofu"
12+
register: opentofu_present
13+
changed_when: opentofu_present.rc == 1
14+
failed_when: opentofu_present.rc != 0 and opentofu_present.rc != 1
15+
when: terraform_use_opentofu|bool
716
tags: ["terraform", "verify"]
817

918
- name: Install Terraform Dependencies
@@ -23,4 +32,16 @@
2332
dest: /usr/local/bin
2433
remote_src: true
2534
when:
35+
- terraform_use_terraform|bool
2636
- force_install_if_present|bool or terraform_present.rc != 0
37+
38+
- name: Download OpenTofu from the latest release and install locally
39+
become: true
40+
become_method: sudo
41+
ansible.builtin.unarchive:
42+
src: https://github.com/opentofu/opentofu/releases/download/v{{ opentofu_version }}/tofu_{{ opentofu_version }}_linux_amd64.zip
43+
dest: /usr/local/bin
44+
remote_src: true
45+
when:
46+
- terraform_use_opentofu|bool
47+
- force_install_if_present|bool or opentofu_present.rc != 0

playbooks/roles/install_terraform/tasks/install-deps/redhat/main.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
register: terraform_present
55
changed_when: terraform_present.rc == 1
66
failed_when: terraform_present.rc != 0 and terraform_present.rc != 1
7+
when: terraform_use_terraform|bool
8+
tags: ["terraform", "verify"]
9+
10+
- name: Verify OpenTofu installation
11+
ansible.builtin.command: "which tofu"
12+
register: opentofu_present
13+
changed_when: opentofu_present.rc == 1
14+
failed_when: opentofu_present.rc != 0 and opentofu_present.rc != 1
15+
when: terraform_use_opentofu|bool
716
tags: ["terraform", "verify"]
817

918
- name: Download Terraform from the latest release and install locally
@@ -14,4 +23,16 @@
1423
dest: /usr/local/bin
1524
remote_src: true
1625
when:
26+
- terraform_use_terraform|bool
1727
- force_install_if_present|bool or terraform_present.rc != 0
28+
29+
- name: Download OpenTofu from the latest release and install locally
30+
become: true
31+
become_method: sudo
32+
ansible.builtin.unarchive:
33+
src: https://github.com/opentofu/opentofu/releases/download/v{{ opentofu_version }}/tofu_{{ opentofu_version }}_linux_amd64.zip
34+
dest: /usr/local/bin
35+
remote_src: true
36+
when:
37+
- terraform_use_opentofu|bool
38+
- force_install_if_present|bool or opentofu_present.rc != 0

playbooks/roles/install_terraform/tasks/install-deps/suse/main.yml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
register: terraform_present
1818
changed_when: terraform_present.rc == 1
1919
failed_when: terraform_present.rc != 0 and terraform_present.rc != 1
20+
when: terraform_use_terraform|bool
21+
tags: ["terraform", "verify"]
22+
23+
- name: Verify OpenTofu installation
24+
ansible.builtin.command: "which tofu"
25+
register: opentofu_present
26+
changed_when: opentofu_present.rc == 1
27+
failed_when: opentofu_present.rc != 0 and opentofu_present.rc != 1
28+
when: terraform_use_opentofu|bool
2029
tags: ["terraform", "verify"]
2130

2231
- name: Download Terraform from the latest release and install locally
@@ -27,17 +36,31 @@
2736
dest: /usr/local/bin
2837
remote_src: true
2938
when:
39+
- terraform_use_terraform|bool
3040
- force_install_zip|bool
3141
- force_install_if_present|bool or (is_sle or is_leap and terraform_present.rc != 0)
3242

33-
- name: Install vagrant and vagrant-libvirt from your tumbleweed repository
43+
- name: Download OpenTofu from the latest release and install locally
44+
become: true
45+
become_method: sudo
46+
ansible.builtin.unarchive:
47+
src: https://github.com/opentofu/opentofu/releases/download/v{{ opentofu_version }}/tofu_{{ opentofu_version }}_linux_amd64.zip
48+
dest: /usr/local/bin
49+
remote_src: true
50+
when:
51+
- terraform_use_opentofu|bool
52+
- force_install_zip|bool
53+
- force_install_if_present|bool or (is_sle or is_leap and opentofu_present.rc != 0)
54+
55+
- name: Install terraform from your tumbleweed repository
3456
become: true
3557
become_method: sudo
3658
ansible.builtin.package:
3759
name:
3860
- terraform
3961
state: present
4062
when:
63+
- terraform_use_terraform|bool
4164
- not force_install_zip|bool
4265
- not force_install_if_present|bool
4366
- terraform_present.rc != 0
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
---
22
ssh_config_kexalgorithms: ""
3+
# This will be set from extra_vars.yaml based on Kconfig selection
4+
terraform_binary_path: "/usr/local/bin/terraform"

playbooks/roles/terraform/tasks/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686

8787
- name: Bring up terraform resources
8888
cloud.terraform.terraform:
89+
binary_path: "{{ terraform_binary_path }}"
8990
force_init: true
9091
project_path: "{{ topdir_path }}/terraform/{{ kdevops_terraform_provider }}"
9192
state: present
@@ -94,6 +95,7 @@
9495

9596
- name: Retrieve the controller_ip_map from terraform
9697
cloud.terraform.terraform_output:
98+
binary_path: "{{ terraform_binary_path }}"
9799
format: json
98100
name: controller_ip_map
99101
project_path: "{{ topdir_path }}/terraform/{{ kdevops_terraform_provider }}"
@@ -131,6 +133,7 @@
131133
block:
132134
- name: Retrieve the controller_ip_map from terraform
133135
cloud.terraform.terraform_output:
136+
binary_path: "{{ terraform_binary_path }}"
134137
format: json
135138
name: controller_ip_map
136139
project_path: "{{ topdir_path }}/terraform/{{ kdevops_terraform_provider }}"
@@ -165,6 +168,7 @@
165168

166169
- name: Destroy terraform resources
167170
cloud.terraform.terraform:
171+
binary_path: "{{ terraform_binary_path }}"
168172
force_init: true
169173
project_path: "{{ topdir_path }}/terraform/{{ kdevops_terraform_provider }}"
170174
state: absent

terraform/Kconfig

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,37 @@ config TERRAFORM_PRIVATE_NET_MASK
3434
depends on TERRAFORM_PRIVATE_NET
3535
help
3636
Length of the network mask to use for the private network.
37+
38+
choice
39+
prompt "Infrastructure as Code tool"
40+
default TERRAFORM_USE_TERRAFORM
41+
help
42+
Choose whether to use Terraform or OpenTofu for infrastructure
43+
provisioning. OpenTofu is an open-source fork of Terraform that
44+
maintains compatibility with Terraform configurations.
45+
46+
config TERRAFORM_USE_TERRAFORM
47+
bool "Use Terraform"
48+
output yaml
49+
help
50+
Use HashiCorp Terraform for infrastructure provisioning.
51+
Terraform is the original infrastructure as code tool.
52+
53+
config TERRAFORM_USE_OPENTOFU
54+
bool "Use OpenTofu"
55+
output yaml
56+
help
57+
Use OpenTofu for infrastructure provisioning. OpenTofu is an
58+
open-source fork of Terraform maintained by the Linux Foundation
59+
that provides a community-driven alternative while maintaining
60+
compatibility with Terraform configurations.
61+
62+
endchoice
63+
64+
config TERRAFORM_BINARY_PATH
65+
string
66+
default "/usr/local/bin/terraform" if TERRAFORM_USE_TERRAFORM
67+
default "/usr/local/bin/tofu" if TERRAFORM_USE_OPENTOFU
68+
output yaml
69+
3770
endif # TERRAFORM

0 commit comments

Comments
 (0)