refactor(k3s): replace code with what the provisioner role now offers and store opentofu data accordingly

This commit is contained in:
NaeiKinDus 2024-07-10 00:00:00 +00:00
parent 1b3e0c401c
commit 95bb861049
Signed by: WoodSmellParticle
GPG key ID: 8E52ADFF7CA8AE56
4 changed files with 22 additions and 83 deletions

View file

@ -1,75 +1,56 @@
- name: deploy OpenTofu resource
connection: local
block:
- name: find terraform binary
ansible.builtin.command:
cmd: bash -c 'command -v terraform || command -v tofu || echo /usr/local/bin/terraform'
register: located_tf_binary
when: terraform_binary_path is undefined
changed_when: false
- name: set terraform binary
- name: set TF resource facts
ansible.builtin.set_fact:
terraform_binary_path: '{{ terraform_binary_path | default(located_tf_binary.stdout, true) }}'
when: terraform_binary_path is undefined
k3s_tf_safe_item_name: "{{ item.name | regex_replace('[^\\w]', '') }}"
k3s_tf_project_git_path: "{{ provisioner_facts.artifacts_dir }}/{{ item.name | regex_replace('[^\\w]', '') }}.git"
- name: create temp directory
ansible.builtin.tempfile:
state: directory
register: tmp_file
- name: check pre-existing TF state file
ansible.builtin.file:
path: "{{ provisioner_facts.k8s_states_dir }}/{{ k3s_tf_safe_item_name }}.tfstate"
register: tfstate_file_info
changed_when: false
when: item.storage_dir is not defined
- name: set target directory for git repository
ansible.builtin.set_fact:
git_clone_dir: '{{ item.storage_dir | default(tmp_file.path, true) }}'
failed_when: false
- name: fetch git repository
ansible.builtin.git:
repo: '{{ item.git_repository }}'
dest: '{{ git_clone_dir }}'
dest: '{{ k3s_tf_project_git_path }}'
version: '{{ item.git_revision }}'
force: true
- name: prepare variables file
ansible.builtin.copy:
content: '{{ item.tfvars_content }}'
dest: '{{ git_clone_dir }}/{{ item.terraform_dir }}/terraform.tfvars'
mode: '0500'
dest: '{{ k3s_tf_project_git_path }}/{{ item.terraform_dir }}/terraform.tfvars'
mode: '0600'
force: true
- name: prepare tfstate file
ansible.builtin.copy:
src: '{{ item.tfstate_path }}'
dest: '{{ git_clone_dir }}/{{ item.terraform_dir }}/terraform.tfstate'
src: '{{ provisioner_facts.k8s_states_dir }}/{{ k3s_tf_safe_item_name }}.tfstate'
dest: '{{ k3s_tf_project_git_path }}/{{ item.terraform_dir }}/terraform.tfstate'
force: true
mode: '0600'
when: git_clone_dir not in item.tfstate_path
- name: deploy k8s resources
community.general.terraform:
binary_path: "{{ terraform_binary_path }}"
project_path: '{{ git_clone_dir }}/{{ item.terraform_dir}}'
binary_path: "{{ provisioner_facts.tofu_binary_path }}"
project_path: '{{ k3s_tf_project_git_path }}/{{ item.terraform_dir }}'
provider_upgrade: true
force_init: true
- name: backup source stored state
- name: backup source state file
ansible.builtin.copy:
src: '{{ item.tfstate_path }}'
dest: '{{ item.tfstate_path }}.ansible.backup'
src: '{{ provisioner_facts.k8s_states_dir }}/{{ k3s_tf_safe_item_name }}.tfstate'
dest: '{{ provisioner_facts.k8s_states_dir }}/{{ k3s_tf_safe_item_name }}.tfstate.previous'
force: true
mode: '0600'
- name: update source tfstate file
ansible.builtin.copy:
src: '{{ git_clone_dir }}/{{ item.terraform_dir}}/terraform.tfstate'
dest: '{{ item.tfstate_path }}'
src: '{{ k3s_tf_project_git_path }}/{{ item.terraform_dir }}/terraform.tfstate'
dest: '{{ provisioner_facts.k8s_states_dir }}/{{ k3s_tf_safe_item_name }}.tfstate'
force: true
mode: '0600'
when: git_clone_dir not in item.tfstate_path
- name: cleanup
ansible.builtin.file:
path: '{{ tmp_file.path }}'
state: absent
when: item.storage_dir is not defined