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

137 lines
4.9 KiB
Python
Raw Normal View History

# (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/>.
#############################################
2012-05-25 23:34:13 +00:00
import ansible.constants as C
from ansible.inventory.host import Host
from ansible.inventory.group import Group
from ansible import errors
from ansible import utils
class InventoryParserYaml(object):
"""
Host inventory for ansible.
"""
def __init__(self, filename=C.DEFAULT_HOST_LIST):
fh = open(filename)
data = fh.read()
fh.close()
self._hosts = {}
self._parse(data)
def _make_host(self, hostname):
if hostname in self._hosts:
return self._hosts[hostname]
else:
host = Host(hostname)
self._hosts[hostname] = host
return host
# see file 'test/yaml_hosts' for syntax
def _parse(self, data):
all = Group('all')
ungrouped = Group('ungrouped')
all.add_child_group(ungrouped)
self.groups = dict(all=all, ungrouped=ungrouped)
grouped_hosts = []
yaml = utils.parse_yaml(data)
# first add all groups
for item in yaml:
if type(item) == dict and 'group' in item:
group = Group(item['group'])
for subresult in item.get('hosts',[]):
if type(subresult) in [ str, unicode ]:
host = self._make_host(subresult)
group.add_host(host)
grouped_hosts.append(host)
elif type(subresult) == dict:
host = self._make_host(subresult['host'])
vars = subresult.get('vars',{})
if type(vars) == list:
for subitem in vars:
2012-07-15 13:32:47 +00:00
for (k,v) in subitem.items():
host.set_variable(k,v)
elif type(vars) == dict:
for (k,v) in subresult.get('vars',{}).items():
host.set_variable(k,v)
else:
raise errors.AnsibleError("unexpected type for variable")
group.add_host(host)
grouped_hosts.append(host)
vars = item.get('vars',{})
if type(vars) == dict:
for (k,v) in item.get('vars',{}).items():
group.set_variable(k,v)
elif type(vars) == list:
for subitem in vars:
if type(subitem) != dict:
raise errors.AnsibleError("expected a dictionary")
for (k,v) in subitem.items():
group.set_variable(k,v)
self.groups[group.name] = group
all.add_child_group(group)
# add host definitions
for item in yaml:
if type(item) in [ str, unicode ]:
host = self._make_host(item)
if host not in grouped_hosts:
ungrouped.add_host(host)
elif type(item) == dict and 'host' in item:
host = self._make_host(item['host'])
Squashed commit of the following: commit 4430ce3eefcdff0b0ceffea0ef66ea8e876a807d Merge: 631783b 649963c Author: Michael DeHaan <michael.dehaan@gmail.com> Date: Thu Jul 12 01:28:43 2012 -0400 Merge branch 'host-groups' of https://github.com/dagwieers/ansible into daggroups commit 649963ca2c2610b97a90d2449132a57ae6b39ec9 Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 23:01:00 2012 +0200 Added comments in the example yaml file as requested commit 7f9718f185ec991bc165c4a52b2468cf41f4c349 Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 22:49:38 2012 +0200 Add the default nose color too, to test specific overrides commit eb63b9e899318ce0c26902ca73af50135a6224e4 Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 22:44:35 2012 +0200 Introduce comics and cartoons to test yaml groups defined on a per-node basis commit aa13d233078b825a8057bebf35ed478342cf4e43 Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 19:33:15 2012 +0200 A small fix to revert to old state commit 264ebaa77c4243f2e9117e8d1168dc2f2eed7ee2 Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 19:31:51 2012 +0200 Combine both yaml unit tests into one example file commit 7db49a8048e78402c4c9a0a6cb2604689280fbbb Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 16:46:53 2012 +0200 Might as well fix this too commit f36c6c8c5b419865939c7e2b0b26f6c97255fdc8 Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 16:42:00 2012 +0200 Added unit tests for host-groups patch For the unit test I chose to keep the original yaml file in place as a reference. This patch also includes a fix. commit a96f6813522c5ae8b2be4514a2de56a775c6b7b0 Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 12:30:43 2012 +0200 Allow groups to be defined on a per-host basis This makes it possible to define on a per-host basis what groups a host is in. When managing a large set of systems it makes it easier to ensure each of the systems is defined in a set of groups (e.g. production/qa/development, linux/solaris/aix) rather than having to add systems to multiple disconnected groups. ---- - host: system01 - host: system02 - host: system03 - group: linux hosts: - system01 - system02 - group: solaris hosts: - system03 - group: production hosts: - system01 - system03 - group: qa - system02 - group: dbserver hosts: - system01 - group: ntpserver hosts: - system02 - group: webserver - system03 ---- Can be redefined as: ---- - host: system01 groups: [ linux, production, dbserver ] - host: system02 groups: [ linux, qa, ntpserver ] - host: system03 groups: [ solaris, production, webserver ] ----
2012-07-12 05:29:51 +00:00
vars = item.get('vars', {})
if type(vars)==list:
varlist, vars = vars, {}
for subitem in varlist:
vars.update(subitem)
for (k,v) in vars.items():
2012-07-15 13:32:47 +00:00
host.set_variable(k,v)
Squashed commit of the following: commit 4430ce3eefcdff0b0ceffea0ef66ea8e876a807d Merge: 631783b 649963c Author: Michael DeHaan <michael.dehaan@gmail.com> Date: Thu Jul 12 01:28:43 2012 -0400 Merge branch 'host-groups' of https://github.com/dagwieers/ansible into daggroups commit 649963ca2c2610b97a90d2449132a57ae6b39ec9 Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 23:01:00 2012 +0200 Added comments in the example yaml file as requested commit 7f9718f185ec991bc165c4a52b2468cf41f4c349 Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 22:49:38 2012 +0200 Add the default nose color too, to test specific overrides commit eb63b9e899318ce0c26902ca73af50135a6224e4 Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 22:44:35 2012 +0200 Introduce comics and cartoons to test yaml groups defined on a per-node basis commit aa13d233078b825a8057bebf35ed478342cf4e43 Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 19:33:15 2012 +0200 A small fix to revert to old state commit 264ebaa77c4243f2e9117e8d1168dc2f2eed7ee2 Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 19:31:51 2012 +0200 Combine both yaml unit tests into one example file commit 7db49a8048e78402c4c9a0a6cb2604689280fbbb Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 16:46:53 2012 +0200 Might as well fix this too commit f36c6c8c5b419865939c7e2b0b26f6c97255fdc8 Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 16:42:00 2012 +0200 Added unit tests for host-groups patch For the unit test I chose to keep the original yaml file in place as a reference. This patch also includes a fix. commit a96f6813522c5ae8b2be4514a2de56a775c6b7b0 Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 12:30:43 2012 +0200 Allow groups to be defined on a per-host basis This makes it possible to define on a per-host basis what groups a host is in. When managing a large set of systems it makes it easier to ensure each of the systems is defined in a set of groups (e.g. production/qa/development, linux/solaris/aix) rather than having to add systems to multiple disconnected groups. ---- - host: system01 - host: system02 - host: system03 - group: linux hosts: - system01 - system02 - group: solaris hosts: - system03 - group: production hosts: - system01 - system03 - group: qa - system02 - group: dbserver hosts: - system01 - group: ntpserver hosts: - system02 - group: webserver - system03 ---- Can be redefined as: ---- - host: system01 groups: [ linux, production, dbserver ] - host: system02 groups: [ linux, qa, ntpserver ] - host: system03 groups: [ solaris, production, webserver ] ----
2012-07-12 05:29:51 +00:00
groups = item.get('groups', {})
if type(groups) in [ str, unicode ]:
groups = [ groups ]
if type(groups)==list:
for subitem in groups:
if subitem in self.groups:
group = self.groups[subitem]
else:
group = Group(subitem)
self.groups[group.name] = group
all.add_child_group(group)
group.add_host(host)
grouped_hosts.append(host)
if host not in grouped_hosts:
ungrouped.add_host(host)
Squashed commit of the following: commit 4430ce3eefcdff0b0ceffea0ef66ea8e876a807d Merge: 631783b 649963c Author: Michael DeHaan <michael.dehaan@gmail.com> Date: Thu Jul 12 01:28:43 2012 -0400 Merge branch 'host-groups' of https://github.com/dagwieers/ansible into daggroups commit 649963ca2c2610b97a90d2449132a57ae6b39ec9 Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 23:01:00 2012 +0200 Added comments in the example yaml file as requested commit 7f9718f185ec991bc165c4a52b2468cf41f4c349 Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 22:49:38 2012 +0200 Add the default nose color too, to test specific overrides commit eb63b9e899318ce0c26902ca73af50135a6224e4 Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 22:44:35 2012 +0200 Introduce comics and cartoons to test yaml groups defined on a per-node basis commit aa13d233078b825a8057bebf35ed478342cf4e43 Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 19:33:15 2012 +0200 A small fix to revert to old state commit 264ebaa77c4243f2e9117e8d1168dc2f2eed7ee2 Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 19:31:51 2012 +0200 Combine both yaml unit tests into one example file commit 7db49a8048e78402c4c9a0a6cb2604689280fbbb Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 16:46:53 2012 +0200 Might as well fix this too commit f36c6c8c5b419865939c7e2b0b26f6c97255fdc8 Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 16:42:00 2012 +0200 Added unit tests for host-groups patch For the unit test I chose to keep the original yaml file in place as a reference. This patch also includes a fix. commit a96f6813522c5ae8b2be4514a2de56a775c6b7b0 Author: Dag Wieërs <dag@wieers.com> Date: Thu Jul 12 12:30:43 2012 +0200 Allow groups to be defined on a per-host basis This makes it possible to define on a per-host basis what groups a host is in. When managing a large set of systems it makes it easier to ensure each of the systems is defined in a set of groups (e.g. production/qa/development, linux/solaris/aix) rather than having to add systems to multiple disconnected groups. ---- - host: system01 - host: system02 - host: system03 - group: linux hosts: - system01 - system02 - group: solaris hosts: - system03 - group: production hosts: - system01 - system03 - group: qa - system02 - group: dbserver hosts: - system01 - group: ntpserver hosts: - system02 - group: webserver - system03 ---- Can be redefined as: ---- - host: system01 groups: [ linux, production, dbserver ] - host: system02 groups: [ linux, qa, ntpserver ] - host: system03 groups: [ solaris, production, webserver ] ----
2012-07-12 05:29:51 +00:00
# make sure ungrouped.hosts is the complement of grouped_hosts
ungrouped_hosts = [host for host in ungrouped.hosts if host not in grouped_hosts]