mirror of
https://github.com/ansible/ansible.git
synced 2026-08-03 08:03:05 +02:00
add --extra-vars for ansible-galaxy role|collection init (#82605)
allow extra vars when templating j2 files in the skeleton, for example: ansible-galaxy init --role-skeleton /path/to/skeleton --extra-vars @/path/to/vars_file.yml newrole ansible-galaxy init --extra-vars "min_ansible_version=2.17.0" newrole
This commit is contained in:
@@ -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.
|
||||
@@ -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:
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user