summaryrefslogtreecommitdiff
path: root/lib/ansible/context.py
diff options
context:
space:
mode:
authorToshio Kuratomi <a.badger@gmail.com>2018-12-17 18:10:59 -0800
committerToshio Kuratomi <a.badger@gmail.com>2019-01-03 18:12:23 -0800
commitafdbb0d9d5bebb91f632f0d4a1364de5393ba17a (patch)
tree4f688eed3ef5ea1ccd3f80dc716dc62ba7958f3f /lib/ansible/context.py
parentc18da65089e396ac2e459654398b32f68aecfc98 (diff)
downloadansible-afdbb0d9d5bebb91f632f0d4a1364de5393ba17a.tar.gz
Save the command line arguments into a global context
* Once cli args are parsed, they're constant. So, save the parsed args into the global context for everyone else to use them from now on. * Port cli scripts to use the CLIARGS in the context * Refactor call to parse cli args into the run() method * Fix unittests for changes to the internals of CLI arg parsing * Port callback plugins to use context.CLIARGS * Got rid of the private self._options attribute * Use context.CLIARGS in the individual callback plugins instead. * Also output positional arguments in default and unixy plugins * Code has been simplified since we're now dealing with a dict rather than Optparse.Value
Diffstat (limited to 'lib/ansible/context.py')
-rw-r--r--lib/ansible/context.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/ansible/context.py b/lib/ansible/context.py
new file mode 100644
index 0000000000..eafcbee064
--- /dev/null
+++ b/lib/ansible/context.py
@@ -0,0 +1,53 @@
+# Copyright: (c) 2018, Toshio Kuratomi <tkuratomi@ansible.com>
+# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
+
+# Make coding more python3-ish
+from __future__ import (absolute_import, division, print_function)
+__metaclass__ = type
+
+"""
+Context of the running Ansible.
+
+In the future we *may* create Context objects to allow running multiple Ansible plays in parallel
+with different contexts but that is currently out of scope as the Ansible library is just for
+running the ansible command line tools.
+
+These APIs are still in flux so do not use them unless you are willing to update them with every Ansible release
+"""
+
+from ansible import arguments
+
+
+# Note: this is not the singleton version. That is only created once the program has actually
+# parsed the args
+CLIARGS = arguments.CLIArgs({})
+
+
+class _Context:
+ """
+ Not yet ready for Prime Time
+
+ Eventually this may allow for code which needs to run under different contexts (for instance, as
+ if they were run with different command line args or from different current working directories)
+ to exist in the same process. But at the moment, we don't need that so this code has not been
+ tested for suitability.
+ """
+ def __init__(self):
+ global CLIARGS
+ self._CLIARGS = arguments.CLIArgs(CLIARGS)
+
+ @property
+ def CLIARGS(self):
+ return self._CLIARGS
+
+ @CLIARGS.setter
+ def CLIARGS_set(self, new_cli_args):
+ if not isinstance(new_cli_args, arguments.CLIArgs):
+ raise TypeError('CLIARGS must be of type (ansible.arguments.CLIArgs)')
+ self._CLIARGS = new_cli_args
+
+
+def _init_global_context(cli_args):
+ """Initialize the global context objects"""
+ global CLIARGS
+ CLIARGS = arguments.GlobalCLIArgs.from_options(cli_args)