diff --git a/roles/setup-project/defaults/main.yml b/roles/setup-project/defaults/main.yml index 5b3f56e..2901784 100644 --- a/roles/setup-project/defaults/main.yml +++ b/roles/setup-project/defaults/main.yml @@ -1,12 +1,14 @@ --- +# 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/template/dir" - vars: "repo_vars" + #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 @@ -15,10 +17,10 @@ template: # repo: # url: "https://example.com/repo.git" # version: "main" - repo: {} + #repo: {} -# The output directory to setup a project in. -output_dir: "{{ lookup('env', 'PWD') }}" +# The preject directory to setup in. +project_dir: "{{ lookup('env', 'PWD') }}" # Files that are copied from the template directory to the output # directory. diff --git a/roles/setup-project/tasks/main.yml b/roles/setup-project/tasks/main.yml index 3500f99..6322bdf 100644 --- a/roles/setup-project/tasks/main.yml +++ b/roles/setup-project/tasks/main.yml @@ -1,11 +1,43 @@ --- - name: Starting setup project. ansible.builtin.debug: - msg: "Output dir: {{ output_dir }}" + msg: "Project dir: {{ project_dir }}" + +- name: Debug template vars pre parse. + ansible.builtin.debug: + var: template + tags: + - 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. + ansible.builtin.debug: + var: template + tags: + - debug + - never - name: Ensure output directory exists. ansible.builtin.file: - path: "{{ output_dir }}" + path: "{{ project_dir }}" state: directory - name: Ensure repo. @@ -37,6 +69,6 @@ file: "copy_if_not_exists.yml" vars: source: "{{ template.path }}/{{ item.src | default(item) }}" - destination: "{{ output_dir }}/{{ item.dest | default(item) }}" + destination: "{{ project_dir }}/{{ item.dest | default(item) }}" mode: "{{ item.mode | default('0600') }}" loop: "{{ copy_on_setup }}" diff --git a/roles/setup-project/tasks/prepare_vars.yml b/roles/setup-project/tasks/prepare_vars.yml new file mode 100644 index 0000000..71334ad --- /dev/null +++ b/roles/setup-project/tasks/prepare_vars.yml @@ -0,0 +1,16 @@ +--- +- 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/test/test.yml b/test/test.yml index bc22541..f446080 100644 --- a/test/test.yml +++ b/test/test.yml @@ -10,7 +10,7 @@ template_dir: path: "/tmp/hpa-playbook-tmp" vars: "repo_vars" - output_dir: "/tmp/hpa-setup-project-tmp" + project_dir: "/tmp/hpa-setup-project-tmp" - role: repo-template tags: @@ -25,3 +25,36 @@ path: "/tmp/hpa-playbook-tmp" vars: "repo_vars" project_dir: "/tmp/hpa-setup-project-tmp" + + tasks: + - name: Test complex var, set fact. + ansible.builtin.set_fact: + template_path: "{{ template.path | default('.build') }}" + vars: + template: + repo: + url: "https://example.com" + version: "main" + tags: + - vars + - never + + - name: Test complex var, set fact when path is specified. + ansible.builtin.set_fact: + template_path_specified: true + vars: + template: + repo: + url: "https://example.com" + version: "main" + when: template.path is defined + tags: + - vars + - never + + - name: Test complex var was set. + ansible.builtin.debug: + msg: "Template path: {{ template_path }}, specified: {{ template_path_specified | default(false) }}" + tags: + - vars + - never