feat: Reorganizes some of the roles and renames some variables

This commit is contained in:
2024-11-27 16:59:22 -05:00
parent ee91b060eb
commit 2692509fbe
9 changed files with 159 additions and 94 deletions

View File

@@ -4,7 +4,7 @@ default:
# Run the playbook with the passed in arguments. # Run the playbook with the passed in arguments.
[group('plays')] [group('plays')]
run-playbook *ARGS: run *ARGS:
@ansible-playbook ./main.yml \ @ansible-playbook ./main.yml \
{{ARGS}} {{ARGS}}

View File

@@ -13,23 +13,13 @@
path: "{{ build_dir }}" path: "{{ build_dir }}"
state: directory state: directory
- name: Ensure template repo. - name: Parse template facts.
ansible.builtin.git: ansible.builtin.include_role:
repo: "{{ template.repo.url }}" name: "prepare-template-facts"
dest: "{{ template.path }}"
version: "{{ template.repo.version | default('main') }}"
when: template.repo.url is defined
- name: Check for repo vars directory.
ansible.builtin.stat:
path: "{{ template.path }}/{{ template.vars }}"
register: repo_vars
- name: Load repo vars if available.
ansible.builtin.include_vars:
dir: "{{ template.path }}/{{ template.vars }}"
when: repo_vars.stat.isdir is defined
- name: Load repo vars.
ansible.builtin.include_role:
name: "load-template-vars"
- name: Copy build files. - name: Copy build files.
ansible.builtin.copy: ansible.builtin.copy:

View File

@@ -0,0 +1,27 @@
---
# Used internally to clone the template repo, if applicable and load it's
# variables.
#
# NOTE: This expects that you've called prepare-template-facts first.
- name: Check if template path exists.
ansible.builtin.stat:
path: "{{ template_dir }}"
register: template_dir_stat
- name: Ensure repo.
ansible.builtin.git:
repo: "{{ template.repo.url }}"
dest: "{{ template_dir }}"
version: "{{ template.repo.version | default('main') }}"
when: template.repo.url is defined and not template_dir_stat.stat.exists
- name: Check for repo vars directory.
ansible.builtin.stat:
path: "{{ template_vars_path }}"
register: repo_vars
- name: Load repo vars if available.
ansible.builtin.include_vars:
dir: "{{ template_vars_path }}"
when: repo_vars.stat.isdir is defined

View File

@@ -0,0 +1,15 @@
---
# This role is used internally to parse template variables, depending on
# what is supplied.
#
- name: Set default template path.
ansible.builtin.set_fact:
repo_template_path: "{{ build_dir }}/template"
- name: Parse template path.
ansible.builtin.set_fact:
template_dir: "{{ template.path | default(repo_template_path) }}"
- name: Parse template vars path.
ansible.builtin.set_fact:
template_vars_path: "{{ template_dir }}/{{ template.vars | default('repo_vars') }}"

View File

@@ -1,29 +1,49 @@
--- ---
# TODO: When using a template repo, we should probably clone it into # template:
# the project directory somewhere. # path: "/path/to/local/template/dir
# vars: "repo_vars" (optional path inside template directory to find variables, defaults to 'repo_vars')
# template_dir: #
# path: "/path/to/template/dir # OR
# vars: "repo_vars" #
# repo: (optional if using a repo as a template) # template:
# repo:
# url: "https://example.com/template.git
# version: "1.0.0" or "branch" (tagging to a version is more ideal)
#
template: template:
#path: "/path/to/template/dir"
#vars: "repo_vars"
# When using a repository as a template dir. In general, it's
# probably best to pin to a particular version of the repo template
# instead of a branch.
#
# repo:
# url: "https://example.com/repo.git"
# version: "main"
#repo: {}
# The preject directory to setup in. # The preject directory to setup in.
project_dir: "{{ lookup('env', 'PWD') }}" project_dir: "{{ lookup('env', 'PWD') }}"
# Files that are copied from the template directory to the output # This path get's setup / parsed based on the template variable,
# it will point to the directory of the template, which could be a
# local path on the system or inside of the project directory, depending
# on if the template is a repo or not.
#
# This is safe to use inside of the project or template specifications
# for paths to files that live in the template directory not the project
# directory. # directory.
copy_on_setup: template_dir: ""
- "Report.md"
- "vars.yml" # Files or directories that are copied from the template directory to the project
# directory.
#
# These can be a simple item that is a path from the root of the template directory
# to a file, which will copy the file to the root of the project directory or
# in the form of:
#
# src: "path/in/template/dir"
# dest: "path/in/project/dir"
# mode: '0600' (optional mode of the file/dir to copy)
#
copy_on_setup: []
# Copies the entire contents of a directory to the root of the project directory.
#
# This is useful if you keep all the template files in a sub-directory of your project
# template, it will copy that entire directory over when setting up a new project.
#
# NOTE: If the project has been setup (indicated by a .setup file) this
# will be skipped so that it does not overwrite any changes to the
# project files. This ensures that a project is only setup once.
copy_directory_on_setup: []

