get_option instead of internal dict (#33191)

* get_option instead of internal dict

* fix slack issue

* not a pugin, revert get_option
This commit is contained in:
Brian Coca 2017-11-28 12:00:22 -05:00 committed by GitHub
commit 22d983c5c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 50 additions and 60 deletions

View file

@ -84,6 +84,9 @@ class CallbackBase(AnsiblePlugin):
def set_option(self, k, v): def set_option(self, k, v):
self._plugin_options[k] = v self._plugin_options[k] = v
def get_option(self, k):
return self._plugin_options[k]
def set_options(self, task_keys=None, var_options=None, direct=None): def set_options(self, task_keys=None, var_options=None, direct=None):
''' This is different than the normal plugin method as callbacks get called early and really don't accept keywords. ''' This is different than the normal plugin method as callbacks get called early and really don't accept keywords.
Also _options was already taken for CLI args and callbacks use _plugin_options instead. Also _options was already taken for CLI args and callbacks use _plugin_options instead.

View file

@ -232,39 +232,26 @@ class CallbackModule(CallbackBase):
self.le_jobid = str(uuid.uuid4()) self.le_jobid = str(uuid.uuid4())
# FIXME: remove when done testing
# initialize configurable
self.api_url = 'data.logentries.com'
self.api_port = 80
self.api_tls_port = 443
self.use_tls = False
self.flatten = False
self.token = None
# FIXME: make configurable, move to options # FIXME: make configurable, move to options
self.timeout = 10 self.timeout = 10
# FIXME: remove testing
# self.set_options({'api': 'data.logentries.com', 'port': 80,
# 'tls_port': 10000, 'use_tls': True, 'flatten': False, 'token': 'ae693734-4c5b-4a44-8814-1d2feb5c8241'})
def set_options(self, task_keys=None, var_options=None, direct=None): def set_options(self, task_keys=None, var_options=None, direct=None):
super(CallbackModule, self).set_options(task_keys=task_keys, var_options=var_options, direct=direct) super(CallbackModule, self).set_options(task_keys=task_keys, var_options=var_options, direct=direct)
# get options # get options
try: try:
self.api_url = self._plugin_options['api'] self.api_url = self.get_option('api')
self.api_port = self._plugin_options['port'] self.api_port = self.get_option('port')
self.api_tls_port = self._plugin_options['tls_port'] self.api_tls_port = self.get_option('tls_port')
self.use_tls = self._plugin_options['use_tls'] self.use_tls = self.get_option('use_tls')
self.flatten = self._plugin_options['flatten'] self.flatten = self.get_option('flatten')
except KeyError as e: except KeyError as e:
self._display.warning("Missing option for Logentries callback plugin: %s" % to_native(e)) self._display.warning("Missing option for Logentries callback plugin: %s" % to_native(e))
self.disabled = True self.disabled = True
try: try:
self.token = self._plugin_options['token'] self.token = self.get_option('token')
except KeyError as e: except KeyError as e:
self._display.warning('Logentries token was not provided, this is required for this callback to operate, disabling') self._display.warning('Logentries token was not provided, this is required for this callback to operate, disabling')
self.disabled = True self.disabled = True

View file

@ -123,7 +123,7 @@ class CallbackModule(CallbackBase):
super(CallbackModule, self).set_options(task_keys=task_keys, var_options=var_options, direct=direct) super(CallbackModule, self).set_options(task_keys=task_keys, var_options=var_options, direct=direct)
self.sort_order = self._plugin_options['sort_order'] self.sort_order = self.get_option('sort_order')
if self.sort_order is not None: if self.sort_order is not None:
if self.sort_order == 'ascending': if self.sort_order == 'ascending':
self.sort_order = False self.sort_order = False
@ -132,7 +132,7 @@ class CallbackModule(CallbackBase):
elif self.sort_order == 'none': elif self.sort_order == 'none':
self.sort_order = None self.sort_order = None
self.task_output_limit = self._plugin_options['output_limit'] self.task_output_limit = self.get_option('output_limit')
if self.task_output_limit is not None: if self.task_output_limit is not None:
if self.task_output_limit == 'all': if self.task_output_limit == 'all':
self.task_output_limit = None self.task_output_limit = None

View file

@ -93,7 +93,7 @@ class CallbackModule(CallbackBase):
super(CallbackModule, self).set_options(task_keys=task_keys, var_options=var_options, direct=direct) super(CallbackModule, self).set_options(task_keys=task_keys, var_options=var_options, direct=direct)
global DONT_COLORIZE global DONT_COLORIZE
DONT_COLORIZE = self._plugin_options['nocolor'] DONT_COLORIZE = self.get_option('nocolor')
def _print_task(self, task_name=None): def _print_task(self, task_name=None):
if task_name is None: if task_name is None:

View file

@ -93,9 +93,9 @@ class CallbackModule(CallbackBase):
super(CallbackModule, self).set_options(task_keys=task_keys, var_options=var_options, direct=direct) super(CallbackModule, self).set_options(task_keys=task_keys, var_options=var_options, direct=direct)
self.webhook_url = self._plugin_options['webhook_url'] self.webhook_url = self.get_option('webhook_url')
self.channel = self._plugin_options['channel'] self.channel = self.get_option('channel')
self.username = self._plugin_options['username'] self.username = self.get_option('username')
self.show_invocation = (self._display.verbosity > 1) self.show_invocation = (self._display.verbosity > 1)
if self.webhook_url is None: if self.webhook_url is None:
@ -133,12 +133,12 @@ class CallbackModule(CallbackBase):
] ]
invocation_items = [] invocation_items = []
if self._plugin_options and self.show_invocation: if self._plugin_options and self.show_invocation:
tags = self._plugin_options.tags tags = self.get_option('tags')
skip_tags = self._plugin_options.skip_tags skip_tags = self.get_option('skip_tags')
extra_vars = self._plugin_options.extra_vars extra_vars = self.get_option('extra_vars')
subset = self._plugin_options.subset subset = self.get_option('subset')
inventory = os.path.basename( inventory = os.path.basename(
os.path.realpath(self._plugin_options.inventory) os.path.realpath(self.get_option('inventory'))
) )
invocation_items.append('Inventory: %s' % inventory) invocation_items.append('Inventory: %s' % inventory)
@ -152,7 +152,7 @@ class CallbackModule(CallbackBase):
invocation_items.append('Extra Vars: %s' % invocation_items.append('Extra Vars: %s' %
' '.join(extra_vars)) ' '.join(extra_vars))
title.append('by *%s*' % self._plugin_options.remote_user) title.append('by *%s*' % self.get_options('remote_user'))
title.append('\n\n*%s*' % self.playbook_name) title.append('\n\n*%s*' % self.playbook_name)
msg_items = [' '.join(title)] msg_items = [' '.join(title)]

