From 2692509fbe6c46be7c5b30ed828b18aa5a8ba49e Mon Sep 17 00:00:00 2001 From: Michael Housh Date: Wed, 27 Nov 2024 16:59:22 -0500 Subject: [PATCH] feat: Reorganizes some of the roles and renames some variables --- justfile | 2 +- roles/build-project/tasks/main.yml | 22 ++--- roles/load-template-vars/tasks/main.yml | 27 ++++++ roles/prepare-template-facts/tasks/main.yml | 15 ++++ roles/setup-project/defaults/main.yml | 64 +++++++++----- roles/setup-project/tasks/main.yml | 95 +++++++++++++-------- roles/setup-project/tasks/prepare_vars.yml | 16 ---- roles/setup-project/templates/setup.txt | 7 ++ test/test.yml | 5 +- 9 files changed, 159 insertions(+), 94 deletions(-) create mode 100644 roles/load-template-vars/tasks/main.yml create mode 100644 roles/prepare-template-facts/tasks/main.yml delete mode 100644 roles/setup-project/tasks/prepare_vars.yml create mode 100644 roles/setup-project/templates/setup.txt diff --git a/justfile b/justfile index 3c86cda..1672d52 100644 --- a/justfile +++ b/justfile @@ -4,7 +4,7 @@ default: # Run the playbook with the passed in arguments. [group('plays')] -run-playbook *ARGS: +run *ARGS: @ansible-playbook ./main.yml \ {{ARGS}} diff --git a/roles/build-project/tasks/main.yml b/roles/build-project/tasks/main.yml index ffffe01..222f099 100644 --- a/roles/build-project/tasks/main.yml +++ b/roles/build-project/tasks/main.yml @@ -13,23 +13,13 @@ path: "{{ build_dir }}" state: directory -- name: Ensure template repo. - ansible.builtin.git: - repo: "{{ template.repo.url }}" - 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: Parse template facts. + ansible.builtin.include_role: + name: "prepare-template-facts" +- name: Load repo vars. + ansible.builtin.include_role: + name: "load-template-vars" - name: Copy build files. ansible.builtin.copy: diff --git a/roles/load-template-vars/tasks/main.yml b/roles/load-template-vars/tasks/main.yml new file mode 100644 index 0000000..b5c12b3 --- /dev/null +++ b/roles/load-template-vars/tasks/main.yml @@ -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 diff --git a/roles/prepare-template-facts/tasks/main.yml b/roles/prepare-template-facts/tasks/main.yml new file mode 100644 index 0000000..9d7b2bc --- /dev/null +++ b/roles/prepare-template-facts/tasks/main.yml @@ -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') }}" diff --git a/roles/setup-project/defaults/main.yml b/roles/setup-project/defaults/main.yml index 2901784..462d2bc 100644 --- a/roles/setup-project/defaults/main.yml +++ b/roles/setup-project/defaults/main.yml @@ -1,29 +1,49 @@ --- -# TODO: When using a template repo, we should probably clone it into -# the project directory somewhere. - -# template_dir: -# path: "/path/to/template/dir -# vars: "repo_vars" -# repo: (optional if using a repo as a template) +# template: +# path: "/path/to/local/template/dir +# vars: "repo_vars" (optional path inside template directory to find variables, defaults to 'repo_vars') +# +# OR +# +# template: +# repo: +# url: "https://example.com/template.git +# version: "1.0.0" or "branch" (tagging to a version is more ideal) +# 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. 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. -copy_on_setup: - - "Report.md" - - "vars.yml" +template_dir: "" + +# 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: [] diff --git a/roles/setup-project/tasks/main.yml b/roles/setup-project/tasks/main.yml index 6322bdf..53112a6 100644 --- a/roles/setup-project/tasks/main.yml +++ b/roles/setup-project/tasks/main.yml @@ -10,27 +10,20 @@ - debug - never -- name: Parse template path. - 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. +- name: Debug build dir. 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: - debug - never @@ -40,22 +33,9 @@ path: "{{ project_dir }}" state: directory -- name: Ensure repo. - ansible.builtin.git: - repo: "{{ template.repo.url }}" - 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 template vars. + ansible.builtin.include_role: + name: "load-template-vars" - name: Debug on_setup. ansible.builtin.debug: @@ -64,11 +44,52 @@ - debug - 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. ansible.builtin.include_tasks: file: "copy_if_not_exists.yml" vars: - source: "{{ template.path }}/{{ item.src | default(item) }}" + source: "{{ template_dir }}/{{ item.src | default(item) }}" destination: "{{ project_dir }}/{{ item.dest | default(item) }}" mode: "{{ item.mode | default('0600') }}" 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 diff --git a/roles/setup-project/tasks/prepare_vars.yml b/roles/setup-project/tasks/prepare_vars.yml deleted file mode 100644 index 71334ad..0000000 --- a/roles/setup-project/tasks/prepare_vars.yml +++ /dev/null @@ -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 diff --git a/roles/setup-project/templates/setup.txt b/roles/setup-project/templates/setup.txt new file mode 100644 index 0000000..75ce69f --- /dev/null +++ b/roles/setup-project/templates/setup.txt @@ -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. diff --git a/test/test.yml b/test/test.yml index f446080..b02788f 100644 --- a/test/test.yml +++ b/test/test.yml @@ -29,7 +29,8 @@ tasks: - name: Test complex var, set fact. ansible.builtin.set_fact: - template_path: "{{ template.path | default('.build') }}" + template: + path: "{{ template.path | default('.build') }}" vars: template: repo: @@ -54,7 +55,7 @@ - name: Test complex var was set. 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: - vars - never