View File

@@ -10,27 +10,20 @@
- debug - debug
- never - never
- name: Parse template path. - name: Debug build dir.
ansible.builtin.set_fact:
template:
path: "{{ project_dir }}/{{ build_dir | default('.build') }}/template"
when: not template.path is defined
- name: Parse template vars.
ansible.builtin.set_fact:
template:
vars: "repo_vars"
when: not template.vars is defined
# - name: Prepare variables.
# ansible.builtin.include_tasks:
# file: "prepare_vars.yml"
# vars:
# template: "{{ template }}"
- name: Debug template vars post parse.
ansible.builtin.debug: ansible.builtin.debug:
var: template var: build_dir
tags:
- debug
- never
- name: Parse template facts.
ansible.builtin.include_role:
name: "prepare-template-facts"
- name: Debug template path post parse.
ansible.builtin.debug:
var: template_dir
tags: tags:
- debug - debug
- never - never
@@ -40,22 +33,9 @@
path: "{{ project_dir }}" path: "{{ project_dir }}"
state: directory state: directory
- name: Ensure repo. - name: Load template vars.
ansible.builtin.git: ansible.builtin.include_role:
repo: "{{ template.repo.url }}" name: "load-template-vars"
dest: "{{ template.path }}"
version: "{{ template.repo.version | default('main') }}"
when: template.repo.url is defined
- name: Check for repo vars directory.
ansible.builtin.stat:
path: "{{ template.path }}/{{ template.vars }}"
register: repo_vars
- name: Load repo vars if available.
ansible.builtin.include_vars:
dir: "{{ template.path }}/{{ template.vars }}"
when: repo_vars.stat.isdir is defined
- name: Debug on_setup. - name: Debug on_setup.
ansible.builtin.debug: ansible.builtin.debug:
@@ -64,11 +44,52 @@
- debug - debug
- never - never
- name: Debug copy directory contents.
ansible.builtin.debug:
var: unsafe_copy_directory_on_setup
tags:
- debug
- never
- name: Check if project has been previously setup.
ansible.builtin.stat:
path: "{{ project_dir }}/.setup"
register: setup_file
- name: Debug setup file stat.
ansible.builtin.debug:
var: setup_file.exists
tags:
- debug
- never
- name: Copy directory contents to project directory.
ansible.builtin.command: |
cp -r "{{ template_dir }}/{{ item.src | default(item) }}/." \
"{{ item.dest | default(project_dir) }}"
with_items: "{{ copy_directory_on_setup }}"
when: setup_file.stat.exists is false
register: copy_directory_stat
- name: Debug copy directory stat.
ansible.builtin.debug:
var: copy_directory_stat
tags:
- debug
- never
- name: Copy project files. - name: Copy project files.
ansible.builtin.include_tasks: ansible.builtin.include_tasks:
file: "copy_if_not_exists.yml" file: "copy_if_not_exists.yml"
vars: vars:
source: "{{ template.path }}/{{ item.src | default(item) }}" source: "{{ template_dir }}/{{ item.src | default(item) }}"
destination: "{{ project_dir }}/{{ item.dest | default(item) }}" destination: "{{ project_dir }}/{{ item.dest | default(item) }}"
mode: "{{ item.mode | default('0600') }}" mode: "{{ item.mode | default('0600') }}"
loop: "{{ copy_on_setup }}" loop: "{{ copy_on_setup }}"
- name: Create setup file.
ansible.builtin.template:
src: "templates/setup.txt"
dest: "{{ project_dir }}/.setup"
mode: '0600'
when: not setup_file.stat.exists

View File

@@ -1,16 +0,0 @@
---
- name: Parse template path.
ansible.builtin.set_fact:
template:
path: "{{ project_dir }}/{{ build_dir | default('.build') }}/template"
when: not template.path is defined
tags:
- always
- name: Parse template vars.
ansible.builtin.set_fact:
template:
vars: "repo_vars"
when: not template.vars is defined
tags:
- always

View File

@@ -0,0 +1,7 @@
This file is managed by the ansible-hpa-playbook. It is an indication
that the project using the template.
{{ template.repo.url | default(template_dir) }}
Has already called setup. This file should not be removed or subsequent calls
to setup may overwrite existing data.

View File

@@ -29,7 +29,8 @@
tasks: tasks:
- name: Test complex var, set fact. - name: Test complex var, set fact.
ansible.builtin.set_fact: ansible.builtin.set_fact:
template_path: "{{ template.path | default('.build') }}" template:
path: "{{ template.path | default('.build') }}"
vars: vars:
template: template:
repo: repo:
@@ -54,7 +55,7 @@
- name: Test complex var was set. - name: Test complex var was set.
ansible.builtin.debug: ansible.builtin.debug:
msg: "Template path: {{ template_path }}, specified: {{ template_path_specified | default(false) }}" msg: "Template path: {{ template.path }}, specified: {{ template_path_specified | default(false) }}"
tags: tags:
- vars - vars
- never - never