Skip to content

use env_name instead of environment, cluster_tags instead of tags #5

Merged
merged 2 commits into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 57 additions & 48 deletions ansible/generate_hcl_files.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
# generate_hcl_files.yml - Ansible playbook to analyze and generate HCL files from templates
# Usage: ansible-playbook generate_hcl_files.yml -e "config_file=/path/to/config.json"
#
#
# Override options:
# - env_override: Override the environment name from the config file
# - region_override: Override the region name from the config file
Expand All @@ -11,76 +11,78 @@
hosts: localhost
connection: local
gather_facts: yes

vars:
# Default to config.json in repository root
config_file: "{{ config_file | default(base_dir + '/config.json') }}"
base_dir: "{{ playbook_dir }}/.."
template_dir: "{{ playbook_dir }}/templates"
output_dir: "{{ base_dir }}"

commit_id: "{{ lookup('git', 'rev-parse HEAD') }}"

# Extract current structure
current:
environment_dir: "environment"
region_dir: "region"
cluster_dir: "cluster"

tasks:
- name: Check if config file exists
ansible.builtin.stat:
path: "{{ config_file }}"
register: config_stat

- name: Fail if config file doesn't exist
ansible.builtin.fail:
msg: "Config file {{ config_file }} does not exist. Please create it or specify a different file with -e 'config_file=/path/to/file.json'"
when: not config_stat.stat.exists

- name: Load configuration from JSON file
ansible.builtin.set_fact:
config: "{{ lookup('file', config_file) | from_json }}"

- name: debug config
ansible.builtin.debug:
var: config

- name: Set directory names from configuration with overrides
ansible.builtin.set_fact:
env_name: "{{ config.environment }}"
region_name: "{{ config.region }}"
cluster_dir: "{{ config.cluster_dir }}"
cluster_name: "{{ config.cluster.cluster_name }}"

# Consolidate all template variables in one place
- name: Set all template variables
ansible.builtin.set_fact:
# Root template variables
config_values: "{{ config }}"

# Account template variables
account_name: "{{ config.account.account_name | default('') }}"
aws_account_id: "{{ config.account.aws_account_id | default('') }}"
aws_profile: "{{ config.account.aws_profile | default('') }}"
environment: "{{ config.environment | default('development') }}"
environment_abbr: "{{ config.account.environment_abbr | default('') }}"

# Region template variables
aws_region: "{{ region_name }}"

# VPC template variables
vpc_name: "{{ config.vpc.vpc_name | default('') }}"
vpc_domain_name: "{{ config.vpc.vpc_domain_name | default('') }}"

# Cluster template variables
cluster_name: "{{ config.cluster.cluster_name | default('') }}"
cluster_mailing_list: "{{ config.cluster.cluster_mailing_list | default('') }}"
cluster_name: "{{ config.cluster.cluster_name | default('default-cluster-name') }}"
cluster_mailing_list: "{{ config.cluster.cluster_mailing_list | default('default-cluster-email') }}"
eks_instance_disk_size: "{{ config.cluster.eks_instance_disk_size | default(0) }}"
eks_ng_desired_size: "{{ config.cluster.eks_ng_desired_size | default(0) }}"
eks_ng_max_size: "{{ config.cluster.eks_ng_max_size | default(0) }}"
eks_ng_min_size: "{{ config.cluster.eks_ng_min_size | default(0) }}"
organization: "{{ config.cluster.organization | default('') }}"
finops_project_name: "{{ config.cluster.finops_project_name | default('') }}"
finops_project_number: "{{ config.cluster.finops_project_number | default('') }}"
finops_project_role: "{{ config.cluster.finops_project_role | default('') }}"
tags: "{{ config.cluster.tags | default({}) }}"
organization: "{{ config.cluster.organization | default('default-none') }}"
finops_project_name: "{{ config.cluster.finops_project_name | default('none') }}"
finops_project_number: "{{ config.cluster.finops_project_number | default('none') }}"
finops_project_role: "{{ config.cluster.finops_project_role | default('none') }}"
cluster_tags: "{{ config.cluster.tags | default({}) }}"
module_enablement_overrides: "{{ config.cluster.module_enablement_overrides | default({}) }}"
# README template variables
generated_date: "{{ ansible_date_time.iso8601 }}"
Expand All @@ -91,32 +93,32 @@
Using the following directory structure:
Environment directory: {{ env_name }}
Region directory: {{ region_name }}
Cluster directory: {{ cluster_dir }}
Cluster directory: {{ cluster_name }}
- name: Find all non-terragrunt HCL files in the template
ansible.builtin.find:
paths: "{{ base_dir }}"
patterns: "*.hcl"
recurse: yes
excludes:
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 with proper permissions
ansible.builtin.file:
path: "{{ output_dir }}/{{ env_name }}/{{ region_name }}/vpc/{{ cluster_dir }}"
path: "{{ output_dir }}/{{ env_name }}/{{ region_name }}/vpc/{{ cluster_name }}"
state: directory
mode: '0755'
recurse: yes