View file

@ -264,7 +264,7 @@ class Connection(ConnectionBase):
if proxy_command: if proxy_command:
break break
proxy_command = proxy_command or self._options['proxy_command'] proxy_command = proxy_command or self.get_option('proxy_command')
sock_kwarg = {} sock_kwarg = {}
if proxy_command: if proxy_command:
@ -299,7 +299,7 @@ class Connection(ConnectionBase):
self.keyfile = os.path.expanduser("~/.ssh/known_hosts") self.keyfile = os.path.expanduser("~/.ssh/known_hosts")
if self._options['host_key_checking']: if self.get_option('host_key_checking'):
for ssh_known_hosts in ("/etc/ssh/ssh_known_hosts", "/etc/openssh/ssh_known_hosts"): for ssh_known_hosts in ("/etc/ssh/ssh_known_hosts", "/etc/openssh/ssh_known_hosts"):
try: try:
# TODO: check if we need to look at several possible locations, possible for loop # TODO: check if we need to look at several possible locations, possible for loop
@ -327,7 +327,7 @@ class Connection(ConnectionBase):
self._play_context.remote_addr, self._play_context.remote_addr,
username=self._play_context.remote_user, username=self._play_context.remote_user,
allow_agent=allow_agent, allow_agent=allow_agent,
look_for_keys=self._options['look_for_keys'], look_for_keys=self.get_option('look_for_keys'),
key_filename=key_filename, key_filename=key_filename,
password=self._play_context.password, password=self._play_context.password,
timeout=self._play_context.timeout, timeout=self._play_context.timeout,
@ -371,7 +371,7 @@ class Connection(ConnectionBase):
# sudo usually requires a PTY (cf. requiretty option), therefore # sudo usually requires a PTY (cf. requiretty option), therefore
# we give it one by default (pty=True in ansble.cfg), and we try # we give it one by default (pty=True in ansble.cfg), and we try
# to initialise from the calling environment when sudoable is enabled # to initialise from the calling environment when sudoable is enabled
if self._options['pty'] and sudoable: if self.get_option('pty') and sudoable:
chan.get_pty(term=os.getenv('TERM', 'vt100'), width=int(os.getenv('COLUMNS', 0)), height=int(os.getenv('LINES', 0))) chan.get_pty(term=os.getenv('TERM', 'vt100'), width=int(os.getenv('COLUMNS', 0)), height=int(os.getenv('LINES', 0)))
display.vvv("EXEC %s" % cmd, host=self._play_context.remote_addr) display.vvv("EXEC %s" % cmd, host=self._play_context.remote_addr)
@ -524,7 +524,7 @@ class Connection(ConnectionBase):
if self.sftp is not None: if self.sftp is not None:
self.sftp.close() self.sftp.close()
if self._options['host_key_checking'] and self._options['record_host_keys'] and self._any_keys_added(): if self.get_option('host_key_checking') and self.get_option('record_host_keys') and self._any_keys_added():
# add any new SSH host keys -- warning -- this could be slow # add any new SSH host keys -- warning -- this could be slow
# (This doesn't acquire the connection lock because it needs # (This doesn't acquire the connection lock because it needs

View file

@ -176,12 +176,12 @@ class Connection(ConnectionBase):
self._become_user = self._play_context.become_user self._become_user = self._play_context.become_user
self._become_pass = self._play_context.become_pass self._become_pass = self._play_context.become_pass
self._winrm_port = self._options['port'] self._winrm_port = self.get_option('port')
self._winrm_scheme = self._options['scheme'] self._winrm_scheme = self.get_option('scheme')
self._winrm_path = self._options['path'] self._winrm_path = self.get_option('path')
self._kinit_cmd = self._options['kerberos_command'] self._kinit_cmd = self.get_option('kerberos_command')
self._winrm_transport = self._options['transport'] self._winrm_transport = self.get_option('transport')
self._winrm_connection_timeout = self._options['connection_timeout'] self._winrm_connection_timeout = self.get_option('connection_timeout')
if hasattr(winrm, 'FEATURE_SUPPORTED_AUTHTYPES'): if hasattr(winrm, 'FEATURE_SUPPORTED_AUTHTYPES'):
self._winrm_supported_authtypes = set(winrm.FEATURE_SUPPORTED_AUTHTYPES) self._winrm_supported_authtypes = set(winrm.FEATURE_SUPPORTED_AUTHTYPES)
@ -205,7 +205,7 @@ class Connection(ConnectionBase):
raise AnsibleError('The installed version of WinRM does not support transport(s) %s' % list(unsupported_transports)) raise AnsibleError('The installed version of WinRM does not support transport(s) %s' % list(unsupported_transports))
# if kerberos is among our transports and there's a password specified, we're managing the tickets # if kerberos is among our transports and there's a password specified, we're managing the tickets
kinit_mode = self._options['kerberos_mode'] kinit_mode = self.get_option('kerberos_mode')
if kinit_mode is None: if kinit_mode is None:
# HACK: ideally, remove multi-transport stuff # HACK: ideally, remove multi-transport stuff
self._kerb_managed = "kerberos" in self._winrm_transport and self._winrm_pass self._kerb_managed = "kerberos" in self._winrm_transport and self._winrm_pass
@ -221,7 +221,7 @@ class Connection(ConnectionBase):
argspec = inspect.getargspec(Protocol.__init__) argspec = inspect.getargspec(Protocol.__init__)
supported_winrm_args = set(argspec.args) supported_winrm_args = set(argspec.args)
supported_winrm_args.update(internal_kwarg_mask) supported_winrm_args.update(internal_kwarg_mask)
passed_winrm_args = set([v.replace('ansible_winrm_', '') for v in self._options['_extras']]) passed_winrm_args = set([v.replace('ansible_winrm_', '') for v in self.get_option('_extras')])
unsupported_args = passed_winrm_args.difference(supported_winrm_args) unsupported_args = passed_winrm_args.difference(supported_winrm_args)
# warn for kwargs unsupported by the installed version of pywinrm # warn for kwargs unsupported by the installed version of pywinrm
@ -230,7 +230,7 @@ class Connection(ConnectionBase):
# pass through matching extras, excluding the list we want to treat specially # pass through matching extras, excluding the list we want to treat specially
for arg in passed_winrm_args.difference(internal_kwarg_mask).intersection(supported_winrm_args): for arg in passed_winrm_args.difference(internal_kwarg_mask).intersection(supported_winrm_args):
self._winrm_kwargs[arg] = self._options['_extras']['ansible_winrm_%s' % arg] self._winrm_kwargs[arg] = self.get_option('_extras')['ansible_winrm_%s' % arg]
# Until pykerberos has enough goodies to implement a rudimentary kinit/klist, simplest way is to let each connection # Until pykerberos has enough goodies to implement a rudimentary kinit/klist, simplest way is to let each connection
# auth itself with a private CCACHE. # auth itself with a private CCACHE.

View file

