def write_ssh_wrapper(): module_dir = get_module_path() try: # make sure we have full permission to the module_dir, which # may not be the case if we're sudo'ing to a non-root user if os.access(module_dir, os.W_OK|os.R_OK|os.X_OK): fd, wrapper_path = tempfile.mkstemp(prefix=module_dir + '/') else: raise OSError except (IOError, OSError): fd, wrapper_path = tempfile.mkstemp() fh = os.fdopen(fd, 'w+b') template = """#!/bin/sh if [ -z "$GIT_SSH_OPTS" ]; then BASEOPTS="" else BASEOPTS=$GIT_SSH_OPTS fi if [ -z "$GIT_KEY" ]; then ssh $BASEOPTS "$@" else ssh -i "$GIT_KEY" -o IdentitiesOnly=yes $BASEOPTS "$@" fi """ fh.write(template) fh.close() st = os.stat(wrapper_path) os.chmod(wrapper_path, st.st_mode | stat.S_IEXEC) return wrapper_path
def main(): module = AnsibleModule(argument_spec={}) module.exit_json( python={ 'version': { 'full': '%i.%i.%i' % sys.version_info[:3], 'info': list(sys.version_info), 'major': sys.version_info[0], 'minor': sys.version_info[1], 'patch': sys.version_info[2], }, }, argv=sys.argv, __file__=__file__, argv_types_correct=all(type(s) is str for s in sys.argv), env=dict(os.environ), cwd=os.getcwd(), python_path=sys.path, pid=os.getpid(), ppid=os.getppid(), uid=os.getuid(), euid=os.geteuid(), sys_executable=sys.executable, mitogen_loaded='mitogen.core' in sys.modules, hostname=socket.gethostname(), username=pwd.getpwuid(os.getuid()).pw_name, module_tmpdir=getattr(module, 'tmpdir', None), module_path=get_module_path(), )
def write_ssh_wrapper(): module_dir = get_module_path() try: # make sure we have full permission to the module_dir, which # may not be the case if we're sudo'ing to a non-root user if os.access(module_dir, os.W_OK|os.R_OK|os.X_OK): fd, wrapper_path = tempfile.mkstemp(prefix=module_dir + '/') else: raise OSError except (IOError, OSError): fd, wrapper_path = tempfile.mkstemp() fh = os.fdopen(fd, 'w+b') template = """#!/bin/sh if [ -z "$GIT_SSH_OPTS" ]; then BASEOPTS="" else BASEOPTS=$GIT_SSH_OPTS fi if [ -z "$GIT_KEY" ]; then ssh $BASEOPTS "$@" else ssh -i "$GIT_KEY" $BASEOPTS "$@" fi """ fh.write(template) fh.close() st = os.stat(wrapper_path) os.chmod(wrapper_path, st.st_mode | stat.S_IEXEC) return wrapper_path
def main(): module = AnsibleModule(argument_spec={}) module.exit_json( python_version=sys.version[:3], argv=sys.argv, __file__=__file__, argv_types_correct=all(type(s) is str for s in sys.argv), env=dict(os.environ), cwd=os.getcwd(), python_path=sys.path, pid=os.getpid(), ppid=os.getppid(), uid=os.getuid(), euid=os.geteuid(), sys_executable=sys.executable, mitogen_loaded='mitogen.core' in sys.modules, hostname=socket.gethostname(), username=pwd.getpwuid(os.getuid()).pw_name, module_tmpdir=getattr(module, 'tmpdir', None), module_path=get_module_path(), )
def test_module_utils_basic_get_module_path(self): from ansible.module_utils.basic import get_module_path with patch('os.path.realpath', return_value='/path/to/foo/'): self.assertEqual(get_module_path(), '/path/to/foo')
def vvv(msg): log(msg, cap=3) def vvvv(msg): log(msg, cap=4) HAS_KEYCZAR = False try: from keyczar.keys import AesKey HAS_KEYCZAR = True except ImportError: pass SOCKET_FILE = os.path.join(get_module_path(), '.ansible-accelerate', ".local.socket") def get_pid_location(module): """ Try to find a pid directory in the common locations, falling back to the user's home directory if no others exist """ for dir in ['/var/run', '/var/lib/run', '/run', os.path.expanduser("~/")]: try: if os.path.isdir(dir) and os.access(dir, os.R_OK | os.W_OK): return os.path.join(dir, '.accelerate.pid') except: pass module.fail_json(
def test_module_utils_basic_get_module_path(self): from ansible.module_utils.basic import get_module_path with patch("os.path.realpath", return_value="/path/to/foo/"): self.assertEqual(get_module_path(), "/path/to/foo")