diff --git a/changelogs/fragments/no_empty_groups.yml b/changelogs/fragments/no_empty_groups.yml new file mode 100644 index 00000000000..0c80f646507 --- /dev/null +++ b/changelogs/fragments/no_empty_groups.yml @@ -0,0 +1,2 @@ +bugfixes: + - avoid empty groups in ansbile-inventory JSON output as they will be interpreted as hosts diff --git a/lib/ansible/cli/inventory.py b/lib/ansible/cli/inventory.py index 788a9fa9232..6590c98103c 100644 --- a/lib/ansible/cli/inventory.py +++ b/lib/ansible/cli/inventory.py @@ -329,6 +329,8 @@ class InventoryCLI(CLI): results[group.name]['vars'] = self._get_group_variables(group) self._remove_empty(results[group.name]) + if not results[group.name]: + del results[group.name] return results