@ -91,7 +91,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
self._read_config_data(path) self._read_config_data(path)
strict = self._options['strict'] strict = self.get_option('strict')
fact_cache = FactCache() fact_cache = FactCache()
try: try:
# Go over hosts (less var copies) # Go over hosts (less var copies)
@ -103,7 +103,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
hostvars = combine_vars(hostvars, fact_cache[host]) hostvars = combine_vars(hostvars, fact_cache[host])
# create composite vars # create composite vars
self._set_composite_vars(self._options['compose'], hostvars, host, strict=strict) self._set_composite_vars(self.get_option('compose'), hostvars, host, strict=strict)
# refetch host vars in case new ones have been created above # refetch host vars in case new ones have been created above
hostvars = inventory.hosts[host].get_vars() hostvars = inventory.hosts[host].get_vars()
@ -111,10 +111,10 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
hostvars = combine_vars(hostvars, self._cache[host]) hostvars = combine_vars(hostvars, self._cache[host])
# constructed groups based on conditionals # constructed groups based on conditionals
self._add_host_to_composed_groups(self._options['groups'], hostvars, host, strict=strict) self._add_host_to_composed_groups(self.get_option('groups'), hostvars, host, strict=strict)
# constructed groups based variable values # constructed groups based variable values
self._add_host_to_keyed_groups(self._options['keyed_groups'], hostvars, host, strict=strict) self._add_host_to_keyed_groups(self.get_option('keyed_groups'), hostvars, host, strict=strict)
except Exception as e: except Exception as e:
raise AnsibleParserError("failed to parse %s: %s " % (to_native(path), to_native(e))) raise AnsibleParserError("failed to parse %s: %s " % (to_native(path), to_native(e)))

View file

@ -77,7 +77,7 @@ class InventoryModule(BaseInventoryPlugin, Cacheable):
super(InventoryModule, self).parse(inventory, loader, path) super(InventoryModule, self).parse(inventory, loader, path)
if cache is None: if cache is None:
cache = self._options['cache'] cache = self.get_option('cache')
# Support inventory scripts that are not prefixed with some # Support inventory scripts that are not prefixed with some
# path information but happen to be in the current working # path information but happen to be in the current working

View file

@ -75,21 +75,21 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
# set vars in inventory from hostvars # set vars in inventory from hostvars
for host in hostvars: for host in hostvars:
query = self._options['query'] query = self.get_option('query')
# create vars from vbox properties # create vars from vbox properties
if query and isinstance(query, MutableMapping): if query and isinstance(query, MutableMapping):
for varname in query: for varname in query:
hostvars[host][varname] = self._query_vbox_data(host, query[varname]) hostvars[host][varname] = self._query_vbox_data(host, query[varname])
# create composite vars # create composite vars
self._set_composite_vars(self._options['compose'], hostvars, host) self._set_composite_vars(self.get_option('compose'), hostvars, host)
# actually update inventory # actually update inventory
for key in hostvars[host]: for key in hostvars[host]:
self.inventory.set_variable(host, key, hostvars[host][key]) self.inventory.set_variable(host, key, hostvars[host][key])
# constructed groups based on conditionals # constructed groups based on conditionals
self._add_host_to_composed_groups(self._options['groups'], hostvars, host) self._add_host_to_composed_groups(self.get_option('groups'), hostvars, host)
def _populate_from_source(self, source_data): def _populate_from_source(self, source_data):
hostvars = {} hostvars = {}
@ -97,7 +97,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
current_host = None current_host = None
# needed to possibly set ansible_host # needed to possibly set ansible_host
netinfo = self._options['network_info_path'] netinfo = self.get_option('network_info_path')
for line in source_data: for line in source_data:
try: try:
@ -173,8 +173,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
pass pass
if not source_data: if not source_data:
b_pwfile = to_bytes(self._options['settings_password_file'], errors='surrogate_or_strict') b_pwfile = to_bytes(self.get_option('settings_password_file'), errors='surrogate_or_strict')
running = self._options['running_only'] running = self.get_option('running_only')
# start getting data # start getting data
cmd = [self.VBOX, b'list', b'-l'] cmd = [self.VBOX, b'list', b'-l']

View file

@ -81,7 +81,7 @@ class InventoryModule(BaseFileInventoryPlugin):
valid = False valid = False
if super(InventoryModule, self).verify_file(path): if super(InventoryModule, self).verify_file(path):
file_name, ext = os.path.splitext(path) file_name, ext = os.path.splitext(path)
if not ext or ext in self._options['yaml_extensions']: if not ext or ext in self.get_option('yaml_extensions'):
valid = True valid = True
return valid return valid

View file

@ -164,9 +164,9 @@ class LookupModule(LookupBase):
self.set_options(var_options=variables, direct=kwargs) self.set_options(var_options=variables, direct=kwargs)
validate_certs = self._options['validate_certs'] validate_certs = self.get_option('validate_certs')
url = self._options['url'] url = self.get_option('url')
version = self._options['version'] version = self.get_option('version')
etcd = Etcd(url=url, version=version, validate_certs=validate_certs) etcd = Etcd(url=url, version=version, validate_certs=validate_certs)