2014-01-04 18:31:44 +00:00
|
|
|
# (c) 2012-2014, Michael DeHaan <michael.dehaan@gmail.com>
|
2012-05-05 20:37:28 +00:00
|
|
|
#
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# 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
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2015-05-04 02:47:26 +00:00
|
|
|
# Make coding more python3-ish
|
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2012-05-05 20:37:28 +00:00
|
|
|
|
2015-05-04 02:47:26 +00:00
|
|
|
from ansible.inventory.group import Group
|
2017-02-10 20:34:37 +00:00
|
|
|
from ansible.utils.vars import combine_vars, get_unique_id
|
2015-05-04 02:47:26 +00:00
|
|
|
|
|
|
|
__all__ = ['Host']
|
|
|
|
|
2017-06-02 11:14:11 +00:00
|
|
|
|
2015-05-04 02:47:26 +00:00
|
|
|
class Host:
|
2012-07-15 16:29:53 +00:00
|
|
|
''' a single ansible host '''
|
2012-05-05 20:37:28 +00:00
|
|
|
|
2017-06-02 11:14:11 +00:00
|
|
|
# __slots__ = [ 'name', 'vars', 'groups' ]
|
2015-05-04 02:47:26 +00:00
|
|
|
|
|
|
|
def __getstate__(self):
|
|
|
|
return self.serialize()
|
|
|
|
|
|
|
|
def __setstate__(self, data):
|
|
|
|
return self.deserialize(data)
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
2015-09-25 18:53:43 +00:00
|
|
|
if not isinstance(other, Host):
|
|
|
|
return False
|
2015-12-04 18:33:27 +00:00
|
|
|
return self._uuid == other._uuid
|
2015-05-04 02:47:26 +00:00
|
|
|
|
2015-09-24 09:43:33 +00:00
|
|
|
def __ne__(self, other):
|
|
|
|
return not self.__eq__(other)
|
|
|
|
|
2015-09-24 09:46:06 +00:00
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.name)
|
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.get_name()
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.get_name()
|
|
|
|
|
2015-05-04 02:47:26 +00:00
|
|
|
def serialize(self):
|
|
|
|
groups = []
|
|
|
|
for group in self.groups:
|
|
|
|
groups.append(group.serialize())
|
|
|
|
|
|
|
|
return dict(
|
|
|
|
name=self.name,
|
|
|
|
vars=self.vars.copy(),
|
2015-09-17 17:30:05 +00:00
|
|
|
address=self.address,
|
2015-12-04 18:33:27 +00:00
|
|
|
uuid=self._uuid,
|
2015-05-04 02:47:26 +00:00
|
|
|
groups=groups,
|
2016-06-04 23:53:47 +00:00
|
|
|
implicit=self.implicit,
|
2015-05-04 02:47:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def deserialize(self, data):
|
2016-07-31 08:23:28 +00:00
|
|
|
self.__init__(gen_uuid=False)
|
2015-05-04 02:47:26 +00:00
|
|
|
|
2017-06-02 11:14:11 +00:00
|
|
|
self.name = data.get('name')
|
|
|
|
self.vars = data.get('vars', dict())
|
2015-12-17 17:43:36 +00:00
|
|
|
self.address = data.get('address', '')
|
2017-06-02 11:14:11 +00:00
|
|
|
self._uuid = data.get('uuid', None)
|
|
|
|
self.implicit = data.get('implicit', False)
|
2015-05-04 02:47:26 +00:00
|
|
|
|
|
|
|
groups = data.get('groups', [])
|
|
|
|
for group_data in groups:
|
|
|
|
g = Group()
|
|
|
|
g.deserialize(group_data)
|
|
|
|
self.groups.append(g)
|
2012-07-22 15:40:02 +00:00
|
|
|
|
2016-07-31 08:23:28 +00:00
|
|
|
def __init__(self, name=None, port=None, gen_uuid=True):
|
2012-07-15 16:29:53 +00:00
|
|
|
|
2012-05-05 20:37:28 +00:00
|
|
|
self.vars = {}
|
|
|
|
self.groups = []
|
2017-05-23 21:16:49 +00:00
|
|
|
self._uuid = None
|
2015-05-04 02:47:26 +00:00
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
self.name = name
|
2015-09-17 17:30:05 +00:00
|
|
|
self.address = name
|
2015-05-04 02:47:26 +00:00
|
|
|
|
2015-07-31 16:01:32 +00:00
|
|
|
if port:
|
2015-09-17 17:30:05 +00:00
|
|
|
self.set_variable('ansible_port', int(port))
|
2015-05-04 02:47:26 +00:00
|
|
|
|
2016-07-31 08:23:28 +00:00
|
|
|
if gen_uuid:
|
2017-02-10 20:34:37 +00:00
|
|
|
self._uuid = get_unique_id()
|
2016-06-04 23:53:47 +00:00
|
|
|
self.implicit = False
|
2012-05-05 20:37:28 +00:00
|
|
|
|
2015-05-04 02:47:26 +00:00
|
|
|
def get_name(self):
|
|
|
|
return self.name
|
|
|
|
|
2018-03-05 20:18:34 +00:00
|
|
|
def populate_ancestors(self, additions=None):
|
2017-03-15 15:52:55 +00:00
|
|
|
# populate ancestors
|
2018-03-05 20:18:34 +00:00
|
|
|
if additions is None:
|
|
|
|
for group in self.groups:
|
|
|
|
self.add_group(group)
|
|
|
|
else:
|
|
|
|
for group in additions:
|
|
|
|
if group not in self.groups:
|
|
|
|
self.groups.append(group)
|
2017-03-15 15:52:55 +00:00
|
|
|
|
2012-05-05 20:37:28 +00:00
|
|
|
def add_group(self, group):
|
2012-07-15 16:29:53 +00:00
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
# populate ancestors first
|
2017-03-15 15:52:55 +00:00
|
|
|
for oldg in group.get_ancestors():
|
|
|
|
if oldg not in self.groups:
|
2018-03-05 20:18:34 +00:00
|
|
|
self.groups.append(oldg)
|
2017-03-15 15:52:55 +00:00
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
# actually add group
|
2017-03-15 15:52:55 +00:00
|
|
|
if group not in self.groups:
|
|
|
|
self.groups.append(group)
|
2012-05-05 20:37:28 +00:00
|
|
|
|
2017-03-07 16:57:20 +00:00
|
|
|
def remove_group(self, group):
|
|
|
|
|
2017-03-15 15:52:55 +00:00
|
|
|
if group in self.groups:
|
|
|
|
self.groups.remove(group)
|
|
|
|
|
|
|
|
# remove exclusive ancestors, xcept all!
|
|
|
|
for oldg in group.get_ancestors():
|
|
|
|
if oldg.name != 'all':
|
|
|
|
for childg in self.groups:
|
|
|
|
if oldg in childg.get_ancestors():
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
self.remove_group(oldg)
|
2017-03-07 16:57:20 +00:00
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
def set_variable(self, key, value):
|
2017-06-02 11:14:11 +00:00
|
|
|
self.vars[key] = value
|
2012-05-05 20:37:28 +00:00
|
|
|
|
|
|
|
def get_groups(self):
|
2017-03-15 15:52:55 +00:00
|
|
|
return self.groups
|
2012-05-05 20:37:28 +00:00
|
|
|
|
2017-05-23 21:16:49 +00:00
|
|
|
def get_magic_vars(self):
|
2012-05-05 20:37:28 +00:00
|
|
|
results = {}
|
|
|
|
results['inventory_hostname'] = self.name
|
2012-07-30 17:26:48 +00:00
|
|
|
results['inventory_hostname_short'] = self.name.split('.')[0]
|
2017-06-02 11:14:11 +00:00
|
|
|
results['group_names'] = sorted([g.name for g in self.get_groups() if g.name != 'all'])
|
2012-10-26 23:55:59 +00:00
|
|
|
|
2017-10-06 23:11:00 +00:00
|
|
|
return results
|
2017-05-23 21:16:49 +00:00
|
|
|
|
|
|
|
def get_vars(self):
|
|
|
|
return combine_vars(self.vars, self.get_magic_vars())
|