Migrate from cloud.terraform to hashicorp.terraform
If you are using the existing cloud.terraform (CLI-based) collection, you can migrate your existing playbooks to the hashicorp.terraform (API-based) collection. The main modules for hashicorp.terraform that you must configure are hashicorp.terraform.configuration_version and hashicorp.terraform.run.
Configure the hashicorp.terraform.configuration_version module Copy linkLink copied!
To migrate to the hashicorp.terraform collection, you must configure the hashicorp.terraform.configuration_version module. This module manages configuration versions in Terraform Enterprise or HCP Terraform.
Before you begin Copy linkLink copied!
- Install the Ansible Automation Platform certified
hashicorp.terraformcollection. - Verify that a valid organization and workspace are correctly set up in Terraform Enterprise or HCP Terraform.
Procedure Copy linkLink copied!
Configure the hashicorp.terraform.run module Copy linkLink copied!
The hashicorp.terraform.run module lets you manage Terraform Enterprise or HCP Terraform runs using create, apply, cancel, and discard operations. You can trigger plans or apply operations on specified workspaces with customizable settings.
Before you begin Copy linkLink copied!
- Ensure that a valid Terraform API token is properly configured to authenticate with your Terraform Enterprise or HCP Terraform environment.
- Verify that a valid organization and workspace are correctly set up in Terraform Enterprise or HCP Terraform.
Procedure Copy linkLink copied!
Examples for hashicorp.terraform modules Copy linkLink copied!
These before and after examples help users understand how the modules can be configured in a real world environment.
Plan Only Copy linkLink copied!
- Before (
cloud.terraform.terraform):
- name: Create a plan file using check mode
cloud.terraform.terraform:
force_init: true
project_path: "/usr/home/tf"
plan_file: "/usr/home/tf/terraform.tfplan"
state: present
check_mode: true
check_destroy: true
variables:
environment: prod
- After (
hashicorp.terraform.*):- The
configuration_versionmodule:- name: Create configuration version with auto_queue_runs to false hashicorp.terraform.configuration_version: workspace_id: ws-1234 configuration_files_path: "usr/home/tf_files" auto_queue_runs: false tf_validate_certs: true poll_interval: 5 poll_timeout: 10 state: present - The
plan_onlyrun with the run module:- name: Create a plan only run with variables hashicorp.terraform.run: workspace_id: ws-1234 run_message: "plan-only vpc creation" poll: false state: "present" tf_token: "{{ tfc_token }}" plan_only: true variables: - key: "env" value: "production"
- The
Plan and apply Copy linkLink copied!
- Before (
cloud.terraform.terraform):- Generate the plan:
- name: Plan and Apply Workflow - Step 1 - Generate Plan cloud.terraform.terraform: force_init: true project_path: "/usr/home/tf" plan_file: "/usr/home/tf/workflow.tfplan" state: present check_mode: true variables: environment: prod - Apply the plan:
- name: Plan and Apply Workflow - Step 2 - Apply Plan cloud.terraform.terraform: project_path: "/usr/home/tf" plan_file: "/usr/home/tf/workflow.tfplan" state: present
- Generate the plan:
- After (
hashicorp.terraform.run):- The
configuration_versionmodule:- name: Create configuration version with auto_queue_runs to false hashicorp.terraform.configuration_version: workspace_id: ws-1234 configuration_files_path: "usr/home/tf_files" auto_queue_runs: false tf_validate_certs: true poll_interval: 5 poll_timeout: 10 state: present - The run module with two options for plan and apply workflow:
- The
- Option 1: Uses the
auto_applyparameter to handle both the plan and apply workflows:- name: Create a run with auto_apply hashicorp.terraform.run: workspace_id: ws-1234 run_message: "destroy vpc" state: "present" tf_token: "{{ tfc_token }}" auto_apply: true poll: true poll_interval: 10 poll_timeout: 30 - Option 2: Uses two sub-steps to create a
save_planrun and then apply it:- Create the plan:
- name: Create a save plan run hashicorp.terraform.run: workspace_id: ws-1234 run_message: "save plan vpc creation" state: "present" tf_token: "{{ tfc_token }}" poll: true poll_interval: 10 poll_timeout: 30 save_plan: true - Apply the plan. You get the
run_idfrom the output of the run module task:- name: Apply the save plan run hashicorp.terraform.run: run_id: run-1234 state: "applied" tf_token: "{{ tfc_token }}" poll: true poll_interval: 10 poll_timeout: 30
- Create the plan: