codeblocks are very easy to copy and paste and share code. A good cloud engineer needs to support future_reviewers and users of the code.
---
- name: Automate Terraform
hosts: localhost
become: yes
tasks:
- name: Update apt cache (for Ubuntu)
apt:
update_cache: yes
when: ansible_os_family == "Debian"
- name: Install Terraform
command: >
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
&& echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
&& sudo apt-get update && sudo apt-get install terraform
when: ansible_os_family == "Debian"
- name: Install Terraform (for RedHat-based systems)
command: >
sudo yum install -y dnf-plugins-core
&& sudo dnf config-manager --add-repo https://rpm.releases.hashicorp.com/$release/hashicorp.repo
&& sudo dnf -y install terraform
when: ansible_os_family == "RedHat"
- name: Clone Terraform configuration from Git (if needed)
git:
repo: https://github.com/your/repo.git
dest: /path/to/terraform-project
version: main # Replace with the branch or tag you want to use
ignore_errors: yes
when: not ansible_check_mode
- name: Change directory to the Terraform project
ansible.builtin.cd:
path: /path/to/terraform-project
- name: Initialize Terraform
command: terraform init
when: not ansible_check_mode
- name: Apply Terraform changes
command: terraform apply -auto-approve
when: not ansible_check_mode
# Additional tasks like running tests or capturing outputs can be added here
# To destroy resources created by Terraform, use:
# - name: Destroy Terraform resources
# command: terraform destroy -auto-approve
# when: not ansible_check_mode
-[x] visualstudio -[x] online cli
GFM github Flavoured Markdown [2]
name | shortcode | Emoji |
---|---|---|
cloud | ☁️ | 🌩️ |