From 10074eddbe094a830fa3c678b39cca700ff5e3f2 Mon Sep 17 00:00:00 2001 From: mikeeq Date: Tue, 16 Jan 2024 21:35:20 +0100 Subject: [PATCH] Add terraform-docs --- playbooks/generic.yml | 1 + .../tasks/get_latest_version.yml | 13 ++++++++++ roles/apps/terraform-docs/tasks/install.yml | 25 +++++++++++++++++++ roles/apps/terraform-docs/tasks/main.yml | 18 +++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 roles/apps/terraform-docs/tasks/get_latest_version.yml create mode 100644 roles/apps/terraform-docs/tasks/install.yml create mode 100644 roles/apps/terraform-docs/tasks/main.yml diff --git a/playbooks/generic.yml b/playbooks/generic.yml index baf904ad..ed96d3bf 100644 --- a/playbooks/generic.yml +++ b/playbooks/generic.yml @@ -17,3 +17,4 @@ - apps/aws-cli - apps/terraform - apps/terragrunt + - apps/terraform-docs diff --git a/roles/apps/terraform-docs/tasks/get_latest_version.yml b/roles/apps/terraform-docs/tasks/get_latest_version.yml new file mode 100644 index 00000000..a6be6339 --- /dev/null +++ b/roles/apps/terraform-docs/tasks/get_latest_version.yml @@ -0,0 +1,13 @@ +--- +- name: Get latest version of terraform_docs + shell: curl -Is https://github.com/terraform-docs/terraform-docs/releases/latest | grep -i 'location:' | cut -f2 -d'v' | cut -f1 -d'"' + register: version + changed_when: false + +- name: "Latest version of terraform_docs:" + debug: + var: version.stdout + +- name: "Set fact with latest version of terraform_docs" + set_fact: + terraform_docs_version: "{{ version.stdout }}" diff --git a/roles/apps/terraform-docs/tasks/install.yml b/roles/apps/terraform-docs/tasks/install.yml new file mode 100644 index 00000000..1f0504b6 --- /dev/null +++ b/roles/apps/terraform-docs/tasks/install.yml @@ -0,0 +1,25 @@ +--- +- name: Directory for terraform_docs + file: + path: /tmp/terraform_docs + state: directory + +- name: Download terraform_docs binary + get_url: + url: "https://github.com/terraform-docs/terraform-docs/releases/download/v{{ terraform_docs_version }}/terraform-docs-v{{ terraform_docs_version }}-linux-amd64.tar.gz" + dest: /tmp/terraform_docs/terraform_docs.tar.gz + +- name: Unarchive a file that is already on the remote machine + unarchive: + src: /tmp/terraform_docs/terraform_docs.tar.gz + dest: /tmp/terraform_docs + remote_src: yes + +- name: Copy terraform_docs + copy: + src: "/tmp/terraform_docs/terraform-docs" + dest: "/usr/local/bin/terraform-docs" + remote_src: true + mode: 0755 + owner: root + group: root diff --git a/roles/apps/terraform-docs/tasks/main.yml b/roles/apps/terraform-docs/tasks/main.yml new file mode 100644 index 00000000..6ed22760 --- /dev/null +++ b/roles/apps/terraform-docs/tasks/main.yml @@ -0,0 +1,18 @@ +--- +- name: Get installed version + become: true + become_user: "{{ user_name }}" + shell: terraform-docs version | awk '{print $3}' | cut -f2 -d'v' + register: installed_terraform_docs_version + changed_when: false + ignore_errors: true + +- name: "Installed version of terraform_docs:" + debug: + var: installed_terraform_docs_version.stdout + +- include_tasks: get_latest_version.yml + when: terraform_docs_version is not defined + +- include_tasks: install.yml + when: installed_terraform_docs_version.stdout != terraform_docs_version