mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Update stat module to handle symlinks
Add follow parameter to stat module that controls whether to follow symlinks. It defaults to no. This then calls os.stat or os.lstat based on the value of follow. Add lnk_source key/value pair if path is a symlink and follow=no. Drop the statement that sets isdir=False and islnk=True when path is a symlink that points to a directory.
This commit is contained in:
parent
fa9635478b
commit
2af6510ded
1 changed files with 14 additions and 4 deletions
|
@ -28,6 +28,12 @@ options:
|
||||||
required: true
|
required: true
|
||||||
default: null
|
default: null
|
||||||
aliases: []
|
aliases: []
|
||||||
|
follow:
|
||||||
|
description:
|
||||||
|
- Whether to follow symlinks
|
||||||
|
required: false
|
||||||
|
default: no
|
||||||
|
aliases: []
|
||||||
author: Bruce Pennypacker
|
author: Bruce Pennypacker
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
@ -49,13 +55,18 @@ def main():
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
argument_spec = dict(
|
argument_spec = dict(
|
||||||
path = dict(required=True),
|
path = dict(required=True),
|
||||||
|
follow = dict(default='no', type='bool')
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
path = module.params.get('path')
|
path = module.params.get('path')
|
||||||
|
follow = module.params.get('follow')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
st = os.stat(path)
|
if follow:
|
||||||
|
st = os.stat(path)
|
||||||
|
else:
|
||||||
|
st = os.lstat(path)
|
||||||
except OSError, e:
|
except OSError, e:
|
||||||
if e.errno == errno.ENOENT:
|
if e.errno == errno.ENOENT:
|
||||||
d = { 'exists' : False }
|
d = { 'exists' : False }
|
||||||
|
@ -98,9 +109,8 @@ def main():
|
||||||
'isgid' : bool(mode & stat.S_ISGID),
|
'isgid' : bool(mode & stat.S_ISGID),
|
||||||
}
|
}
|
||||||
|
|
||||||
if S_ISDIR(mode) and os.path.islink(path):
|
if S_ISLNK(mode):
|
||||||
d['isdir'] = False
|
d['lnk_source'] = os.path.realpath(path)
|
||||||
d['islnk'] = True
|
|
||||||
|
|
||||||
if S_ISREG(mode):
|
if S_ISREG(mode):
|
||||||
d['md5'] = module.md5(path)
|
d['md5'] = module.md5(path)
|
||||||
|
|
Loading…
Reference in a new issue