From 2f3837a5cb89707d25e7ac9e6939b3d06448118f Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 26 Jun 2025 16:51:07 -0400 Subject: [PATCH] Enhance HCL file handling by refining exclusion patterns and ensuring proper permissions for terragrunt files --- ansible/generate_hcl_files.yml | 58 +++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/ansible/generate_hcl_files.yml b/ansible/generate_hcl_files.yml index 3fed9a5..51f03a2 100644 --- a/ansible/generate_hcl_files.yml +++ b/ansible/generate_hcl_files.yml @@ -98,37 +98,73 @@ paths: "{{ base_dir }}" patterns: "*.hcl" recurse: yes - excludes: "*terragrunt.hcl*" + excludes: + - "**/terragrunt.hcl" + - "**/terragrunt.hcl.off" + file_type: file register: all_hcl_files - name: Analyze HCL file structure and content ansible.builtin.debug: msg: "Analyzing {{ item.path }}" + verbosity: 1 loop: "{{ all_hcl_files.files }}" - - name: Create target directory structure + - name: Create target directory structure with proper permissions ansible.builtin.file: path: "{{ output_dir }}/{{ env_name }}/{{ region_name }}/vpc/{{ cluster_dir }}" state: directory mode: '0755' - - # Copy any additional files in module directories (like README.md) - - name: Find all additional files in module directories + recurse: yes + + # Separate find for terragrunt files to handle them specially + - name: Find all terragrunt.hcl files in current cluster structure ansible.builtin.find: paths: "{{ base_dir }}/{{ current.environment_dir }}/{{ current.region_dir }}/vpc/{{ current.cluster_dir }}" - excludes: - - "*/terragrunt.hcl" - - "*/terragrunt.hcl.off" + patterns: "terragrunt.hcl" + recurse: yes + file_type: file + register: terragrunt_files + + - name: Debug terragrunt files + ansible.builtin.debug: + var: terragrunt_files + verbosity: 2 + + - name: Ensure module directories exist with proper permissions + ansible.builtin.file: + path: "{{ output_dir }}/{{ env_name }}/{{ region_name }}/vpc/{{ cluster_dir }}/{{ (item.path | regex_replace('.*' + current.cluster_dir + '/(.*)/terragrunt.hcl', '\\1')) }}" + state: directory + mode: '0755' + loop: "{{ terragrunt_files.files }}" + + - name: Copy terragrunt files with preserved permissions + ansible.builtin.copy: + src: "{{ item.path }}" + dest: "{{ output_dir }}/{{ env_name }}/{{ region_name }}/vpc/{{ cluster_dir }}/{{ item.path | regex_replace('.*' + current.cluster_dir + '/(.*)', '\\1') }}" + mode: preserve + loop: "{{ terragrunt_files.files }}" + + # Handle non-terragrunt files + - name: Find all additional files (excluding terragrunt.hcl) + ansible.builtin.find: + paths: "{{ base_dir }}/{{ current.environment_dir }}/{{ current.region_dir }}/vpc/{{ current.cluster_dir }}" + excludes: + - "**/terragrunt.hcl" + - "**/.terraform" + - "**/.terraform.lock.hcl" recurse: yes + file_type: file register: additional_files - - name: Copy additional files to target structure + - name: Copy additional files with preserved permissions ansible.builtin.copy: src: "{{ item.path }}" dest: "{{ output_dir }}/{{ env_name }}/{{ region_name }}/vpc/{{ cluster_dir }}/{{ item.path | regex_replace('.*' + current.cluster_dir + '/(.*)', '\\1') }}" - mode: '0644' + mode: preserve + force: no # Don't overwrite existing files loop: "{{ additional_files.files }}" - + - name: Generate root.hcl from template ansible.builtin.template: src: "{{ template_dir }}/root.hcl.j2"