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

* 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

(cherry picked from commit 3e7a622204)

Co-authored-by: devon-mar <devon-mar@users.noreply.github.com>
pull/75864/head
Sloane Hertel 3 years ago committed by GitHub
parent 15f5886479
commit 4967b8650c
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): if self._loader.path_exists(full_path):
# Note: _load_role_yaml() takes care of rebuilding the path. # Note: _load_role_yaml() takes care of rebuilding the path.
argument_specs = self._load_role_yaml('meta', main='argument_specs') 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 # 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 # 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 - debug: var=ansible_failed_result
- fail: - fail:
msg: "Should not get here" 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