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.
This commit is contained in:
Michael DeHaan 2012-05-07 23:16:20 -04:00
parent 036b779188
commit 07508ad535
4 changed files with 37 additions and 49 deletions

View file

@ -33,8 +33,6 @@ def main(args):
# create parser for CLI options # create parser for CLI options
usage = "%prog playbook.yml" usage = "%prog playbook.yml"
parser = utils.base_parser(constants=C, usage=usage, connect_opts=True, runas_opts=True) 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, parser.add_option('-e', '--extra-vars', dest="extra_vars", default=None,
help="set additional key=value variables from the CLI") help="set additional key=value variables from the CLI")
@ -54,9 +52,6 @@ def main(args):
if options.sudo_user: if options.sudo_user:
options.sudo = True options.sudo = True
options.sudo_user = options.sudo_user or C.DEFAULT_SUDO_USER 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) extra_vars = utils.parse_kv(options.extra_vars)
# run all playbooks specified on the command line # run all playbooks specified on the command line
@ -70,7 +65,6 @@ def main(args):
playbook=playbook, playbook=playbook,
module_path=options.module_path, module_path=options.module_path,
host_list=options.inventory, host_list=options.inventory,
override_hosts=override_hosts,
forks=options.forks, forks=options.forks,
debug=options.debug, debug=options.debug,
remote_user=options.remote_user, remote_user=options.remote_user,

View file

@ -73,3 +73,4 @@ class Group(object):

View file

@ -37,15 +37,21 @@ class Inventory(object):
def __init__(self, host_list=C.DEFAULT_HOST_LIST): 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 self.host_list = host_list
# the inventory object holds a list of groups
self.groups = [] self.groups = []
# a list of host(names) to contain current inquiries to
self._restriction = None self._restriction = None
# whether the inventory file is a script
self._is_script = False self._is_script = False
if host_list: if os.access(host_list, os.X_OK):
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._is_script = True
self.parser = InventoryScript(filename=host_list) self.parser = InventoryScript(filename=host_list)
self.groups = self.parser.groups.values() self.groups = self.parser.groups.values()
@ -58,13 +64,6 @@ class Inventory(object):
self.parser = InventoryParserYaml(filename=host_list) self.parser = InventoryParserYaml(filename=host_list)
self.groups = self.parser.groups.values() 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)
def _match(self, str, pattern_str): def _match(self, str, pattern_str):
return fnmatch.fnmatch(str, pattern_str) return fnmatch.fnmatch(str, pattern_str)
@ -73,7 +72,8 @@ class Inventory(object):
hosts = {} hosts = {}
patterns = pattern.replace(";",":").split(":") 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 host in group.get_hosts():
for pat in patterns: for pat in patterns:
if group.name == pat or pat == 'all' or self._match(host.name, pat): if group.name == pat or pat == 'all' or self._match(host.name, pat):
@ -131,19 +131,22 @@ class Inventory(object):
self.groups.append(group) self.groups.append(group)
def list_hosts(self, pattern="all"): def list_hosts(self, pattern="all"):
""" DEPRECATED: Get all host names matching the pattern """
return [ h.name for h in self.get_hosts(pattern) ] return [ h.name for h in self.get_hosts(pattern) ]
def list_groups(self): def list_groups(self):
return [ g.name for g in self.groups ] 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 """ """ Restrict list operations to the hosts given in restriction """
if type(restriction) != list: if type(restriction) != list:
restriction = [ restriction ] restriction = [ restriction ]
self._restriction = restriction self._restriction = restriction
def lift_restriction(self): def lift_restriction(self):
""" Do not restrict list operations """ """ Do not restrict list operations """
self._restriction = None
self._restriction = None

View file

@ -56,7 +56,6 @@ class PlayBook(object):
sudo_pass = C.DEFAULT_SUDO_PASS, sudo_pass = C.DEFAULT_SUDO_PASS,
remote_port = C.DEFAULT_REMOTE_PORT, remote_port = C.DEFAULT_REMOTE_PORT,
transport = C.DEFAULT_TRANSPORT, transport = C.DEFAULT_TRANSPORT,
override_hosts = None,
debug = False, debug = False,
callbacks = None, callbacks = None,
runner_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 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 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) 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 callbacks output callbacks for the playbook
runner_callbacks: more callbacks, this time for the runner API runner_callbacks: more callbacks, this time for the runner API
stats: holds aggregrate data about events occuring to each host stats: holds aggregrate data about events occuring to each host
@ -99,7 +97,6 @@ class PlayBook(object):
self.debug = debug self.debug = debug
self.callbacks = callbacks self.callbacks = callbacks
self.runner_callbacks = runner_callbacks self.runner_callbacks = runner_callbacks
self.override_hosts = override_hosts
self.stats = stats self.stats = stats
self.sudo = sudo self.sudo = sudo
self.sudo_pass = sudo_pass self.sudo_pass = sudo_pass
@ -107,16 +104,11 @@ class PlayBook(object):
self.extra_vars = extra_vars self.extra_vars = extra_vars
self.global_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'))
else:
self.inventory = ansible.inventory.Inventory(host_list) self.inventory = ansible.inventory.Inventory(host_list)
if not self.inventory._is_script: if not self.inventory._is_script:
self.global_vars.update(ansible.inventory.Inventory(host_list).get_group_variables('all')) self.global_vars.update(self.inventory.get_group_variables('all'))
self.basedir = os.path.dirname(playbook) self.basedir = os.path.dirname(playbook)
self.playbook = self._parse_playbook(playbook) self.playbook = self._parse_playbook(playbook)
@ -504,8 +496,6 @@ class PlayBook(object):
name = pg.get('name', pattern) name = pg.get('name', pattern)
if isinstance(pattern, list): if isinstance(pattern, list):
pattern = ';'.join(pattern) pattern = ';'.join(pattern)
if self.override_hosts:
pattern = 'all'
if pattern is None: if pattern is None:
raise errors.AnsibleError('hosts declaration is required') raise errors.AnsibleError('hosts declaration is required')