Fix unexpected exception when a role has an empty argument_specs.yml (#75604)

* Fix role with empty argument_specs.yml

* Use try/except and add changelog fragment

* Always return a dict

* Add test for empty argument_specs key
pull/75690/head
devon-mar 3 years ago committed by GitHub
parent 55d448baa7
commit 3e7a622204
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,3 @@
---
bugfixes:
- roles - fix unexpected ``AttributeError`` when an empty ``argument_specs.yml`` is present (https://github.com/ansible/ansible/pull/75604).

@ -294,7 +294,10 @@ class Role(Base, Conditional, Taggable, CollectionSearch):
if self._loader.path_exists(full_path):
# Note: _load_role_yaml() takes care of rebuilding the path.
argument_specs = self._load_role_yaml('meta', main='argument_specs')
return argument_specs.get('argument_specs', {})
try:
return argument_specs.get('argument_specs') or {}
except AttributeError:
return {}
# We did not find the meta/argument_specs.[yml|yaml] file, so use the spec
# dict from the role meta data, if it exists. Ansible 2.11 and later will

@ -0,0 +1,3 @@
---
- debug:
msg: "Role with empty argument_specs key"

@ -0,0 +1,3 @@
---
- debug:
msg: "Role with empty argument_specs.yml"

@ -338,3 +338,19 @@
- debug: var=ansible_failed_result
- fail:
msg: "Should not get here"
- name: "New play to reset vars: Test empty argument_specs.yml"
hosts: localhost
gather_facts: false
tasks:
- name: Import role with an empty argument_specs.yml
import_role:
name: empty_file
- name: "New play to reset vars: Test empty argument_specs key"
hosts: localhost
gather_facts: false
tasks:
- name: Import role with an empty argument_specs key
import_role:
name: empty_argspec

Loading…
Cancel
Save