# Separate find for terragrunt files to handle them specially
- name: Find all terragrunt.hcl files in current cluster structure
ansible.builtin.find:
Expand All @@ -125,26 +127,26 @@
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')) }}"
path: "{{ output_dir }}/{{ env_name }}/{{ region_name }}/vpc/{{ cluster_name }}/{{ (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') }}"
dest: "{{ output_dir }}/{{ env_name }}/{{ region_name }}/vpc/{{ cluster_name }}/{{ 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:
Expand All @@ -156,11 +158,11 @@
recurse: yes
file_type: file
register: additional_files

- 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') }}"
dest: "{{ output_dir }}/{{ env_name }}/{{ region_name }}/vpc/{{ cluster_name }}/{{ item.path | regex_replace('.*' + current.cluster_dir + '/(.*)', '\\1') }}"
mode: preserve
force: no # Don't overwrite existing files
loop: "{{ additional_files.files }}"
Expand All @@ -170,71 +172,78 @@
src: "{{ template_dir }}/root.hcl.j2"
dest: "{{ output_dir }}/root.hcl"
mode: '0644'

- name: Generate account.hcl from template
ansible.builtin.template:
src: "{{ template_dir }}/account.hcl.j2"
dest: "{{ output_dir }}/{{ env_name }}/account.hcl"
mode: '0644'

- name: Generate region.hcl from template
ansible.builtin.template:
src: "{{ template_dir }}/region.hcl.j2"
dest: "{{ output_dir }}/{{ env_name }}/{{ region_name }}/region.hcl"
mode: '0644'

- name: Generate vpc.hcl from template
ansible.builtin.template:
src: "{{ template_dir }}/vpc.hcl.j2"
dest: "{{ output_dir }}/{{ env_name }}/{{ region_name }}/vpc/vpc.hcl"
mode: '0644'

- name: Generate cluster.hcl from template
ansible.builtin.template:
src: "{{ template_dir }}/cluster.hcl.j2"
dest: "{{ output_dir }}/{{ env_name }}/{{ region_name }}/vpc/{{ cluster_dir }}/cluster.hcl"
dest: "{{ output_dir }}/{{ env_name }}/{{ region_name }}/vpc/{{ cluster_name }}/cluster.hcl"
mode: '0644'

# Extract info from current structure
- name: Find all modules in current cluster directory
ansible.builtin.find:
paths: "{{ base_dir }}/{{ current.environment_dir }}/{{ current.region_dir }}/vpc/{{ current.cluster_dir }}"
file_type: directory
register: cluster_modules

- name: Create directories for each module in the target structure
ansible.builtin.file:
path: "{{ output_dir }}/{{ env_name }}/{{ region_name }}/vpc/{{ cluster_dir }}/{{ item.path | basename }}"
path: "{{ output_dir }}/{{ env_name }}/{{ region_name }}/vpc/{{ cluster_name }}/{{ item.path | basename }}"
state: directory
mode: '0755'
loop: "{{ cluster_modules.files }}"


- name: Generate README.md with documentation
ansible.builtin.template:
src: "{{ template_dir }}/README.md.j2"
dest: "{{ output_dir }}/README.md"
mode: '0644'

- name: Summary of generated files
ansible.builtin.debug:
msg: |
Successfully generated HCL files for:
Environment: {{ env_name }}
Region: {{ region_name }}
Cluster: {{ cluster_dir }}
Cluster: {{ cluster_name }}
Generated Files:
- {{ output_dir }}/root.hcl
- {{ output_dir }}/{{ env_name }}/account.hcl
- {{ output_dir }}/{{ env_name }}/{{ region_name }}/region.hcl
- {{ output_dir }}/{{ env_name }}/{{ region_name }}/vpc/vpc.hcl
- {{ output_dir }}/{{ env_name }}/{{ region_name }}/vpc/{{ cluster_dir }}/cluster.hcl
Module files are preserved in: {{ output_dir }}/{{ env_name }}/{{ region_name }}/vpc/{{ cluster_dir }}/
- {{ output_dir }}/{{ env_name }}/{{ region_name }}/vpc/{{ cluster_name }}/cluster.hcl
Module files are preserved in: {{ output_dir }}/{{ env_name }}/{{ region_name }}/vpc/{{ cluster_name }}/
- name: cleanup pip files
ignore_errors: yes
ansible.builtin.file:
path: "{{ item }}"
state: absent
loop:
- \~
- ../~

