2013-06-25 17:04:22 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# This file is part of Ansible
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: stat
|
|
|
|
version_added: "1.3"
|
|
|
|
short_description: retrieve file or file system status
|
|
|
|
description:
|
|
|
|
- Retrieves facts for a file similar to the linux/unix 'stat' command.
|
|
|
|
options:
|
|
|
|
path:
|
|
|
|
description:
|
|
|
|
- The full path of the file/object to get the facts of
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: []
|
2013-07-16 19:27:38 +02:00
|
|
|
follow:
|
|
|
|
description:
|
|
|
|
- Whether to follow symlinks
|
|
|
|
required: false
|
|
|
|
default: no
|
|
|
|
aliases: []
|
2014-02-26 03:33:14 +01:00
|
|
|
get_md5:
|
|
|
|
description:
|
2014-02-28 18:02:58 +01:00
|
|
|
- Whether to return the md5 sum of the file
|
2014-02-26 03:33:14 +01:00
|
|
|
required: false
|
|
|
|
default: yes
|
|
|
|
aliases: []
|
2013-06-25 17:04:22 +02:00
|
|
|
author: Bruce Pennypacker
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2013-07-03 16:17:07 +02:00
|
|
|
# Obtain the stats of /etc/foo.conf, and check that the file still belongs
|
|
|
|
# to 'root'. Fail otherwise.
|
2013-07-25 15:59:23 +02:00
|
|
|
- stat: path=/etc/foo.conf
|
2013-07-03 16:17:07 +02:00
|
|
|
register: st
|
|
|
|
- fail: msg="Whoops! file ownership has changed"
|
|
|
|
when: st.stat.pw_name != 'root'
|
2013-07-25 15:59:23 +02:00
|
|
|
|
|
|
|
# Determine if a path exists and is a directory. Note we need to test
|
|
|
|
# both that p.stat.isdir actually exists, and also that it's set to true.
|
|
|
|
- stat: path=/path/to/something
|
|
|
|
register: p
|
|
|
|
- debug: msg="Path exists and is a directory"
|
|
|
|
when: p.stat.isdir is defined and p.stat.isdir == true
|
2014-02-26 03:33:14 +01:00
|
|
|
|
|
|
|
# Don't do md5 checksum
|
|
|
|
- stat: path=/path/to/myhugefile get_md5=no
|
2013-06-25 17:04:22 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from stat import *
|
2013-07-03 16:17:07 +02:00
|
|
|
import pwd
|
2013-06-25 17:04:22 +02:00
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
path = dict(required=True),
|
2014-02-26 03:33:14 +01:00
|
|
|
follow = dict(default='no', type='bool'),
|
|
|
|
get_md5 = dict(default='yes', type='bool')
|
2014-01-18 22:21:48 +01:00
|
|
|
),
|
|
|
|
supports_check_mode = True
|
2013-06-25 17:04:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
path = module.params.get('path')
|
2013-08-12 09:34:54 +02:00
|
|
|
path = os.path.expanduser(path)
|
2013-07-16 19:27:38 +02:00
|
|
|
follow = module.params.get('follow')
|
2014-02-26 03:33:14 +01:00
|
|
|
get_md5 = module.params.get('get_md5')
|
2013-06-25 17:04:22 +02:00
|
|
|
|
|
|
|
try:
|
2013-07-16 19:27:38 +02:00
|
|
|
if follow:
|
|
|
|
st = os.stat(path)
|
|
|
|
else:
|
|
|
|
st = os.lstat(path)
|
2013-06-25 17:04:22 +02:00
|
|
|
except OSError, e:
|
2013-06-26 18:32:28 +02:00
|
|
|
if e.errno == errno.ENOENT:
|
|
|
|
d = { 'exists' : False }
|
|
|
|
module.exit_json(changed=False, stat=d)
|
2014-02-26 03:33:14 +01:00
|
|
|
|
2013-06-25 17:04:22 +02:00
|
|
|
module.fail_json(msg = e.strerror)
|
|
|
|
|
|
|
|
mode = st.st_mode
|
|
|
|
|
|
|
|
# back to ansible
|
|
|
|
d = {
|
2013-06-26 18:32:28 +02:00
|
|
|
'exists' : True,
|
2014-02-28 17:42:20 +01:00
|
|
|
'mode' : "%04o" % S_IMODE(mode),
|
2013-06-25 17:04:22 +02:00
|
|
|
'isdir' : S_ISDIR(mode),
|
|
|
|
'ischr' : S_ISCHR(mode),
|
|
|
|
'isblk' : S_ISBLK(mode),
|
|
|
|
'isreg' : S_ISREG(mode),
|
|
|
|
'isfifo' : S_ISFIFO(mode),
|
|
|
|
'islnk' : S_ISLNK(mode),
|
|
|
|
'issock' : S_ISSOCK(mode),
|
|
|
|
'uid' : st.st_uid,
|
|
|
|
'gid' : st.st_gid,
|
|
|
|
'size' : st.st_size,
|
|
|
|
'inode' : st.st_ino,
|
|
|
|
'dev' : st.st_dev,
|
|
|
|
'nlink' : st.st_nlink,
|
|
|
|
'atime' : st.st_atime,
|
|
|
|
'mtime' : st.st_mtime,
|
|
|
|
'ctime' : st.st_ctime,
|
|
|
|
'wusr' : bool(mode & stat.S_IWUSR),
|
|
|
|
'rusr' : bool(mode & stat.S_IRUSR),
|
|
|
|
'xusr' : bool(mode & stat.S_IXUSR),
|
|
|
|
'wgrp' : bool(mode & stat.S_IWGRP),
|
|
|
|
'rgrp' : bool(mode & stat.S_IRGRP),
|
|
|
|
'xgrp' : bool(mode & stat.S_IXGRP),
|
|
|
|
'woth' : bool(mode & stat.S_IWOTH),
|
|
|
|
'roth' : bool(mode & stat.S_IROTH),
|
|
|
|
'xoth' : bool(mode & stat.S_IXOTH),
|
|
|
|
'isuid' : bool(mode & stat.S_ISUID),
|
|
|
|
'isgid' : bool(mode & stat.S_ISGID),
|
|
|
|
}
|
|
|
|
|
2013-07-16 19:27:38 +02:00
|
|
|
if S_ISLNK(mode):
|
|
|
|
d['lnk_source'] = os.path.realpath(path)
|
2013-07-03 16:17:07 +02:00
|
|
|
|
2014-03-12 03:50:05 +01:00
|
|
|
if S_ISREG(mode) and get_md5 and os.access(path,os.R_OK):
|
|
|
|
d['md5'] = module.md5(path)
|
|
|
|
|
2013-07-03 16:17:07 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
pw = pwd.getpwuid(st.st_uid)
|
|
|
|
|
|
|
|
d['pw_name'] = pw.pw_name
|
|
|
|
except:
|
|
|
|
pass
|
2014-02-26 03:33:14 +01:00
|
|
|
|
2013-06-25 20:14:18 +02:00
|
|
|
|
2013-06-25 17:04:22 +02:00
|
|
|
module.exit_json(changed=False, stat=d)
|
|
|
|
|
2013-12-02 21:13:49 +01:00
|
|
|
# import module snippets
|
2013-12-02 21:11:23 +01:00
|
|
|
from ansible.module_utils.basic import *
|
2013-06-25 17:04:22 +02:00
|
|
|
|
|
|
|
main()
|