This playbook installs Terraform from official HashiCorp repositories on Debian/Ubuntu and RHEL-compatible systems.
become privileges on target hosts- name: Install Terraform on Debian and Ubuntu
hosts: terraform_debian
become: true
tasks:
- name: Install repository prerequisites
ansible.builtin.apt:
name:
- gpg
- wget
state: present
update_cache: true
- name: Download HashiCorp apt GPG key
ansible.builtin.get_url:
url: https://apt.releases.hashicorp.com/gpg
dest: /tmp/hashicorp-archive-keyring.asc
mode: "0644"
- name: Dearmor HashiCorp GPG key
ansible.builtin.command:
cmd: gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg /tmp/hashicorp-archive-keyring.asc
args:
creates: /usr/share/keyrings/hashicorp-archive-keyring.gpg
- name: Add HashiCorp apt repository
ansible.builtin.apt_repository:
repo: "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com {{ ansible_distribution_release }} main"
filename: hashicorp
state: present
- name: Install Terraform
ansible.builtin.apt:
name: terraform
state: present
update_cache: true
- name: Verify Terraform installation
ansible.builtin.command: terraform -version
register: terraform_version
changed_when: false
- name: Show Terraform version
ansible.builtin.debug:
var: terraform_version.stdout_lines
- name: Install Terraform on RHEL-compatible systems
hosts: terraform_rhel
become: true
tasks:
- name: Configure HashiCorp yum repository
ansible.builtin.yum_repository:
name: hashicorp
description: HashiCorp Stable
baseurl: https://rpm.releases.hashicorp.com/RHEL/$releasever/$basearch/stable
enabled: true
gpgcheck: true
gpgkey: https://rpm.releases.hashicorp.com/gpg
- name: Install Terraform
ansible.builtin.dnf:
name: terraform
state: present
- name: Verify Terraform installation
ansible.builtin.command: terraform -version
register: terraform_version
changed_when: false
- name: Show Terraform version
ansible.builtin.debug:
var: terraform_version.stdout_lines
ansible-playbook -i inventory.ini terraform-install.yml