- name: cleanup template files
ignore_errors: yes
ansible.builtin.file:
path: "{{ item }}"
state: absent
loop:
- ../environment
19 changes: 8 additions & 11 deletions ansible/templates/README.md.j2
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
# EKS Cluster Configuration - {{ environment | capitalize }}
# {{ cluster_name | capitalize }} - {{ env_name }} - EKS Cluster Configuration

This EKS cluster configuration was generated on {{ generated_date }} using the template-eks-cluster Ansible automation.
This EKS cluster configuration was generated on {{ generated_date }} using the template-eks-cluster platform automation at {{ commit_id }}.

## Environment Details

- **Environment**: {{ environment }}
- **Environment**: {{ env_name }}
- **Region**: {{ aws_region }}
- **Cluster Name**: {{ cluster_name }}

## Directory Structur`
## Directory Structure
## Getting Started

To apply this configuration:

1. Change to the directory of the module you want to deploy:
```
cd {{ environment }}/{{ aws_region }}/vpc/{{ cluster_name }}/eks
cd {{ env_name }}/{{ aws_region }}/vpc/{{ cluster_name }}
```

2. Initialize and apply the Terragrunt configuration:
```
terragrunt init
terragrunt plan
terragrunt apply
terragrunt run-all plan
terragrunt run-all apply
```

3. Deploy additional modules as needed:
Expand All @@ -38,7 +37,5 @@ To apply this configuration:
To customize this configuration further, modify the HCL files in the directory structure or regenerate the configuration using the Ansible playbook with a new configuration file:

```
ansible-playbook ansible/generate_hcl_files.yml -e "config_file=your-custom-config.json"
ansible-playbook ansible/generate_hcl_files.yml -e "config_file=../config.json"
```

Each module can be deployed independently using Terragrunt.
4 changes: 2 additions & 2 deletions ansible/templates/account.hcl.j2
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# {{ environment }}/environment.hcl
# {{ env_name }}/environment.hcl

# Set account-wide variables. These are automatically pulled in to configure the remote state bucket in the root
# terragrunt.hcl configuration.
locals {
account_name = "{{ account_name }}"
aws_account_id = "{{ aws_account_id }}"
aws_profile = format("%v-%v", local.aws_account_id, replace(local.account_name, "-ew", "-gov"))
environment = "{{ environment }}"
environment = "{{ env_name }}"
environment_abbr = "{{ environment_abbr }}"
}
22 changes: 11 additions & 11 deletions ansible/templates/cluster.hcl.j2
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
locals {
# Cluster specific configuration
cluster_name = {{ cluster_name }}
cluster_mailing_list = {{ cluster_mailing_list }}
eks_instance_disk_size = {{ eks_instance_disk_size }}
eks_ng_desired_size = {{ eks_ng_desired_size }}
eks_ng_max_size = {{ eks_ng_max_size }}
eks_ng_min_size = {{ eks_ng_min_size }}
organization = {{ organization }}
finops_project_name = {{ finops_project_name }}
finops_project_number = {{ finops_project_number }}
finops_project_role = {{ finops_project_role }}
cluster_name = "{{ cluster_name }}"
cluster_mailing_list = "{{ cluster_mailing_list }}"
eks_instance_disk_size = "{{ eks_instance_disk_size }}"
eks_ng_desired_size = "{{ eks_ng_desired_size }}"
eks_ng_max_size = "{{ eks_ng_max_size }}"
eks_ng_min_size = "{{ eks_ng_min_size }}"
organization = "{{ organization }}"
finops_project_name = "{{ finops_project_name }}"
finops_project_number = "{{ finops_project_number }}"
finops_project_role = "{{ finops_project_role }}"
tags = {
{% for key, value in tags.items() %}
{% for key, value in cluster_tags.items() %}
"{{ key }}" = "{{ value }}"
{% endfor %}
}
Expand Down
4 changes: 2 additions & 2 deletions ansible/templates/region.hcl.j2
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# {{ environment }}/{{ aws_region }}/region.hcl
# {{ env_name }}/{{ aws_region }}/region.hcl

# Set common variables for the region. This is automatically pulled in in the root terragrunt.hcl configuration to
# configure the remote state bucket and pass forward to the child modules as inputs.
locals {
aws_region = "{{ aws_region }}"
}
}
2 changes: 1 addition & 1 deletion ansible/templates/root.hcl.j2
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# {{environment}}/root.hcl
# {{ env_name }}/root.hcl
# ---------------------------------------------------------------------------------------------------------------------
# TERRAGRUNT CONFIGURATION
# Terragrunt is a thin wrapper for Terraform that provides extra tools for working with multiple Terraform modules,
Expand Down
Loading