From 07508ad5353f437dba4510b42c86285b7e557bf6 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Mon, 7 May 2012 23:16:20 -0400 Subject: [PATCH] Since host variables are becoming important, it did not make sense to sustain --override-hosts, with the ability to create hosts that didn't have inventory information, but also existed, in various groups. --- bin/ansible-playbook | 6 ---- lib/ansible/group.py | 1 + lib/ansible/inventory.py | 61 +++++++++++++++++++++------------------- lib/ansible/playbook.py | 18 +++--------- 4 files changed, 37 insertions(+), 49 deletions(-) diff --git a/bin/ansible-playbook b/bin/ansible-playbook index b40964c8d55..da22164c753 100755 --- a/bin/ansible-playbook +++ b/bin/ansible-playbook @@ -33,8 +33,6 @@ def main(args): # create parser for CLI options usage = "%prog playbook.yml" parser = utils.base_parser(constants=C, usage=usage, connect_opts=True, runas_opts=True) - parser.add_option('-O', '--override-hosts', dest="override_hosts", default=None, - help="run playbook against these hosts regardless of inventory settings") parser.add_option('-e', '--extra-vars', dest="extra_vars", default=None, help="set additional key=value variables from the CLI") @@ -54,9 +52,6 @@ def main(args): if options.sudo_user: options.sudo = True options.sudo_user = options.sudo_user or C.DEFAULT_SUDO_USER - override_hosts = None - if options.override_hosts: - override_hosts = options.override_hosts.split(",") extra_vars = utils.parse_kv(options.extra_vars) # run all playbooks specified on the command line @@ -70,7 +65,6 @@ def main(args): playbook=playbook, module_path=options.module_path, host_list=options.inventory, - override_hosts=override_hosts, forks=options.forks, debug=options.debug, remote_user=options.remote_user, diff --git a/lib/ansible/group.py b/lib/ansible/group.py index 704c64611da..e8446741816 100644 --- a/lib/ansible/group.py +++ b/lib/ansible/group.py @@ -73,3 +73,4 @@ class Group(object): + diff --git a/lib/ansible/inventory.py b/lib/ansible/inventory.py index 21e11a1b47c..788ae9a2abb 100644 --- a/lib/ansible/inventory.py +++ b/lib/ansible/inventory.py @@ -37,34 +37,33 @@ class Inventory(object): def __init__(self, host_list=C.DEFAULT_HOST_LIST): + # the host file file or script path + if type(host_list) not in [ str, unicode ]: + raise Exception("host list must be a path") self.host_list = host_list + + # the inventory object holds a list of groups self.groups = [] + + # a list of host(names) to contain current inquiries to self._restriction = None + + # whether the inventory file is a script self._is_script = False - if host_list: - if type(host_list) == list: - self.groups = self._groups_from_override_hosts(host_list) - elif os.access(host_list, os.X_OK): - self._is_script = True - self.parser = InventoryScript(filename=host_list) + if os.access(host_list, os.X_OK): + self._is_script = True + self.parser = InventoryScript(filename=host_list) + self.groups = self.parser.groups.values() + else: + data = file(host_list).read() + if not data.startswith("---"): + self.parser = InventoryParser(filename=host_list) self.groups = self.parser.groups.values() - else: - data = file(host_list).read() - if not data.startswith("---"): - self.parser = InventoryParser(filename=host_list) - self.groups = self.parser.groups.values() - else: - self.parser = InventoryParserYaml(filename=host_list) - self.groups = self.parser.groups.values() - - def _groups_from_override_hosts(self, list): - # support for playbook's --override-hosts only - all = Group(name='all') - for h in list: - all.add_host(Host(name=h)) - return dict(all=all) - + else: + self.parser = InventoryParserYaml(filename=host_list) + self.groups = self.parser.groups.values() + def _match(self, str, pattern_str): return fnmatch.fnmatch(str, pattern_str) @@ -73,7 +72,8 @@ class Inventory(object): hosts = {} patterns = pattern.replace(";",":").split(":") - for group in self.get_groups(): + groups = self.get_groups() + for group in groups: for host in group.get_hosts(): for pat in patterns: if group.name == pat or pat == 'all' or self._match(host.name, pat): @@ -131,19 +131,22 @@ class Inventory(object): self.groups.append(group) def list_hosts(self, pattern="all"): - """ DEPRECATED: Get all host names matching the pattern """ return [ h.name for h in self.get_hosts(pattern) ] def list_groups(self): return [ g.name for g in self.groups ] - def restrict_to(self, restriction): + def get_restriction(self): + return self._restriction + + def restrict_to(self, restriction, append_missing=False): """ Restrict list operations to the hosts given in restriction """ - if type(restriction) != list: - restriction = [ restriction ] - self._restriction = restriction + + if type(restriction) != list: + restriction = [ restriction ] + self._restriction = restriction def lift_restriction(self): """ Do not restrict list operations """ - self._restriction = None + self._restriction = None diff --git a/lib/ansible/playbook.py b/lib/ansible/playbook.py index d664880db55..6671acb7ab8 100644 --- a/lib/ansible/playbook.py +++ b/lib/ansible/playbook.py @@ -56,7 +56,6 @@ class PlayBook(object): sudo_pass = C.DEFAULT_SUDO_PASS, remote_port = C.DEFAULT_REMOTE_PORT, transport = C.DEFAULT_TRANSPORT, - override_hosts = None, debug = False, callbacks = None, runner_callbacks = None, @@ -76,7 +75,6 @@ class PlayBook(object): sudo_pass: if sudo==True, and a password is required, this is the sudo password remote_port: default remote port to use if not specified with the host or play transport: how to connect to hosts that don't specify a transport (local, paramiko, etc) - override_hosts: skip the inventory file, just talk to these hosts callbacks output callbacks for the playbook runner_callbacks: more callbacks, this time for the runner API stats: holds aggregrate data about events occuring to each host @@ -99,7 +97,6 @@ class PlayBook(object): self.debug = debug self.callbacks = callbacks self.runner_callbacks = runner_callbacks - self.override_hosts = override_hosts self.stats = stats self.sudo = sudo self.sudo_pass = sudo_pass @@ -107,16 +104,11 @@ class PlayBook(object): self.extra_vars = extra_vars self.global_vars = {} - if override_hosts is not None: - if type(override_hosts) != list: - raise errors.AnsibleError("override hosts must be a list") - if not self.inventory._is_script: - self.global_vars.update(ansible.inventory.Inventory(host_list).get_group_variables('all')) + self.inventory = ansible.inventory.Inventory(host_list) + - else: - self.inventory = ansible.inventory.Inventory(host_list) - if not self.inventory._is_script: - self.global_vars.update(ansible.inventory.Inventory(host_list).get_group_variables('all')) + if not self.inventory._is_script: + self.global_vars.update(self.inventory.get_group_variables('all')) self.basedir = os.path.dirname(playbook) self.playbook = self._parse_playbook(playbook) @@ -504,8 +496,6 @@ class PlayBook(object): name = pg.get('name', pattern) if isinstance(pattern, list): pattern = ';'.join(pattern) - if self.override_hosts: - pattern = 'all' if pattern is None: raise errors.AnsibleError('hosts declaration is required')