/lib/ansible/inventory/__init__.py

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