community.general/lib/ansible/inventory/host.py

65 lines
1.9 KiB
Python
Raw Normal View History

2012-05-05 20:37:28 +00:00
# (c) 2012, Michael DeHaan <michael.dehaan@gmail.com>
#
# 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/>.
import ansible.constants as C
class Host(object):
''' a single ansible host '''
2012-05-05 20:37:28 +00:00
__slots__ = [ 'name', 'vars', 'groups' ]
2012-05-05 20:37:28 +00:00
def __init__(self, name=None, port=None):
2012-05-05 20:37:28 +00:00
self.name = name
self.vars = {}
self.groups = []
if port and port != C.DEFAULT_REMOTE_PORT:
self.set_variable('ansible_ssh_port', int(port))
if self.name is None:
raise Exception("host name is required")
def add_group(self, group):
2012-05-05 20:37:28 +00:00
self.groups.append(group)
def set_variable(self, key, value):
2012-07-15 13:32:47 +00:00
self.vars[key]=value
2012-05-05 20:37:28 +00:00
def get_groups(self):
2012-05-05 20:37:28 +00:00
groups = {}
for g in self.groups:
groups[g.name] = g
ancestors = g.get_ancestors()
for a in ancestors:
groups[a.name] = a
2012-05-05 20:37:28 +00:00
return groups.values()
def get_variables(self):
2012-05-05 20:37:28 +00:00
results = {}
for group in self.groups:
results.update(group.get_variables())
results.update(self.vars)
results['inventory_hostname'] = self.name
2012-07-30 17:26:48 +00:00
results['inventory_hostname_short'] = self.name.split('.')[0]
2012-05-05 20:37:28 +00:00
groups = self.get_groups()
results['group_names'] = sorted([ g.name for g in groups if g.name != 'all'])
2012-05-05 20:37:28 +00:00
return results