2017-05-23 21:16:49 +00:00
|
|
|
# (c) 2017, Red Hat, inc
|
2012-07-24 19:43:35 +00:00
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
2017-05-23 21:16:49 +00:00
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
2012-07-24 19:43:35 +00:00
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
2017-05-23 21:16:49 +00:00
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
2012-07-24 19:43:35 +00:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
2017-05-23 21:16:49 +00:00
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
2012-07-24 19:43:35 +00:00
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
# Make coding more python3-ish
|
2015-05-04 02:47:26 +00:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
import os
|
|
|
|
import hashlib
|
2012-12-11 02:48:38 +00:00
|
|
|
import string
|
2012-07-24 19:43:35 +00:00
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
from ansible.module_utils._text import to_bytes
|
|
|
|
from ansible.template import Templar
|
|
|
|
|
|
|
|
try:
|
|
|
|
from __main__ import display
|
|
|
|
except ImportError:
|
|
|
|
from ansible.utils.display import Display
|
|
|
|
display = Display()
|
|
|
|
|
|
|
|
|
|
|
|
class BaseInventoryPlugin(object):
|
|
|
|
""" Parses an Inventory Source"""
|
|
|
|
|
|
|
|
TYPE = 'generator'
|
|
|
|
|
|
|
|
def __init__(self, cache=None):
|
|
|
|
|
|
|
|
self.inventory = None
|
|
|
|
self.display = display
|
|
|
|
self.cache = cache
|
|
|
|
|
|
|
|
def parse(self, inventory, loader, path, cache=True):
|
|
|
|
''' Populates self.groups from the given data. Raises an error on any parse failure. '''
|
|
|
|
|
|
|
|
self.loader = loader
|
|
|
|
self.inventory = inventory
|
2017-08-21 20:06:15 +00:00
|
|
|
self.templar = Templar(loader=loader)
|
2017-05-23 21:16:49 +00:00
|
|
|
|
|
|
|
def verify_file(self, path):
|
|
|
|
''' Verify if file is usable by this plugin, base does minimal accessability check '''
|
|
|
|
|
|
|
|
b_path = to_bytes(path)
|
|
|
|
return (os.path.exists(b_path) and os.access(b_path, os.R_OK))
|
|
|
|
|
|
|
|
def get_cache_prefix(self, path):
|
|
|
|
''' create predictable unique prefix for plugin/inventory '''
|
|
|
|
|
|
|
|
m = hashlib.sha1()
|
2017-06-02 17:41:02 +00:00
|
|
|
m.update(to_bytes(self.NAME))
|
2017-05-23 21:16:49 +00:00
|
|
|
d1 = m.hexdigest()
|
2012-07-25 00:16:35 +00:00
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
n = hashlib.sha1()
|
2017-06-02 17:41:02 +00:00
|
|
|
n.update(to_bytes(path))
|
2017-05-23 21:16:49 +00:00
|
|
|
d2 = n.hexdigest()
|
|
|
|
|
|
|
|
return 's_'.join([d1[:5], d2[:5]])
|
|
|
|
|
|
|
|
def clear_cache(self):
|
|
|
|
pass
|
|
|
|
|
2017-06-05 22:25:48 +00:00
|
|
|
def populate_host_vars(self, hosts, variables, group=None, port=None):
|
2017-07-14 03:15:13 +00:00
|
|
|
for host in hosts:
|
|
|
|
self.inventory.add_host(host, group=group, port=port)
|
|
|
|
for k in variables:
|
|
|
|
self.inventory.set_variable(host, k, variables[k])
|
2017-05-23 21:16:49 +00:00
|
|
|
|
|
|
|
def _compose(self, template, variables):
|
|
|
|
''' helper method for pluigns to compose variables for Ansible based on jinja2 expression and inventory vars'''
|
2017-08-21 20:06:15 +00:00
|
|
|
t = self.templar
|
|
|
|
t.set_available_variables(variables)
|
2017-06-02 11:14:11 +00:00
|
|
|
return t.do_template('%s%s%s' % (t.environment.variable_start_string, template, t.environment.variable_end_string), disable_lookups=True)
|
2017-05-23 21:16:49 +00:00
|
|
|
|
2017-08-21 20:06:15 +00:00
|
|
|
def _set_composite_vars(self, compose, variables, host):
|
|
|
|
''' loops over compose entries to create vars for hosts '''
|
|
|
|
if compose and isinstance(compose, dict):
|
|
|
|
for varname in compose:
|
|
|
|
composite = self._compose(compose[varname], variables)
|
|
|
|
self.inventory.set_variable(host, varname, composite)
|
|
|
|
|
|
|
|
def _add_host_to_composed_groups(self, groups, variables, host):
|
|
|
|
''' helper to create complex groups for plugins based on jinaj2 conditionals, hosts that meet the conditional are added to group'''
|
|
|
|
# process each 'group entry'
|
|
|
|
if groups and isinstance(groups, dict):
|
|
|
|
self.templar.set_available_variables(variables)
|
|
|
|
for group_name in groups:
|
|
|
|
conditional = "{%% if %s %%} True {%% else %%} False {%% endif %%}" % groups[group_name]
|
|
|
|
result = self.templar.template(conditional)
|
|
|
|
if result and bool(result):
|
|
|
|
# ensure group exists
|
|
|
|
self.inventory.add_group(group_name)
|
|
|
|
# add host to group
|
|
|
|
self.inventory.add_child(group_name, host)
|
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
|
|
|
|
class BaseFileInventoryPlugin(BaseInventoryPlugin):
|
|
|
|
""" Parses a File based Inventory Source"""
|
|
|
|
|
|
|
|
TYPE = 'storage'
|
|
|
|
|
|
|
|
def __init__(self, cache=None):
|
|
|
|
|
|
|
|
# file based inventories are always local so no need for cache
|
|
|
|
super(BaseFileInventoryPlugin, self).__init__(cache=None)
|
|
|
|
|
|
|
|
|
2017-06-02 11:14:11 +00:00
|
|
|
# Helper methods
|
|
|
|
def detect_range(line=None):
|
2012-07-24 19:43:35 +00:00
|
|
|
'''
|
|
|
|
A helper function that checks a given host line to see if it contains
|
2014-05-03 16:40:05 +00:00
|
|
|
a range pattern described in the docstring above.
|
2012-07-24 19:43:35 +00:00
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
Returns True if the given line contains a pattern, else False.
|
2012-07-24 19:43:35 +00:00
|
|
|
'''
|
2017-05-23 21:16:49 +00:00
|
|
|
return '[' in line
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2017-06-02 11:14:11 +00:00
|
|
|
|
|
|
|
def expand_hostname_range(line=None):
|
2012-07-24 19:43:35 +00:00
|
|
|
'''
|
|
|
|
A helper function that expands a given line that contains a pattern
|
|
|
|
specified in top docstring, and returns a list that consists of the
|
|
|
|
expanded version.
|
|
|
|
|
|
|
|
The '[' and ']' characters are used to maintain the pseudo-code
|
|
|
|
appearance. They are replaced in this function with '|' to ease
|
|
|
|
string splitting.
|
|
|
|
|
|
|
|
References: http://ansible.github.com/patterns.html#hosts-and-groups
|
|
|
|
'''
|
|
|
|
all_hosts = []
|
|
|
|
if line:
|
|
|
|
# A hostname such as db[1:6]-node is considered to consists
|
2012-08-07 00:07:02 +00:00
|
|
|
# three parts:
|
2012-07-24 19:43:35 +00:00
|
|
|
# head: 'db'
|
|
|
|
# nrange: [1:6]; range() is a built-in. Can't use the name
|
|
|
|
# tail: '-node'
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2013-06-06 20:27:30 +00:00
|
|
|
# Add support for multiple ranges in a host so:
|
|
|
|
# db[01:10:3]node-[01:10]
|
|
|
|
# - to do this we split off at the first [...] set, getting the list
|
|
|
|
# of hosts and then repeat until none left.
|
|
|
|
# - also add an optional third parameter which contains the step. (Default: 1)
|
|
|
|
# so range can be [01:10:2] -> 01 03 05 07 09
|
|
|
|
|
2017-06-02 11:14:11 +00:00
|
|
|
(head, nrange, tail) = line.replace('[', '|', 1).replace(']', '|', 1).split('|')
|
2012-07-24 19:43:35 +00:00
|
|
|
bounds = nrange.split(":")
|
2013-06-06 20:27:30 +00:00
|
|
|
if len(bounds) != 2 and len(bounds) != 3:
|
2017-05-23 21:16:49 +00:00
|
|
|
raise AnsibleError("host range must be begin:end or begin:end:step")
|
2012-07-24 19:43:35 +00:00
|
|
|
beg = bounds[0]
|
|
|
|
end = bounds[1]
|
2013-06-06 20:27:30 +00:00
|
|
|
if len(bounds) == 2:
|
|
|
|
step = 1
|
|
|
|
else:
|
|
|
|
step = bounds[2]
|
2012-07-24 19:43:35 +00:00
|
|
|
if not beg:
|
|
|
|
beg = "0"
|
|
|
|
if not end:
|
2017-05-23 21:16:49 +00:00
|
|
|
raise AnsibleError("host range must specify end value")
|
2012-07-24 19:43:35 +00:00
|
|
|
if beg[0] == '0' and len(beg) > 1:
|
2017-06-02 11:14:11 +00:00
|
|
|
rlen = len(beg) # range length formatting hint
|
2012-12-11 02:48:38 +00:00
|
|
|
if rlen != len(end):
|
2017-05-23 21:16:49 +00:00
|
|
|
raise AnsibleError("host range must specify equal-length begin and end formats")
|
2017-06-02 11:14:11 +00:00
|
|
|
|
|
|
|
def fill(x):
|
|
|
|
return str(x).zfill(rlen) # range sequence
|
|
|
|
|
2012-07-24 19:43:35 +00:00
|
|
|
else:
|
2012-12-11 02:48:38 +00:00
|
|
|
fill = str
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-12-11 02:48:38 +00:00
|
|
|
try:
|
|
|
|
i_beg = string.ascii_letters.index(beg)
|
|
|
|
i_end = string.ascii_letters.index(end)
|
|
|
|
if i_beg > i_end:
|
2017-05-23 21:16:49 +00:00
|
|
|
raise AnsibleError("host range must have begin <= end")
|
2017-06-02 11:14:11 +00:00
|
|
|
seq = list(string.ascii_letters[i_beg:i_end + 1:int(step)])
|
2014-05-03 16:40:05 +00:00
|
|
|
except ValueError: # not an alpha range
|
2017-06-02 11:14:11 +00:00
|
|
|
seq = range(int(beg), int(end) + 1, int(step))
|
2012-12-11 02:48:38 +00:00
|
|
|
|
|
|
|
for rseq in seq:
|
|
|
|
hname = ''.join((head, fill(rseq), tail))
|
2013-06-06 20:27:30 +00:00
|
|
|
|
|
|
|
if detect_range(hname):
|
2017-06-02 11:14:11 +00:00
|
|
|
all_hosts.extend(expand_hostname_range(hname))
|
2013-06-06 20:27:30 +00:00
|
|
|
else:
|
|
|
|
all_hosts.append(hname)
|
2012-08-07 00:07:02 +00:00
|
|
|
|
2012-07-24 19:43:35 +00:00
|
|
|
return all_hosts
|