PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/v2/ansible/inventory/__init__.py

https://gitlab.com/18runt88/ansible
Python | 672 lines | 625 code | 15 blank | 32 comment | 19 complexity | 54ff9ce9a5299b1ecce953e0365b5134 MD5 | raw file
  1. # (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
  2. #
  3. # This file is part of Ansible
  4. #
  5. # Ansible is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # Ansible is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
  17. #############################################
  18. from __future__ import (absolute_import, division, print_function)
  19. __metaclass__ = type
  20. import fnmatch
  21. import os
  22. import sys
  23. import re
  24. import stat
  25. import subprocess
  26. from ansible import constants as C
  27. from ansible.errors import *
  28. from ansible.inventory.ini import InventoryParser
  29. from ansible.inventory.script import InventoryScript
  30. from ansible.inventory.dir import InventoryDirectory
  31. from ansible.inventory.group import Group
  32. from ansible.inventory.host import Host
  33. from ansible.plugins import vars_loader
  34. from ansible.utils.path import is_executable
  35. from ansible.utils.vars import combine_vars
  36. class Inventory(object):
  37. """
  38. Host inventory for ansible.
  39. """
  40. #__slots__ = [ 'host_list', 'groups', '_restriction', '_also_restriction', '_subset',
  41. # 'parser', '_vars_per_host', '_vars_per_group', '_hosts_cache', '_groups_list',
  42. # '_pattern_cache', '_vault_password', '_vars_plugins', '_playbook_basedir']
  43. def __init__(self, loader, variable_manager, host_list=C.DEFAULT_HOST_LIST):
  44. # the host file file, or script path, or list of hosts
  45. # if a list, inventory data will NOT be loaded
  46. self.host_list = host_list
  47. self._loader = loader
  48. self._variable_manager = variable_manager
  49. # caching to avoid repeated calculations, particularly with
  50. # external inventory scripts.
  51. self._vars_per_host = {}
  52. self._vars_per_group = {}
  53. self._hosts_cache = {}
  54. self._groups_list = {}
  55. self._pattern_cache = {}
  56. # to be set by calling set_playbook_basedir by playbook code
  57. self._playbook_basedir = None
  58. # the inventory object holds a list of groups
  59. self.groups = []
  60. # a list of host(names) to contain current inquiries to
  61. self._restriction = None
  62. self._also_restriction = None
  63. self._subset = None
  64. if isinstance(host_list, basestring):
  65. if "," in host_list:
  66. host_list = host_list.split(",")
  67. host_list = [ h for h in host_list if h and h.strip() ]
  68. if host_list is None:
  69. self.parser = None
  70. elif isinstance(host_list, list):
  71. self.parser = None
  72. all = Group('all')
  73. self.groups = [ all ]
  74. ipv6_re = re.compile('\[([a-f:A-F0-9]*[%[0-z]+]?)\](?::(\d+))?')
  75. for x in host_list:
  76. m = ipv6_re.match(x)
  77. if m:
  78. all.add_host(Host(m.groups()[0], m.groups()[1]))
  79. else:
  80. if ":" in x:
  81. tokens = x.rsplit(":", 1)
  82. # if there is ':' in the address, then this is an ipv6
  83. if ':' in tokens[0]:
  84. all.add_host(Host(x))
  85. else:
  86. all.add_host(Host(tokens[0], tokens[1]))
  87. else:
  88. all.add_host(Host(x))
  89. elif os.path.exists(host_list):
  90. if os.path.isdir(host_list):
  91. # Ensure basedir is inside the directory
  92. self.host_list = os.path.join(self.host_list, "")
  93. self.parser = InventoryDirectory(loader=self._loader, filename=host_list)
  94. self.groups = self.parser.groups.values()
  95. else:
  96. # check to see if the specified file starts with a
  97. # shebang (#!/), so if an error is raised by the parser
  98. # class we can show a more apropos error
  99. shebang_present = False
  100. try:
  101. inv_file = open(host_list)
  102. first_line = inv_file.readlines()[0]
  103. inv_file.close()
  104. if first_line.startswith('#!'):
  105. shebang_present = True
  106. except:
  107. pass
  108. if is_executable(host_list):
  109. try:
  110. self.parser = InventoryScript(loader=self._loader, filename=host_list)
  111. self.groups = self.parser.groups.values()
  112. except:
  113. if not shebang_present:
  114. raise errors.AnsibleError("The file %s is marked as executable, but failed to execute correctly. " % host_list + \
  115. "If this is not supposed to be an executable script, correct this with `chmod -x %s`." % host_list)
  116. else:
  117. raise
  118. else:
  119. try:
  120. self.parser = InventoryParser(filename=host_list)
  121. self.groups = self.parser.groups.values()
  122. except:
  123. if shebang_present:
  124. raise errors.AnsibleError("The file %s looks like it should be an executable inventory script, but is not marked executable. " % host_list + \
  125. "Perhaps you want to correct this with `chmod +x %s`?" % host_list)
  126. else:
  127. raise
  128. vars_loader.add_directory(self.basedir(), with_subdir=True)
  129. else:
  130. raise errors.AnsibleError("Unable to find an inventory file, specify one with -i ?")
  131. self._vars_plugins = [ x for x in vars_loader.all(self) ]
  132. # FIXME: shouldn't be required, since the group/host vars file
  133. # management will be done in VariableManager
  134. # get group vars from group_vars/ files and vars plugins
  135. for group in self.groups:
  136. # FIXME: combine_vars
  137. group.vars = combine_vars(group.vars, self.get_group_variables(group.name))
  138. # get host vars from host_vars/ files and vars plugins
  139. for host in self.get_hosts():
  140. # FIXME: combine_vars
  141. host.vars = combine_vars(host.vars, self.get_host_variables(host.name))
  142. def _match(self, str, pattern_str):
  143. try:
  144. if pattern_str.startswith('~'):
  145. return re.search(pattern_str[1:], str)
  146. else:
  147. return fnmatch.fnmatch(str, pattern_str)
  148. except Exception, e:
  149. raise errors.AnsibleError('invalid host pattern: %s' % pattern_str)
  150. def _match_list(self, items, item_attr, pattern_str):
  151. results = []
  152. try:
  153. if not pattern_str.startswith('~'):
  154. pattern = re.compile(fnmatch.translate(pattern_str))
  155. else:
  156. pattern = re.compile(pattern_str[1:])
  157. except Exception, e:
  158. raise errors.AnsibleError('invalid host pattern: %s' % pattern_str)
  159. for item in items:
  160. if pattern.match(getattr(item, item_attr)):
  161. results.append(item)
  162. return results
  163. def get_hosts(self, pattern="all"):
  164. """
  165. find all host names matching a pattern string, taking into account any inventory restrictions or
  166. applied subsets.
  167. """
  168. # process patterns
  169. if isinstance(pattern, list):
  170. pattern = ';'.join(pattern)
  171. patterns = pattern.replace(";",":").split(":")
  172. hosts = self._get_hosts(patterns)
  173. # exclude hosts not in a subset, if defined
  174. if self._subset:
  175. subset = self._get_hosts(self._subset)
  176. hosts = [ h for h in hosts if h in subset ]
  177. # exclude hosts mentioned in any restriction (ex: failed hosts)
  178. if self._restriction is not None:
  179. hosts = [ h for h in hosts if h in self._restriction ]
  180. if self._also_restriction is not None:
  181. hosts = [ h for h in hosts if h in self._also_restriction ]
  182. return hosts
  183. def _get_hosts(self, patterns):
  184. """
  185. finds hosts that match a list of patterns. Handles negative
  186. matches as well as intersection matches.
  187. """
  188. # Host specifiers should be sorted to ensure consistent behavior
  189. pattern_regular = []
  190. pattern_intersection = []
  191. pattern_exclude = []
  192. for p in patterns:
  193. if p.startswith("!"):
  194. pattern_exclude.append(p)
  195. elif p.startswith("&"):
  196. pattern_intersection.append(p)
  197. elif p:
  198. pattern_regular.append(p)
  199. # if no regular pattern was given, hence only exclude and/or intersection
  200. # make that magically work
  201. if pattern_regular == []:
  202. pattern_regular = ['all']
  203. # when applying the host selectors, run those without the "&" or "!"
  204. # first, then the &s, then the !s.
  205. patterns = pattern_regular + pattern_intersection + pattern_exclude
  206. hosts = []
  207. for p in patterns:
  208. # avoid resolving a pattern that is a plain host
  209. if p in self._hosts_cache:
  210. hosts.append(self.get_host(p))
  211. else:
  212. that = self.__get_hosts(p)
  213. if p.startswith("!"):
  214. hosts = [ h for h in hosts if h not in that ]
  215. elif p.startswith("&"):
  216. hosts = [ h for h in hosts if h in that ]
  217. else:
  218. to_append = [ h for h in that if h.name not in [ y.name for y in hosts ] ]
  219. hosts.extend(to_append)
  220. return hosts
  221. def __get_hosts(self, pattern):
  222. """
  223. finds hosts that positively match a particular pattern. Does not
  224. take into account negative matches.
  225. """
  226. if pattern in self._pattern_cache:
  227. return self._pattern_cache[pattern]
  228. (name, enumeration_details) = self._enumeration_info(pattern)
  229. hpat = self._hosts_in_unenumerated_pattern(name)
  230. result = self._apply_ranges(pattern, hpat)
  231. self._pattern_cache[pattern] = result
  232. return result
  233. def _enumeration_info(self, pattern):
  234. """
  235. returns (pattern, limits) taking a regular pattern and finding out
  236. which parts of it correspond to start/stop offsets. limits is
  237. a tuple of (start, stop) or None
  238. """
  239. # Do not parse regexes for enumeration info
  240. if pattern.startswith('~'):
  241. return (pattern, None)
  242. # The regex used to match on the range, which can be [x] or [x-y].
  243. pattern_re = re.compile("^(.*)\[([-]?[0-9]+)(?:(?:-)([0-9]+))?\](.*)$")
  244. m = pattern_re.match(pattern)
  245. if m:
  246. (target, first, last, rest) = m.groups()
  247. first = int(first)
  248. if last:
  249. if first < 0:
  250. raise errors.AnsibleError("invalid range: negative indices cannot be used as the first item in a range")
  251. last = int(last)
  252. else:
  253. last = first
  254. return (target, (first, last))
  255. else:
  256. return (pattern, None)
  257. def _apply_ranges(self, pat, hosts):
  258. """
  259. given a pattern like foo, that matches hosts, return all of hosts
  260. given a pattern like foo[0:5], where foo matches hosts, return the first 6 hosts
  261. """
  262. # If there are no hosts to select from, just return the
  263. # empty set. This prevents trying to do selections on an empty set.
  264. # issue#6258
  265. if not hosts:
  266. return hosts
  267. (loose_pattern, limits) = self._enumeration_info(pat)
  268. if not limits:
  269. return hosts
  270. (left, right) = limits
  271. if left == '':
  272. left = 0
  273. if right == '':
  274. right = 0
  275. left=int(left)
  276. right=int(right)
  277. try:
  278. if left != right:
  279. return hosts[left:right]
  280. else:
  281. return [ hosts[left] ]
  282. except IndexError:
  283. raise errors.AnsibleError("no hosts matching the pattern '%s' were found" % pat)
  284. def _create_implicit_localhost(self, pattern):
  285. new_host = Host(pattern)
  286. new_host.set_variable("ansible_python_interpreter", sys.executable)
  287. new_host.set_variable("ansible_connection", "local")
  288. new_host.ipv4_address = '127.0.0.1'
  289. ungrouped = self.get_group("ungrouped")
  290. if ungrouped is None:
  291. self.add_group(Group('ungrouped'))
  292. ungrouped = self.get_group('ungrouped')
  293. self.get_group('all').add_child_group(ungrouped)
  294. ungrouped.add_host(new_host)
  295. return new_host
  296. def _hosts_in_unenumerated_pattern(self, pattern):
  297. """ Get all host names matching the pattern """
  298. results = []
  299. hosts = []
  300. hostnames = set()
  301. # ignore any negative checks here, this is handled elsewhere
  302. pattern = pattern.replace("!","").replace("&", "")
  303. def __append_host_to_results(host):
  304. if host not in results and host.name not in hostnames:
  305. hostnames.add(host.name)
  306. results.append(host)
  307. groups = self.get_groups()
  308. for group in groups:
  309. if pattern == 'all':
  310. for host in group.get_hosts():
  311. __append_host_to_results(host)
  312. else:
  313. if self._match(group.name, pattern):
  314. for host in group.get_hosts():
  315. __append_host_to_results(host)
  316. else:
  317. matching_hosts = self._match_list(group.get_hosts(), 'name', pattern)
  318. for host in matching_hosts:
  319. __append_host_to_results(host)
  320. if pattern in ["localhost", "127.0.0.1"] and len(results) == 0:
  321. new_host = self._create_implicit_localhost(pattern)
  322. results.append(new_host)
  323. return results
  324. def clear_pattern_cache(self):
  325. ''' called exclusively by the add_host plugin to allow patterns to be recalculated '''
  326. self._pattern_cache = {}
  327. def groups_for_host(self, host):
  328. if host in self._hosts_cache:
  329. return self._hosts_cache[host].get_groups()
  330. else:
  331. return []
  332. def groups_list(self):
  333. if not self._groups_list:
  334. groups = {}
  335. for g in self.groups:
  336. groups[g.name] = [h.name for h in g.get_hosts()]
  337. ancestors = g.get_ancestors()
  338. for a in ancestors:
  339. if a.name not in groups:
  340. groups[a.name] = [h.name for h in a.get_hosts()]
  341. self._groups_list = groups
  342. return self._groups_list
  343. def get_groups(self):
  344. return self.groups
  345. def get_host(self, hostname):
  346. if hostname not in self._hosts_cache:
  347. self._hosts_cache[hostname] = self._get_host(hostname)
  348. return self._hosts_cache[hostname]
  349. def _get_host(self, hostname):
  350. if hostname in ['localhost','127.0.0.1']:
  351. for host in self.get_group('all').get_hosts():
  352. if host.name in ['localhost', '127.0.0.1']:
  353. return host
  354. return self._create_implicit_localhost(hostname)
  355. else:
  356. for group in self.groups:
  357. for host in group.get_hosts():
  358. if hostname == host.name:
  359. return host
  360. return None
  361. def get_group(self, groupname):
  362. for group in self.groups:
  363. if group.name == groupname:
  364. return group
  365. return None
  366. def get_group_variables(self, groupname, update_cached=False, vault_password=None):
  367. if groupname not in self._vars_per_group or update_cached:
  368. self._vars_per_group[groupname] = self._get_group_variables(groupname, vault_password=vault_password)
  369. return self._vars_per_group[groupname]
  370. def _get_group_variables(self, groupname, vault_password=None):
  371. group = self.get_group(groupname)
  372. if group is None:
  373. raise Exception("group not found: %s" % groupname)
  374. vars = {}
  375. # plugin.get_group_vars retrieves just vars for specific group
  376. vars_results = [ plugin.get_group_vars(group, vault_password=vault_password) for plugin in self._vars_plugins if hasattr(plugin, 'get_group_vars')]
  377. for updated in vars_results:
  378. if updated is not None:
  379. # FIXME: combine_vars
  380. vars = combine_vars(vars, updated)
  381. # Read group_vars/ files
  382. # FIXME: combine_vars
  383. vars = combine_vars(vars, self.get_group_vars(group))
  384. return vars
  385. def get_vars(self, hostname, update_cached=False, vault_password=None):
  386. host = self.get_host(hostname)
  387. if not host:
  388. raise Exception("host not found: %s" % hostname)
  389. return host.get_vars()
  390. def get_host_variables(self, hostname, update_cached=False, vault_password=None):
  391. if hostname not in self._vars_per_host or update_cached:
  392. self._vars_per_host[hostname] = self._get_host_variables(hostname, vault_password=vault_password)
  393. return self._vars_per_host[hostname]
  394. def _get_host_variables(self, hostname, vault_password=None):
  395. host = self.get_host(hostname)
  396. if host is None:
  397. raise errors.AnsibleError("host not found: %s" % hostname)
  398. vars = {}
  399. # plugin.run retrieves all vars (also from groups) for host
  400. vars_results = [ plugin.run(host, vault_password=vault_password) for plugin in self._vars_plugins if hasattr(plugin, 'run')]
  401. for updated in vars_results:
  402. if updated is not None:
  403. # FIXME: combine_vars
  404. vars = combine_vars(vars, updated)
  405. # plugin.get_host_vars retrieves just vars for specific host
  406. vars_results = [ plugin.get_host_vars(host, vault_password=vault_password) for plugin in self._vars_plugins if hasattr(plugin, 'get_host_vars')]
  407. for updated in vars_results:
  408. if updated is not None:
  409. # FIXME: combine_vars
  410. vars = combine_vars(vars, updated)
  411. # still need to check InventoryParser per host vars
  412. # which actually means InventoryScript per host,
  413. # which is not performant
  414. if self.parser is not None:
  415. # FIXME: combine_vars
  416. vars = combine_vars(vars, self.parser.get_host_variables(host))
  417. # Read host_vars/ files
  418. # FIXME: combine_vars
  419. vars = combine_vars(vars, self.get_host_vars(host))
  420. return vars
  421. def add_group(self, group):
  422. if group.name not in self.groups_list():
  423. self.groups.append(group)
  424. self._groups_list = None # invalidate internal cache
  425. else:
  426. raise errors.AnsibleError("group already in inventory: %s" % group.name)
  427. def list_hosts(self, pattern="all"):
  428. """ return a list of hostnames for a pattern """
  429. result = [ h for h in self.get_hosts(pattern) ]
  430. if len(result) == 0 and pattern in ["localhost", "127.0.0.1"]:
  431. result = [pattern]
  432. return result
  433. def list_groups(self):
  434. return sorted([ g.name for g in self.groups ], key=lambda x: x)
  435. def restrict_to_hosts(self, restriction):
  436. """
  437. Restrict list operations to the hosts given in restriction. This is used
  438. to exclude failed hosts in main playbook code, don't use this for other
  439. reasons.
  440. """
  441. if not isinstance(restriction, list):
  442. restriction = [ restriction ]
  443. self._restriction = restriction
  444. def also_restrict_to(self, restriction):
  445. """
  446. Works like restict_to but offers an additional restriction. Playbooks use this
  447. to implement serial behavior.
  448. """
  449. if not isinstance(restriction, list):
  450. restriction = [ restriction ]
  451. self._also_restriction = restriction
  452. def subset(self, subset_pattern):
  453. """
  454. Limits inventory results to a subset of inventory that matches a given
  455. pattern, such as to select a given geographic of numeric slice amongst
  456. a previous 'hosts' selection that only select roles, or vice versa.
  457. Corresponds to --limit parameter to ansible-playbook
  458. """
  459. if subset_pattern is None:
  460. self._subset = None
  461. else:
  462. subset_pattern = subset_pattern.replace(',',':')
  463. subset_pattern = subset_pattern.replace(";",":").split(":")
  464. results = []
  465. # allow Unix style @filename data
  466. for x in subset_pattern:
  467. if x.startswith("@"):
  468. fd = open(x[1:])
  469. results.extend(fd.read().split("\n"))
  470. fd.close()
  471. else:
  472. results.append(x)
  473. self._subset = results
  474. def remove_restriction(self):
  475. """ Do not restrict list operations """
  476. self._restriction = None
  477. def lift_also_restriction(self):
  478. """ Clears the also restriction """
  479. self._also_restriction = None
  480. def is_file(self):
  481. """ did inventory come from a file? """
  482. if not isinstance(self.host_list, basestring):
  483. return False
  484. return os.path.exists(self.host_list)
  485. def basedir(self):
  486. """ if inventory came from a file, what's the directory? """
  487. if not self.is_file():
  488. return None
  489. dname = os.path.dirname(self.host_list)
  490. if dname is None or dname == '' or dname == '.':
  491. cwd = os.getcwd()
  492. return os.path.abspath(cwd)
  493. return os.path.abspath(dname)
  494. def src(self):
  495. """ if inventory came from a file, what's the directory and file name? """
  496. if not self.is_file():
  497. return None
  498. return self.host_list
  499. def playbook_basedir(self):
  500. """ returns the directory of the current playbook """
  501. return self._playbook_basedir
  502. def set_playbook_basedir(self, dir):
  503. """
  504. sets the base directory of the playbook so inventory can use it as a
  505. basedir for host_ and group_vars, and other things.
  506. """
  507. # Only update things if dir is a different playbook basedir
  508. if dir != self._playbook_basedir:
  509. self._playbook_basedir = dir
  510. # get group vars from group_vars/ files
  511. for group in self.groups:
  512. # FIXME: combine_vars
  513. group.vars = combine_vars(group.vars, self.get_group_vars(group, new_pb_basedir=True))
  514. # get host vars from host_vars/ files
  515. for host in self.get_hosts():
  516. # FIXME: combine_vars
  517. host.vars = combine_vars(host.vars, self.get_host_vars(host, new_pb_basedir=True))
  518. # invalidate cache
  519. self._vars_per_host = {}
  520. self._vars_per_group = {}
  521. def get_host_vars(self, host, new_pb_basedir=False):
  522. """ Read host_vars/ files """
  523. return self._get_hostgroup_vars(host=host, group=None, new_pb_basedir=new_pb_basedir)
  524. def get_group_vars(self, group, new_pb_basedir=False):
  525. """ Read group_vars/ files """
  526. return self._get_hostgroup_vars(host=None, group=group, new_pb_basedir=new_pb_basedir)
  527. def _get_hostgroup_vars(self, host=None, group=None, new_pb_basedir=False):
  528. """
  529. Loads variables from group_vars/<groupname> and host_vars/<hostname> in directories parallel
  530. to the inventory base directory or in the same directory as the playbook. Variables in the playbook
  531. dir will win over the inventory dir if files are in both.
  532. """
  533. results = {}
  534. scan_pass = 0
  535. _basedir = self.basedir()
  536. # look in both the inventory base directory and the playbook base directory
  537. # unless we do an update for a new playbook base dir
  538. if not new_pb_basedir:
  539. basedirs = [_basedir, self._playbook_basedir]
  540. else:
  541. basedirs = [self._playbook_basedir]
  542. for basedir in basedirs:
  543. # this can happen from particular API usages, particularly if not run
  544. # from /usr/bin/ansible-playbook
  545. if basedir is None:
  546. continue
  547. scan_pass = scan_pass + 1
  548. # it's not an eror if the directory does not exist, keep moving
  549. if not os.path.exists(basedir):
  550. continue
  551. # save work of second scan if the directories are the same
  552. if _basedir == self._playbook_basedir and scan_pass != 1:
  553. continue
  554. # FIXME: these should go to VariableManager
  555. if group and host is None:
  556. # load vars in dir/group_vars/name_of_group
  557. base_path = os.path.join(basedir, "group_vars/%s" % group.name)
  558. self._variable_manager.add_group_vars_file(base_path, self._loader)
  559. elif host and group is None:
  560. # same for hostvars in dir/host_vars/name_of_host
  561. base_path = os.path.join(basedir, "host_vars/%s" % host.name)
  562. self._variable_manager.add_host_vars_file(base_path, self._loader)
  563. # all done, results is a dictionary of variables for this particular host.
  564. return results