Skip to content

Commit 918a4fe

Browse files
committed
fix(schema): Make inventory JSON Schema match actually accepted inventories
1 parent b294d5b commit 918a4fe

File tree

2 files changed

+136
-33
lines changed

2 files changed

+136
-33
lines changed
Lines changed: 120 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,154 @@
11
{
22
"$defs": {
33
"group": {
4+
"additionalProperties": false,
5+
"description": "See https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#inventory-basics-formats-hosts-and-groups",
46
"properties": {
57
"children": {
8+
"additionalProperties": false,
9+
"description": "See https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#grouping-groups-parent-child-group-relationships",
10+
"pattern": "^[a-zA-Z_]\\w*$",
611
"patternProperties": {
7-
"[a-zA-Z-_0-9]": {
12+
"^[a-zA-Z_]\\w*$": {
813
"$ref": "#/$defs/group"
914
}
10-
}
15+
},
16+
"title": "Children",
17+
"type": ["object", "string", "null"]
1118
},
1219
"hosts": {
20+
"additionalProperties": false,
21+
"description": "See https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#inventory-basics-formats-hosts-and-groups",
22+
"examples": [
23+
{
24+
"foo.example.com": null
25+
},
26+
"foo.example.com",
27+
{
28+
"www[01:50].example.com": null
29+
},
30+
{
31+
"www[01:50:2].example.com": null
32+
}
33+
],
34+
"minLength": 1,
1335
"patternProperties": {
14-
"[a-zA-Z.-_0-9]": {
36+
".": {
37+
"additionalProperties": false,
38+
"description": "See https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#inventory-basics-formats-hosts-and-groups",
39+
"patternProperties": {
40+
"^[a-zA-Z_]\\w*$": {
41+
"description": "See https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#assigning-a-variable-to-one-machine-host-variables",
42+
"title": "Host Variable"
43+
}
44+
},
45+
"title": "Host",
1546
"type": ["object", "null"]
1647
}
1748
},
18-
"type": ["object", "string"]
49+
"title": "Hosts",
50+
"type": ["object", "string", "null"]
1951
},
2052
"vars": {
21-
"type": "object"
53+
"additionalProperties": false,
54+
"description": "See https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#assigning-a-variable-to-many-machines-group-variables",
55+
"examples": [
56+
{
57+
"ntp_server": "ntp.atlanta.example.com",
58+
"proxy": "proxy.atlanta.example.com"
59+
}
60+
],
61+
"pattern": "^[a-zA-Z_]\\w*$",
62+
"patternProperties": {
63+
"^[a-zA-Z_]\\w*$": {
64+
"description": "See https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#assigning-a-variable-to-many-machines-group-variables",
65+
"title": "Group Variable"
66+
}
67+
},
68+
"title": "Group Variables",
69+
"type": ["object", "string", "null"]
2270
}
2371
},
72+
"title": "Group",
2473
"type": ["object", "null"]
25-
},
26-
"special-group": {
27-
"additionalProperties": false,
28-
"properties": {
29-
"children": {
30-
"type": ["object", "null"]
31-
},
32-
"groups": {
33-
"type": ["object", "null"]
34-
},
35-
"hosts": {
36-
"type": ["object", "null"]
37-
},
38-
"vars": {
39-
"type": ["object", "null"]
40-
}
41-
},
42-
"type": "object"
4374
}
4475
},
4576
"$id": "https://raw.githubusercontent.com/ansible/ansible-lint/main/src/ansiblelint/schemas/inventory.json",
4677
"$schema": "http://json-schema.org/draft-07/schema",
47-
"additionalProperties": true,
78+
"additionalProperties": false,
4879
"description": "See https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html",
4980
"examples": [
50-
"inventory.yaml",
51-
"inventory.yml",
52-
"inventory/*.yml",
53-
"inventory/*.yaml"
81+
{
82+
"dbservers": {
83+
"hosts": {
84+
"one.example.com": null,
85+
"three.example.com": null,
86+
"two.example.com": null
87+
}
88+
},
89+
"ungrouped": {
90+
"hosts": {
91+
"mail.example.com": null
92+
}
93+
},
94+
"webservers": {
95+
"hosts": {
96+
"bar.example.com": null,
97+
"foo.example.com": null
98+
}
99+
}
100+
},
101+
{
102+
"atlanta": {
103+
"hosts": {
104+
"host1": {
105+
"http_port": 80,
106+
"maxRequestsPerChild": 808
107+
},
108+
"host2": {
109+
"http_port": 303,
110+
"maxRequestsPerChild": 909
111+
}
112+
}
113+
}
114+
},
115+
{
116+
"webservers": {
117+
"hosts": {
118+
"www[01:50].example.com": null
119+
}
120+
}
121+
},
122+
{
123+
"webservers": {
124+
"hosts": {
125+
"www[01:50:2].example.com": null
126+
}
127+
}
128+
}
54129
],
55130
"markdownDescription": "All keys at top levels are groups with `all` and `ungrouped` having a special meaning.\n\nSee [How to build your inventory](https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html)",
131+
"patternProperties": {
132+
"^[a-zA-Z_]\\w*$": {
133+
"$ref": "#/$defs/group"
134+
}
135+
},
56136
"properties": {
57137
"all": {
58-
"$ref": "#/$defs/special-group"
138+
"$ref": "#/$defs/group",
139+
"description": "See https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#default-groups"
59140
},
60141
"ungrouped": {
61-
"$ref": "#/$defs/group"
142+
"$ref": "#/$defs/group",
143+
"description": "See https://docs.ansible.com/ansible/latest/inventory_guide/intro_inventory.html#default-groups"
62144
}
63145
},
64-
"title": "Ansible Inventory Schema",
65-
"type": "object"
146+
"title": "Ansible Inventory",
147+
"type": "object",
148+
"x-ansible-lint": [
149+
"inventory.yaml",
150+
"inventory.yml",
151+
"inventory/*.yml",
152+
"inventory/*.yaml"
153+
]
66154
}

test/schemas/negative_test/inventory/broken_dev_inventory.yml.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,16 @@
99
"params": {
1010
"additionalProperty": "foo"
1111
},
12-
"schemaPath": "#/$defs/special-group/additionalProperties"
12+
"schemaPath": "#/additionalProperties"
13+
},
14+
{
15+
"instancePath": "/all",
16+
"keyword": "additionalProperties",
17+
"message": "must NOT have additional properties",
18+
"params": {
19+
"additionalProperty": "foo"
20+
},
21+
"schemaPath": "#/additionalProperties"
1322
}
1423
]
1524
```
@@ -23,6 +32,12 @@ stdout:
2332
"status": "fail",
2433
"successes": [],
2534
"errors": [
35+
{
36+
"filename": "negative_test/inventory/broken_dev_inventory.yml",
37+
"path": "$.all",
38+
"message": "Additional properties are not allowed ('foo' was unexpected)",
39+
"has_sub_errors": false
40+
},
2641
{
2742
"filename": "negative_test/inventory/broken_dev_inventory.yml",
2843
"path": "$.all",

0 commit comments

Comments
 (0)