diff --git a/changelogs/fragments/ansible-galaxy-init-extra-vars.yml b/changelogs/fragments/ansible-galaxy-init-extra-vars.yml new file mode 100644 index 00000000000..da013a7ef25 --- /dev/null +++ b/changelogs/fragments/ansible-galaxy-init-extra-vars.yml @@ -0,0 +1,5 @@ +minor_changes: +- >- + ``ansible-galaxy role|collection init`` - accept ``--extra-vars`` to + supplement/override the variables ``ansible-galaxy`` injects for templating + ``.j2`` files in the skeleton. diff --git a/lib/ansible/cli/galaxy.py b/lib/ansible/cli/galaxy.py index 7803bdd9a3f..805bd650372 100755 --- a/lib/ansible/cli/galaxy.py +++ b/lib/ansible/cli/galaxy.py @@ -61,6 +61,7 @@ from ansible.template import Templar from ansible.utils.collection_loader import AnsibleCollectionConfig from ansible.utils.display import Display from ansible.utils.plugin_docs import get_versioned_doclink +from ansible.utils.vars import load_extra_vars display = Display() urlparse = six.moves.urllib.parse.urlparse @@ -366,6 +367,7 @@ class GalaxyCLI(CLI): init_parser.add_argument('--type', dest='role_type', action='store', default='default', help="Initialize using an alternate role type. Valid types include: 'container', " "'apb' and 'network'.") + opt_help.add_runtask_options(init_parser) def add_remove_options(self, parser, parents=None): remove_parser = parser.add_parser('remove', parents=parents, help='Delete roles from roles_path.') @@ -1171,6 +1173,7 @@ class GalaxyCLI(CLI): ) loader = DataLoader() + inject_data.update(load_extra_vars(loader)) templar = Templar(loader, variables=inject_data) # create role directory @@ -1214,7 +1217,11 @@ class GalaxyCLI(CLI): src_template = os.path.join(root, f) dest_file = os.path.join(obj_path, rel_root, filename) template_data = to_text(loader._get_file_contents(src_template)[0], errors='surrogate_or_strict') - b_rendered = to_bytes(templar.template(template_data), errors='surrogate_or_strict') + try: + b_rendered = to_bytes(templar.template(template_data), errors='surrogate_or_strict') + except AnsibleError as e: + shutil.rmtree(b_obj_path) + raise AnsibleError(f"Failed to create {galaxy_type.title()} {obj_name}. Templating {src_template} failed with the error: {e}") from e with open(dest_file, 'wb') as df: df.write(b_rendered) else: diff --git a/test/integration/targets/ansible-galaxy-collection/tasks/init.yml b/test/integration/targets/ansible-galaxy-collection/tasks/init.yml index 46198fef390..6a00553efcb 100644 --- a/test/integration/targets/ansible-galaxy-collection/tasks/init.yml +++ b/test/integration/targets/ansible-galaxy-collection/tasks/init.yml @@ -128,11 +128,18 @@ - link: custom_skeleton/galaxy.yml source: galaxy.yml + - name: create j2 file + copy: + dest: "{{ galaxy_dir }}/scratch/skeleton/custom_skeleton/README.j2" + content: !unsafe | + Requires ansible-core >={{ min_ansible_version }} + - name: initialize a collection using the skeleton - command: ansible-galaxy collection init ansible_test.my_collection {{ init_path }} {{ skeleton }} + command: ansible-galaxy collection init ansible_test.my_collection {{ init_path }} {{ skeleton }} {{ extra }} vars: init_path: '--init-path {{ galaxy_dir }}/scratch/skeleton' skeleton: '--collection-skeleton {{ galaxy_dir }}/scratch/skeleton/custom_skeleton' + extra: '-e min_ansible_version="2.17"' - name: stat expected collection contents stat: @@ -143,6 +150,7 @@ - plugins/inventory - galaxy.yml - plugins/inventory/foo.py + - README - assert: that: @@ -150,7 +158,18 @@ - stat_result.results[1].stat.islnk - stat_result.results[2].stat.islnk - stat_result.results[3].stat.isreg + - stat_result.results[4].stat.isreg + + - name: Verify the README was templated successfully + copy: + dest: "{{ galaxy_dir }}/scratch/skeleton/ansible_test/my_collection/README" + content: | + Requires ansible-core >=2.17 + register: validate_readme_content + - assert: + that: + - not validate_readme_content.changed always: - name: cleanup file: