Skip to content

Commit

Permalink
Enhance HCL file handling by refining exclusion patterns and ensuring…
Browse files Browse the repository at this point in the history
… proper permissions for terragrunt files
  • Loading branch information
Your Name committed Jun 26, 2025
1 parent 5d45532 commit 2f3837a
Showing 1 changed file with 47 additions and 11 deletions.
58 changes: 47 additions & 11 deletions ansible/generate_hcl_files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 2f3837a

Please sign in to comment.