Add umask option to pip module
On systems with restrictive umasks, the pip module won't allow you to install pip packages that are usable by everyone on the system. This commit adds a umask option to optionally override the umask on a per-package basis.
This commit is contained in:
parent
e5decc3e51
commit
d8d8d7da64
1 changed files with 139 additions and 103 deletions
|
@ -22,6 +22,7 @@
|
|||
import tempfile
|
||||
import re
|
||||
import os
|
||||
import sys
|
||||
|
||||
DOCUMENTATION = '''
|
||||
---
|
||||
|
@ -114,6 +115,17 @@ options:
|
|||
version_added: "1.3"
|
||||
required: false
|
||||
default: null
|
||||
umask:
|
||||
description:
|
||||
- The system umask to apply before installing the pip package. This is
|
||||
useful, for example, when installing on systems that have a very
|
||||
restrictive umask by default (e.g., 0077) and you want to pip install
|
||||
packages which are to be used by all users. Note that this requires you
|
||||
to specify desired umask mode in octal, with a leading 0 (e.g., 0077).
|
||||
version_added: "2.1"
|
||||
required: false
|
||||
default: null
|
||||
|
||||
notes:
|
||||
- Please note that virtualenv (U(http://www.virtualenv.org/)) must be installed on the remote host if the virtualenv parameter is specified and the virtualenv needs to be initialized.
|
||||
requirements: [ "virtualenv", "pip" ]
|
||||
|
@ -159,6 +171,10 @@ EXAMPLES = '''
|
|||
|
||||
# Install (Bottle), forcing reinstallation if it's already installed
|
||||
- pip: name=bottle state=forcereinstall
|
||||
|
||||
# Install (Bottle) while ensuring the umask is 0022 (to ensure other users can use it)
|
||||
- pip: name=bottle umask=0022
|
||||
become: True
|
||||
'''
|
||||
|
||||
def _get_cmd_options(module, cmd):
|
||||
|
@ -258,6 +274,7 @@ def main():
|
|||
editable=dict(default='yes', type='bool', required=False),
|
||||
chdir=dict(default=None, required=False, type='path'),
|
||||
executable=dict(default=None, required=False),
|
||||
umask=dict(reqiured=False,default=None),
|
||||
),
|
||||
required_one_of=[['name', 'requirements']],
|
||||
mutually_exclusive=[['name', 'requirements']],
|
||||
|
@ -271,7 +288,20 @@ def main():
|
|||
extra_args = module.params['extra_args']
|
||||
virtualenv_python = module.params['virtualenv_python']
|
||||
chdir = module.params['chdir']
|
||||
umask = module.params['umask']
|
||||
|
||||
if umask and not isinstance(umask, int):
|
||||
try:
|
||||
umask = int(umask, 8)
|
||||
except Exception:
|
||||
module.fail_json(msg="umask must be an octal integer",
|
||||
details=str(sys.exc_info()[1]))
|
||||
|
||||
|
||||
old_umask = None
|
||||
if umask != None:
|
||||
old_umask = os.umask(umask)
|
||||
try:
|
||||
if state == 'latest' and version is not None:
|
||||
module.fail_json(msg='version is incompatible with state=latest')
|
||||
|
||||
|
@ -364,7 +394,7 @@ def main():
|
|||
|
||||
is_present = _is_present(name, version, out.split())
|
||||
|
||||
changed = (state == 'present' and not is_present) or (state == 'absent' and is_present) or (state == 'forcereinstall' and is_present)
|
||||
changed = (state == 'present' and not is_present) or (state == 'absent' and is_present)
|
||||
module.exit_json(changed=changed, cmd=freeze_cmd, stdout=out, stderr=err)
|
||||
|
||||
if requirements or has_vcs:
|
||||
|
@ -384,6 +414,9 @@ def main():
|
|||
|
||||
if state == 'absent':
|
||||
changed = 'Successfully uninstalled' in out_pip
|
||||
else:
|
||||
if out_freeze_before is None:
|
||||
changed = 'Successfully installed' in out_pip
|
||||
else:
|
||||
if out_freeze_before is None:
|
||||
changed = 'Successfully installed' in out_pip
|
||||
|
@ -394,6 +427,9 @@ def main():
|
|||
module.exit_json(changed=changed, cmd=cmd, name=name, version=version,
|
||||
state=state, requirements=requirements, virtualenv=env,
|
||||
stdout=out, stderr=err)
|
||||
finally:
|
||||
if old_umask != None:
|
||||
os.umask(old_umask)
|
||||
|
||||
# import module snippets
|
||||
from ansible.module_utils.basic import *
|
||||
|
|
Loading…
Reference in a new issue