mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Yaml inventory more tolerant (#48883)
* make yaml inv more tolerant to comments * add tests for bad inventory processing fixes #47254
This commit is contained in:
parent
bd1050dfc7
commit
fc71cde7d3
7 changed files with 112 additions and 1 deletions
2
changelogs/fragments/yaml_inventory_more_tolerant.yml
Normal file
2
changelogs/fragments/yaml_inventory_more_tolerant.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- make YAML inventory more tolerant to comments/empty/None entries
|
|
@ -135,7 +135,13 @@ class InventoryModule(BaseFileInventoryPlugin):
|
||||||
|
|
||||||
for key in group_data:
|
for key in group_data:
|
||||||
|
|
||||||
if key == 'vars':
|
if not isinstance(group_data[key], (MutableMapping, NoneType)):
|
||||||
|
self.display.warning('Skipping key (%s) in group (%s) as it is not a mapping, it is a %s' % (key, group, type(group_data[key])))
|
||||||
|
continue
|
||||||
|
|
||||||
|
if isinstance(group_data[key], NoneType):
|
||||||
|
self.display.vvv('Skipping empty key (%s) in group (%s)' % (key, group))
|
||||||
|
elif key == 'vars':
|
||||||
for var in group_data[key]:
|
for var in group_data[key]:
|
||||||
self.inventory.set_variable(group, var, group_data[key][var])
|
self.inventory.set_variable(group, var, group_data[key][var])
|
||||||
elif key == 'children':
|
elif key == 'children':
|
||||||
|
|
1
test/integration/targets/inventory_yaml/aliases
Normal file
1
test/integration/targets/inventory_yaml/aliases
Normal file
|
@ -0,0 +1 @@
|
||||||
|
shippable/posix/group1
|
10
test/integration/targets/inventory_yaml/empty.json
Normal file
10
test/integration/targets/inventory_yaml/empty.json
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
{
|
||||||
|
"_meta": {
|
||||||
|
"hostvars": {}
|
||||||
|
},
|
||||||
|
"all": {
|
||||||
|
"children": [
|
||||||
|
"ungrouped"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
4
test/integration/targets/inventory_yaml/runme.sh
Executable file
4
test/integration/targets/inventory_yaml/runme.sh
Executable file
|
@ -0,0 +1,4 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
# handle empty/commented out group keys correctly https://github.com/ansible/ansible/issues/47254
|
||||||
|
ANSIBLE_VERBOSITY=0 diff -w <(ansible-inventory -i ./test.yml --list) success.json
|
61
test/integration/targets/inventory_yaml/success.json
Normal file
61
test/integration/targets/inventory_yaml/success.json
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
{
|
||||||
|
"_meta": {
|
||||||
|
"hostvars": {
|
||||||
|
"alice": {
|
||||||
|
"status": "single"
|
||||||
|
},
|
||||||
|
"bobby": {
|
||||||
|
"in_trouble": true,
|
||||||
|
"popular": false
|
||||||
|
},
|
||||||
|
"cindy": {
|
||||||
|
"in_trouble": true,
|
||||||
|
"popular": true
|
||||||
|
},
|
||||||
|
"greg": {
|
||||||
|
"in_trouble": true,
|
||||||
|
"popular": true
|
||||||
|
},
|
||||||
|
"jan": {
|
||||||
|
"in_trouble": true,
|
||||||
|
"popular": false
|
||||||
|
},
|
||||||
|
"marcia": {
|
||||||
|
"in_trouble": true,
|
||||||
|
"popular": true
|
||||||
|
},
|
||||||
|
"peter": {
|
||||||
|
"in_trouble": true,
|
||||||
|
"popular": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"all": {
|
||||||
|
"children": [
|
||||||
|
"cousins",
|
||||||
|
"kids",
|
||||||
|
"the-maid",
|
||||||
|
"ungrouped"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"cousins": {
|
||||||
|
"children": [
|
||||||
|
"redheads"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"kids": {
|
||||||
|
"hosts": [
|
||||||
|
"bobby",
|
||||||
|
"cindy",
|
||||||
|
"greg",
|
||||||
|
"jan",
|
||||||
|
"marcia",
|
||||||
|
"peter"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"the-maid": {
|
||||||
|
"hosts": [
|
||||||
|
"alice"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
27
test/integration/targets/inventory_yaml/test.yml
Normal file
27
test/integration/targets/inventory_yaml/test.yml
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
all:
|
||||||
|
children:
|
||||||
|
kids:
|
||||||
|
hosts:
|
||||||
|
marcia:
|
||||||
|
popular: True
|
||||||
|
jan:
|
||||||
|
popular: False
|
||||||
|
cindy:
|
||||||
|
popular: True
|
||||||
|
greg:
|
||||||
|
popular: True
|
||||||
|
peter:
|
||||||
|
popular: False
|
||||||
|
bobby:
|
||||||
|
popular: False
|
||||||
|
vars:
|
||||||
|
in_trouble: True
|
||||||
|
cousins:
|
||||||
|
children:
|
||||||
|
redheads:
|
||||||
|
hosts:
|
||||||
|
#oliver: # this used to cause an error and deliver incomplete inventory
|
||||||
|
the-maid:
|
||||||
|
hosts:
|
||||||
|
alice:
|
||||||
|
status: single
|
Loading…
Reference in a new issue