| 1 |
# Copyright (c) 2018 Matt Martz <[email protected]> |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
DOCUMENTATION = r""" |
| 7 |
name: toml |
| 8 |
version_added: "2.8" |
| 9 |
short_description: Uses a specific TOML file as an inventory source. |
| 10 |
description: |
| 11 |
- TOML based inventory format |
| 12 |
- File MUST have a valid '.toml' file extension |
| 13 |
""" |
| 14 |
|
| 15 |
EXAMPLES = r"""# fmt: toml |
| 16 |
# Example 1 |
| 17 |
[all.vars] |
| 18 |
has_java = false |
| 19 |
|
| 20 |
[web] |
| 21 |
children = [ |
| 22 |
"apache", |
| 23 |
"nginx" |
| 24 |
] |
| 25 |
vars = { http_port = 8080, myvar = 23 } |
| 26 |
|
| 27 |
[web.hosts] |
| 28 |
host1 = {} |
| 29 |
host2 = { ansible_port = 222 } |
| 30 |
|
| 31 |
[apache.hosts] |
| 32 |
tomcat1 = {} |
| 33 |
tomcat2 = { myvar = 34 } |
| 34 |
tomcat3 = { mysecret = "03#pa33w0rd" } |
| 35 |
|
| 36 |
[nginx.hosts] |
| 37 |
jenkins1 = {} |
| 38 |
|
| 39 |
[nginx.vars] |
| 40 |
has_java = true |
| 41 |
|
| 42 |
# Example 2 |
| 43 |
[all.vars] |
| 44 |
has_java = false |
| 45 |
|
| 46 |
[web] |
| 47 |
children = [ |
| 48 |
"apache", |
| 49 |
"nginx" |
| 50 |
] |
| 51 |
|
| 52 |
[web.vars] |
| 53 |
http_port = 8080 |
| 54 |
myvar = 23 |
| 55 |
|
| 56 |
[web.hosts.host1] |
| 57 |
[web.hosts.host2] |
| 58 |
ansible_port = 222 |
| 59 |
|
| 60 |
[apache.hosts.tomcat1] |
| 61 |
|
| 62 |
[apache.hosts.tomcat2] |
| 63 |
myvar = 34 |
| 64 |
|
| 65 |
[apache.hosts.tomcat3] |
| 66 |
mysecret = "03#pa33w0rd" |
| 67 |
|
| 68 |
[nginx.hosts.jenkins1] |
| 69 |
|
| 70 |
[nginx.vars] |
| 71 |
has_java = true |
| 72 |
|
| 73 |
# Example 3 |
| 74 |
[ungrouped.hosts] |
| 75 |
host1 = {} |
| 76 |
host2 = { ansible_host = "127.0.0.1", ansible_port = 44 } |
| 77 |
host3 = { ansible_host = "127.0.0.1", ansible_port = 45 } |
| 78 |
|
| 79 |
[g1.hosts] |
| 80 |
host4 = {} |
| 81 |
|
| 82 |
[g2.hosts] |
| 83 |
host4 = {} |
| 84 |
""" |
| 85 |
|
| 86 |
import os |
| 87 |
import tomllib |
| 88 |
|
| 89 |
from collections.abc import MutableMapping, MutableSequence |
| 90 |
|
| 91 |
from ansible.errors import AnsibleFileNotFound, AnsibleParserError |
| 92 |
from ansible.plugins.inventory import BaseFileInventoryPlugin |
| 93 |
from ansible.utils.display import Display |
| 94 |
|
| 95 |
display = Display() |
| 96 |
|
| 97 |
|
| 98 |
class InventoryModule(BaseFileInventoryPlugin): |
| 99 |
NAME = 'toml' |
| 100 |
|
| 101 |
trusted_by_default = True # we need the inventory system to mark trust for us, since we're not manually traversing var assignments |
| 102 |
|
| 103 |
def _parse_group(self, group, group_data): |
| 104 |
if group_data is not None and not isinstance(group_data, MutableMapping): |
| 105 |
self.display.warning("Skipping '%s' as this is not a valid group definition" % group) |
| 106 |
return |
| 107 |
|
| 108 |
group = self.inventory.add_group(group) |
| 109 |
if group_data is None: |
| 110 |
return |
| 111 |
|
| 112 |
for key, data in group_data.items(): |
| 113 |
if key == 'vars': |
| 114 |
if not isinstance(data, MutableMapping): |
| 115 |
raise AnsibleParserError( |
| 116 |
'Invalid "vars" entry for "%s" group, requires a dict, found "%s" instead.' % |
| 117 |
(group, type(data)) |
| 118 |
) |
| 119 |
for var, value in data.items(): |
| 120 |
self.inventory.set_variable(group, var, value) |
| 121 |
|
| 122 |
elif key == 'children': |
| 123 |
if not isinstance(data, MutableSequence): |
| 124 |
raise AnsibleParserError( |
| 125 |
'Invalid "children" entry for "%s" group, requires a list, found "%s" instead.' % |
| 126 |
(group, type(data)) |
| 127 |
) |
| 128 |
for subgroup in data: |
| 129 |
self._parse_group(subgroup, {}) |
| 130 |
self.inventory.add_child(group, subgroup) |
| 131 |
|
| 132 |
elif key == 'hosts': |
| 133 |
if not isinstance(data, MutableMapping): |
| 134 |
raise AnsibleParserError( |
| 135 |
'Invalid "hosts" entry for "%s" group, requires a dict, found "%s" instead.' % |
| 136 |
(group, type(data)) |
| 137 |
) |
| 138 |
for host_pattern, value in data.items(): |
| 139 |
hosts, port = self._expand_hostpattern(host_pattern) |
| 140 |
self._populate_host_vars(hosts, value, group, port) |
| 141 |
else: |
| 142 |
self.display.warning( |
| 143 |
'Skipping unexpected key "%s" in group "%s", only "vars", "children" and "hosts" are valid' % |
| 144 |
(key, group) |
| 145 |
) |
| 146 |
|
| 147 |
def _load_file(self, file_name): |
| 148 |
if not file_name or not isinstance(file_name, str): |
| 149 |
raise AnsibleParserError("Invalid filename: '%s'" % file_name) |
| 150 |
|
| 151 |
if not self.loader.path_exists(file_name): |
| 152 |
raise AnsibleFileNotFound("Unable to retrieve file contents", file_name=file_name) |
| 153 |
|
| 154 |
try: |
| 155 |
return tomllib.loads(self.loader.get_text_file_contents(file_name)) |
| 156 |
except tomllib.TOMLDecodeError as ex: |
| 157 |
raise AnsibleParserError(f'TOML file {file_name!r} is invalid.') from ex |
| 158 |
except Exception as ex: |
| 159 |
raise AnsibleParserError(f'An error occurred while parsing the file {file_name!r}.') from ex |
| 160 |
|
| 161 |
def parse(self, inventory, loader, path, cache=True): |
| 162 |
""" parses the inventory file """ |
| 163 |
super(InventoryModule, self).parse(inventory, loader, path) |
| 164 |
self.set_options() |
| 165 |
|
| 166 |
try: |
| 167 |
data = self._load_file(path) |
| 168 |
except Exception as e: |
| 169 |
raise AnsibleParserError(e) |
| 170 |
|
| 171 |
if not data: |
| 172 |
raise AnsibleParserError('Parsed empty TOML file') |
| 173 |
elif data.get('plugin'): |
| 174 |
raise AnsibleParserError('Plugin configuration TOML file, not TOML inventory') |
| 175 |
|
| 176 |
for group_name in data: |
| 177 |
self._parse_group(group_name, data[group_name]) |
| 178 |
|
| 179 |
def verify_file(self, path): |
| 180 |
if super(InventoryModule, self).verify_file(path): |
| 181 |
file_name, ext = os.path.splitext(path) |
| 182 |
if ext == '.toml': |
| 183 |
return True |
| 184 |
return False |