From 916e62f6e6cd5896f026199df4c9c75259d2901c Mon Sep 17 00:00:00 2001 From: Michael Housh Date: Tue, 26 Nov 2024 14:26:58 -0500 Subject: [PATCH] feat: Initial roles --- justfile | 28 ++++++++--- main.yml | 11 ++++- roles/build-project/defaults/main.yml | 31 +++++++++++++ roles/build-project/tasks/main.yml | 46 +++++++++++++++++++ roles/create_if_not_exists/defaults/main.yml | 6 --- .../defaults/main.yml | 1 + .../files/Definitions.md | 0 .../files/Report.md | 0 .../files/footer.tex | 0 .../files/head.tex | 0 .../files/vars.default.yml | 0 roles/repo-template/files/vars.repo.yml | 5 ++ .../files/vars.vault.yml | 0 .../files/vault.default.yml | 0 .../tasks/main.yml | 24 +++++----- roles/setup-project/defaults/main.yml | 26 +++++++++-- .../tasks/copy_if_not_exists.yml} | 8 ++-- roles/setup-project/tasks/main.yml | 40 +++++++++++++--- test/test.yml | 21 +++++++-- 19 files changed, 203 insertions(+), 44 deletions(-) create mode 100644 roles/build-project/defaults/main.yml create mode 100644 roles/build-project/tasks/main.yml delete mode 100644 roles/create_if_not_exists/defaults/main.yml rename roles/{repo_template => repo-template}/defaults/main.yml (69%) rename roles/{repo_template => repo-template}/files/Definitions.md (100%) rename roles/{repo_template => repo-template}/files/Report.md (100%) rename roles/{repo_template => repo-template}/files/footer.tex (100%) rename roles/{repo_template => repo-template}/files/head.tex (100%) rename roles/{repo_template => repo-template}/files/vars.default.yml (100%) create mode 100644 roles/repo-template/files/vars.repo.yml rename roles/{repo_template => repo-template}/files/vars.vault.yml (100%) rename roles/{repo_template => repo-template}/files/vault.default.yml (100%) rename roles/{repo_template => repo-template}/tasks/main.yml (62%) rename roles/{create_if_not_exists/tasks/main.yml => setup-project/tasks/copy_if_not_exists.yml} (52%) diff --git a/justfile b/justfile index 84b2589..3c86cda 100644 --- a/justfile +++ b/justfile @@ -1,13 +1,29 @@ -work_dir := "/tmp/hpa-playbook-tmp" +[private] +default: + just --list +# Run the playbook with the passed in arguments. +[group('plays')] run-playbook *ARGS: @ansible-playbook ./main.yml \ - --extra-vars output_dir={{work_dir}} \ {{ARGS}} +# Run the repo-template option in the `dir` with the passed in arguements. +[group('plays')] +create-repo-template dir *ARGS: + @just run-playbook \ + --tags repo-template \ + --extra-vars output_dir={{dir}} \ + {{ARGS}} + +# Run the build-project option in the `dir` with the passed in arguements. +[group('plays')] +build-project dir *ARGS: + @just run-playbook \ + --tags build-project \ + --extra-vars project_dir={{dir}} \ + {{ARGS}} + +[group('test')] test *ARGS: @ansible-playbook ./test/test.yml {{ARGS}} - -[group("utilities")] -clean: - @rm -rf {{work_dir}} diff --git a/main.yml b/main.yml index dfc1cf3..33d4184 100644 --- a/main.yml +++ b/main.yml @@ -2,6 +2,15 @@ - name: HPA Playbook hosts: all roles: - - role: "repo_template" + - role: repo-template tags: + - repo-template + - never # makes it so a tag must be supplied to run. + - role: build-project + tags: + - build-project + - never # makes it so a tag must be supplied to run. + - role: setup-project + tags: + - setup-project - never # makes it so a tag must be supplied to run. diff --git a/roles/build-project/defaults/main.yml b/roles/build-project/defaults/main.yml new file mode 100644 index 0000000..37569a9 --- /dev/null +++ b/roles/build-project/defaults/main.yml @@ -0,0 +1,31 @@ +--- +build_dir_name: ".build" +build_dir: "{{ project_dir }}/{{ build_dir_name }}" + +project_dir: "{{ lookup('env', 'PWD') }}" +project_vars_dir: "{{ project_dir }}" + +# template_dir: +# path: "/path/to/template/dir +# vars: "repo_vars" +# repo: (optional if using a repo as a template) +template_dir: + 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: {} + +copy_on_build: + - "{{ template_dir.path }}/head.tex" + - "{{ template_dir.path }}/Definitions.md" + +template_on_build: + - "{{ project_dir }}/Report.md" + - "{{ template_dir.path }}/footer.tex" diff --git a/roles/build-project/tasks/main.yml b/roles/build-project/tasks/main.yml new file mode 100644 index 0000000..34dbc79 --- /dev/null +++ b/roles/build-project/tasks/main.yml @@ -0,0 +1,46 @@ +--- +- name: Starting build project. + ansible.builtin.debug: + msg: "Build dir: {{ build_dir }}" + +- name: Load project vars. + ansible.builtin.include_vars: + dir: "{{ project_vars_dir }}" + ignore_unknown_extensions: true + +- name: Ensure build directory exists. + ansible.builtin.file: + path: "{{ build_dir }}" + state: directory + +- name: Ensure template repo. + ansible.builtin.git: + repo: "{{ template_dir.repo.url }}" + dest: "{{ template_dir.path }}" + version: "{{ template_dir.repo.version | default('main') }}" + when: template_dir.repo.url is defined + +- name: Check for repo vars directory. + ansible.builtin.stat: + path: "{{ template_dir.path }}/{{ template_dir.vars }}" + register: repo_vars + +- name: Load repo vars if available. + ansible.builtin.include_vars: + dir: "{{ template_dir.path }}/{{ template_dir.vars }}" + when: repo_vars.stat.isdir is defined + + +- name: Copy build files. + ansible.builtin.copy: + src: "{{ item }}" + dest: "{{ build_dir }}/{{ item | basename }}" + mode: '0600' + with_items: "{{ copy_on_build }}" + +- name: Template build files. + ansible.builtin.template: + src: "{{ item }}" + dest: "{{ build_dir }}/{{ item | basename }}" + mode: '0600' + with_items: "{{ template_on_build }}" diff --git a/roles/create_if_not_exists/defaults/main.yml b/roles/create_if_not_exists/defaults/main.yml deleted file mode 100644 index 39514f6..0000000 --- a/roles/create_if_not_exists/defaults/main.yml +++ /dev/null @@ -1,6 +0,0 @@ ---- - -# Variables for use with the copy_if_not_exists role. -source: "" -destination: "" -mode: '0600' diff --git a/roles/repo_template/defaults/main.yml b/roles/repo-template/defaults/main.yml similarity index 69% rename from roles/repo_template/defaults/main.yml rename to roles/repo-template/defaults/main.yml index f7311b4..0778bd7 100644 --- a/roles/repo_template/defaults/main.yml +++ b/roles/repo-template/defaults/main.yml @@ -2,3 +2,4 @@ output_dir: "{{ lookup('env', 'PWD') }}" use_vault: true +repo_vars_dir: "repo_vars" diff --git a/roles/repo_template/files/Definitions.md b/roles/repo-template/files/Definitions.md similarity index 100% rename from roles/repo_template/files/Definitions.md rename to roles/repo-template/files/Definitions.md diff --git a/roles/repo_template/files/Report.md b/roles/repo-template/files/Report.md similarity index 100% rename from roles/repo_template/files/Report.md rename to roles/repo-template/files/Report.md diff --git a/roles/repo_template/files/footer.tex b/roles/repo-template/files/footer.tex similarity index 100% rename from roles/repo_template/files/footer.tex rename to roles/repo-template/files/footer.tex diff --git a/roles/repo_template/files/head.tex b/roles/repo-template/files/head.tex similarity index 100% rename from roles/repo_template/files/head.tex rename to roles/repo-template/files/head.tex diff --git a/roles/repo_template/files/vars.default.yml b/roles/repo-template/files/vars.default.yml similarity index 100% rename from roles/repo_template/files/vars.default.yml rename to roles/repo-template/files/vars.default.yml diff --git a/roles/repo-template/files/vars.repo.yml b/roles/repo-template/files/vars.repo.yml new file mode 100644 index 0000000..9370511 --- /dev/null +++ b/roles/repo-template/files/vars.repo.yml @@ -0,0 +1,5 @@ +--- +on_setup: + - "Report.md" + - "vars.yml" + - "vault.yml" # optional if using vault. diff --git a/roles/repo_template/files/vars.vault.yml b/roles/repo-template/files/vars.vault.yml similarity index 100% rename from roles/repo_template/files/vars.vault.yml rename to roles/repo-template/files/vars.vault.yml diff --git a/roles/repo_template/files/vault.default.yml b/roles/repo-template/files/vault.default.yml similarity index 100% rename from roles/repo_template/files/vault.default.yml rename to roles/repo-template/files/vault.default.yml diff --git a/roles/repo_template/tasks/main.yml b/roles/repo-template/tasks/main.yml similarity index 62% rename from roles/repo_template/tasks/main.yml rename to roles/repo-template/tasks/main.yml index 1a1ac72..347662f 100644 --- a/roles/repo_template/tasks/main.yml +++ b/roles/repo-template/tasks/main.yml @@ -2,35 +2,34 @@ - name: Starting repo template role. ansible.builtin.debug: msg: "Output directory: {{ output_dir }}" - tags: - - "repo-template" - name: Ensure output directory exists. ansible.builtin.file: path: "{{ output_dir }}" state: directory - tags: - - repo-template + +- name: Ensure repo vars directory. + ansible.builtin.file: + path: "{{ output_dir }}/{{ repo_vars_dir }}" + state: directory - name: Copy general files. ansible.builtin.copy: - src: "files/{{ item }}" - dest: "{{ output_dir }}/{{ item }}" + src: "files/{{ item.src | default(item) }}" + dest: "{{ output_dir }}/{{ item.dest | default(item) }}" with_items: - "Definitions.md" - "Report.md" - "head.tex" - "footer.tex" - tags: - - "repo-template" + - src: "vars.repo.yml" + dest: "{{ repo_vars_dir }}/vars.yml" - name: Copy basic vars files. ansible.builtin.copy: src: "files/vars.default.yml" dest: "{{ output_dir }}/vars.yml" - when: "not 'with-vault' in ansible_run_tags" - tags: - - "repo-template" + when: not 'with-vault' in ansible_run_tags or use_vault | bool == False - name: Copy vault and vars files. ansible.builtin.copy: @@ -41,6 +40,7 @@ dest: "vars.yml" - src: "vault.default.yml" dest: "vault.yml" + when: "'with_vault' in ansible_run_tags or use_vault | bool == True" tags: - - "with-vault" + - with-vault - never diff --git a/roles/setup-project/defaults/main.yml b/roles/setup-project/defaults/main.yml index 4343d65..c7d4592 100644 --- a/roles/setup-project/defaults/main.yml +++ b/roles/setup-project/defaults/main.yml @@ -1,11 +1,27 @@ --- -template_repo: - repo: "https://example.com/repo.git" - version: "main" - dest: "/path/to/clone" +# template_dir: +# path: "/path/to/template/dir +# vars: "repo_vars" +# repo: (optional if using a repo as a template) +template_dir: + 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 output directory to setup a project in. output_dir: "{{ lookup('env', 'PWD') }}" -files_to_copy: +# Files that are copied from the template directory to the output +# directory. +copy_on_setup: - "Report.md" - "vars.yml" diff --git a/roles/create_if_not_exists/tasks/main.yml b/roles/setup-project/tasks/copy_if_not_exists.yml similarity index 52% rename from roles/create_if_not_exists/tasks/main.yml rename to roles/setup-project/tasks/copy_if_not_exists.yml index 84f35bb..bbf29c3 100644 --- a/roles/create_if_not_exists/tasks/main.yml +++ b/roles/setup-project/tasks/copy_if_not_exists.yml @@ -1,13 +1,15 @@ --- -# Check if a file exists at the destination already, copy it if not. -- name: "Check if {{ destination | basename }} exists." +# NOTE: This is just an internal task that checks if a file exists +# before copying, to prevent changes from being overwritten. +# +- name: "Check if {{ destination | basename }} exists already." ansible.builtin.stat: path: "{{ destination }}" register: filestat tags: - always -- name: "Copy {{ source }} file to {{ destination }}." +- name: "Copy {{ destination | basename }} file." ansible.builtin.copy: src: "{{ source }}" dest: "{{ destination }}" diff --git a/roles/setup-project/tasks/main.yml b/roles/setup-project/tasks/main.yml index 8270a76..da4c9ca 100644 --- a/roles/setup-project/tasks/main.yml +++ b/roles/setup-project/tasks/main.yml @@ -2,13 +2,41 @@ - name: Starting setup project. ansible.builtin.debug: msg: "Output dir: {{ output_dir }}" - tags: - - setup-project + +- name: Ensure output directory exists. + ansible.builtin.file: + path: "{{ output_dir }}" + state: directory - name: Ensure repo. ansible.builtin.git: - repo: "{{ template_repo.repo }}" - dest: "{{ template_repo.dest }}" - version: "{{ template_repo.version }}" + repo: "{{ template_dir.repo.url }}" + dest: "{{ template_dir.path }}" + version: "{{ template_dir.repo.version | default('main') }}" + when: template_dir.repo.url is defined + +- name: Check for repo vars directory. + ansible.builtin.stat: + path: "{{ template_dir.path }}/{{ template_dir.vars }}" + register: repo_vars + +- name: Load repo vars if available. + ansible.builtin.include_vars: + dir: "{{ template_dir.path }}/{{ template_dir.vars }}" + when: repo_vars.stat.isdir is defined + +- name: Debug on_setup. + ansible.builtin.debug: + msg: "On setup vars: {{ copy_on_setup }}" tags: - - setup-project + - debug + - never + +- name: Copy project files. + ansible.builtin.include_tasks: + file: "copy_if_not_exists.yml" + vars: + source: "{{ template_dir.path }}/{{ item }}" + destination: "{{ output_dir }}/{{ item }}" + mode: '0600' + with_items: "{{ copy_on_setup }}" diff --git a/test/test.yml b/test/test.yml index 14c4c00..bc22541 100644 --- a/test/test.yml +++ b/test/test.yml @@ -4,13 +4,24 @@ roles: - role: setup-project tags: + - setup-project - never # force passing tags to run. vars: - template_repo: - repo: "https://git.housh.dev/michael/ansible-hpa-playbook.git" - dest: "/tmp/playbook-clone" - version: "main" + template_dir: + path: "/tmp/hpa-playbook-tmp" + vars: "repo_vars" + output_dir: "/tmp/hpa-setup-project-tmp" - - role: repo_template + - role: repo-template tags: + - repo-template - never # force passing tags to run. + - role: build-project + tags: + - build-project + - never # force passing tags to run. + vars: + template_dir: + path: "/tmp/hpa-playbook-tmp" + vars: "repo_vars" + project_dir: "/tmp/hpa-setup-project-tmp"