| 1 |
|
| 2 |
# Copyright: (c) 2017, Ansible Project |
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
--- |
| 10 |
module: stat |
| 11 |
version_added: "1.3" |
| 12 |
short_description: Retrieve file or file system status |
| 13 |
description: |
| 14 |
- Retrieves facts for a file similar to the Linux/Unix C(stat) command. |
| 15 |
- For Windows targets, use the M(ansible.windows.win_stat) module instead. |
| 16 |
options: |
| 17 |
path: |
| 18 |
description: |
| 19 |
- The full path of the file/object to get the facts of. |
| 20 |
type: path |
| 21 |
required: true |
| 22 |
aliases: [ dest, name ] |
| 23 |
follow: |
| 24 |
description: |
| 25 |
- Whether to follow symlinks. |
| 26 |
type: bool |
| 27 |
default: no |
| 28 |
get_mime: |
| 29 |
description: |
| 30 |
- Use file magic and return data about the nature of the file. This uses |
| 31 |
the C(file) utility found on most Linux/Unix systems. |
| 32 |
- This will add both RV(stat.mimetype) and RV(stat.charset) fields to the return, if possible. |
| 33 |
- In Ansible 2.3 this option changed from O(mime) to O(get_mime) and the default changed to V(true). |
| 34 |
type: bool |
| 35 |
default: yes |
| 36 |
aliases: [ mime, mime_type, mime-type ] |
| 37 |
version_added: "2.1" |
| 38 |
get_attributes: |
| 39 |
description: |
| 40 |
- Get file attributes using lsattr tool if present. |
| 41 |
type: bool |
| 42 |
default: yes |
| 43 |
aliases: [ attr, attributes ] |
| 44 |
version_added: "2.3" |
| 45 |
get_checksum: |
| 46 |
version_added: "1.8" |
| 47 |
get_selinux_context: |
| 48 |
description: |
| 49 |
- Get file SELinux context in a list V([user, role, type, range]), |
| 50 |
and will get V([None, None, None, None]) if it is not possible to retrieve the context, |
| 51 |
either because it does not exist or some other issue. |
| 52 |
type: bool |
| 53 |
default: no |
| 54 |
version_added: '2.20' |
| 55 |
extends_documentation_fragment: |
| 56 |
- action_common_attributes |
| 57 |
- checksum_common |
| 58 |
attributes: |
| 59 |
check_mode: |
| 60 |
support: full |
| 61 |
diff_mode: |
| 62 |
support: none |
| 63 |
platform: |
| 64 |
platforms: posix |
| 65 |
seealso: |
| 66 |
- module: ansible.builtin.file |
| 67 |
- module: ansible.windows.win_stat |
| 68 |
author: Bruce Pennypacker (@bpennypacker) |
| 69 |
""" |
| 70 |
|
| 71 |
EXAMPLES = r""" |
| 72 |
# Obtain the stats of /etc/foo.conf, and check that the file still belongs |
| 73 |
# to 'root'. Fail otherwise. |
| 74 |
- name: Get stats of a file |
| 75 |
ansible.builtin.stat: |
| 76 |
path: /etc/foo.conf |
| 77 |
register: st |
| 78 |
- name: Fail if the file does not belong to 'root' |
| 79 |
ansible.builtin.fail: |
| 80 |
msg: "Whoops! file ownership has changed" |
| 81 |
when: st.stat.pw_name != 'root' |
| 82 |
|
| 83 |
# Determine if a path exists and is a symlink. Note that if the path does |
| 84 |
# not exist, and we test sym.stat.islnk, it will fail with an error. So |
| 85 |
# therefore, we must test whether it is defined. |
| 86 |
# Run this to understand the structure, the skipped ones do not pass the |
| 87 |
# check performed by 'when' |
| 88 |
- name: Get stats of the FS object |
| 89 |
ansible.builtin.stat: |
| 90 |
path: /path/to/something |
| 91 |
register: sym |
| 92 |
|
| 93 |
- name: Print a debug message |
| 94 |
ansible.builtin.debug: |
| 95 |
msg: "islnk isn't defined (path doesn't exist)" |
| 96 |
when: sym.stat.islnk is not defined |
| 97 |
|
| 98 |
- name: Print a debug message |
| 99 |
ansible.builtin.debug: |
| 100 |
msg: "islnk is defined (path must exist)" |
| 101 |
when: sym.stat.islnk is defined |
| 102 |
|
| 103 |
- name: Print a debug message |
| 104 |
ansible.builtin.debug: |
| 105 |
msg: "Path exists and is a symlink" |
| 106 |
when: sym.stat.islnk is defined and sym.stat.islnk |
| 107 |
|
| 108 |
- name: Print a debug message |
| 109 |
ansible.builtin.debug: |
| 110 |
msg: "Path exists and isn't a symlink" |
| 111 |
when: sym.stat.islnk is defined and sym.stat.islnk == False |
| 112 |
|
| 113 |
|
| 114 |
# Determine if a path exists and is a directory. Note that we need to test |
| 115 |
# both that p.stat.isdir actually exists, and also that it's set to true. |
| 116 |
- name: Get stats of the FS object |
| 117 |
ansible.builtin.stat: |
| 118 |
path: /path/to/something |
| 119 |
register: p |
| 120 |
- name: Print a debug message |
| 121 |
ansible.builtin.debug: |
| 122 |
msg: "Path exists and is a directory" |
| 123 |
when: p.stat.isdir is defined and p.stat.isdir |
| 124 |
|
| 125 |
- name: Do not calculate the checksum |
| 126 |
ansible.builtin.stat: |
| 127 |
path: /path/to/myhugefile |
| 128 |
get_checksum: no |
| 129 |
|
| 130 |
- name: Use sha256 to calculate the checksum |
| 131 |
ansible.builtin.stat: |
| 132 |
path: /path/to/something |
| 133 |
checksum_algorithm: sha256 |
| 134 |
""" |
| 135 |
|
| 136 |
RETURN = r""" |
| 137 |
stat: |
| 138 |
description: Dictionary containing all the stat data, some platforms might add additional fields. |
| 139 |
returned: success |
| 140 |
type: dict |
| 141 |
contains: |
| 142 |
exists: |
| 143 |
description: If the destination path actually exists or not |
| 144 |
returned: success |
| 145 |
type: bool |
| 146 |
sample: True |
| 147 |
path: |
| 148 |
description: The full path of the file/object to get the facts of |
| 149 |
returned: success and if path exists |
| 150 |
type: str |
| 151 |
sample: '/path/to/file' |
| 152 |
mode: |
| 153 |
description: Unix permissions of the file in octal representation as a string |
| 154 |
returned: success, path exists and user can read stats |
| 155 |
type: str |
| 156 |
sample: 1755 |
| 157 |
isdir: |
| 158 |
description: Tells you if the path is a directory |
| 159 |
returned: success, path exists and user can read stats |
| 160 |
type: bool |
| 161 |
sample: False |
| 162 |
ischr: |
| 163 |
description: Tells you if the path is a character device |
| 164 |
returned: success, path exists and user can read stats |
| 165 |
type: bool |
| 166 |
sample: False |
| 167 |
isblk: |
| 168 |
description: Tells you if the path is a block device |
| 169 |
returned: success, path exists and user can read stats |
| 170 |
type: bool |
| 171 |
sample: False |
| 172 |
isreg: |
| 173 |
description: Tells you if the path is a regular file |
| 174 |
returned: success, path exists and user can read stats |
| 175 |
type: bool |
| 176 |
sample: True |
| 177 |
isfifo: |
| 178 |
description: Tells you if the path is a named pipe |
| 179 |
returned: success, path exists and user can read stats |
| 180 |
type: bool |
| 181 |
sample: False |
| 182 |
islnk: |
| 183 |
description: Tells you if the path is a symbolic link |
| 184 |
returned: success, path exists and user can read stats |
| 185 |
type: bool |
| 186 |
sample: False |
| 187 |
issock: |
| 188 |
description: Tells you if the path is a unix domain socket |
| 189 |
returned: success, path exists and user can read stats |
| 190 |
type: bool |
| 191 |
sample: False |
| 192 |
uid: |
| 193 |
description: Numeric id representing the file owner |
| 194 |
returned: success, path exists and user can read stats |
| 195 |
type: int |
| 196 |
sample: 1003 |
| 197 |
gid: |
| 198 |
description: Numeric id representing the group of the owner |
| 199 |
returned: success, path exists and user can read stats |
| 200 |
type: int |
| 201 |
sample: 1003 |
| 202 |
size: |
| 203 |
description: Size in bytes for a plain file, amount of data for some special files |
| 204 |
returned: success, path exists and user can read stats |
| 205 |
type: int |
| 206 |
sample: 203 |
| 207 |
inode: |
| 208 |
description: Inode number of the path |
| 209 |
returned: success, path exists and user can read stats |
| 210 |
type: int |
| 211 |
sample: 12758 |
| 212 |
dev: |
| 213 |
description: Device the inode resides on |
| 214 |
returned: success, path exists and user can read stats |
| 215 |
type: int |
| 216 |
sample: 33 |
| 217 |
nlink: |
| 218 |
description: Number of links to the inode (hard links) |
| 219 |
returned: success, path exists and user can read stats |
| 220 |
type: int |
| 221 |
sample: 1 |
| 222 |
atime: |
| 223 |
description: Time of last access |
| 224 |
returned: success, path exists and user can read stats |
| 225 |
type: float |
| 226 |
sample: 1424348972.575 |
| 227 |
mtime: |
| 228 |
description: Time of last modification |
| 229 |
returned: success, path exists and user can read stats |
| 230 |
type: float |
| 231 |
sample: 1424348972.575 |
| 232 |
ctime: |
| 233 |
description: Time of last metadata update or creation (depends on OS) |
| 234 |
returned: success, path exists and user can read stats |
| 235 |
type: float |
| 236 |
sample: 1424348972.575 |
| 237 |
wusr: |
| 238 |
description: Tells you if the owner has write permission |
| 239 |
returned: success, path exists and user can read stats |
| 240 |
type: bool |
| 241 |
sample: True |
| 242 |
rusr: |
| 243 |
description: Tells you if the owner has read permission |
| 244 |
returned: success, path exists and user can read stats |
| 245 |
type: bool |
| 246 |
sample: True |
| 247 |
xusr: |
| 248 |
description: Tells you if the owner has execute permission |
| 249 |
returned: success, path exists and user can read stats |
| 250 |
type: bool |
| 251 |
sample: True |
| 252 |
wgrp: |
| 253 |
description: Tells you if the owner's group has write permission |
| 254 |
returned: success, path exists and user can read stats |
| 255 |
type: bool |
| 256 |
sample: False |
| 257 |
rgrp: |
| 258 |
description: Tells you if the owner's group has read permission |
| 259 |
returned: success, path exists and user can read stats |
| 260 |
type: bool |
| 261 |
sample: True |
| 262 |
xgrp: |
| 263 |
description: Tells you if the owner's group has execute permission |
| 264 |
returned: success, path exists and user can read stats |
| 265 |
type: bool |
| 266 |
sample: True |
| 267 |
woth: |
| 268 |
description: Tells you if others have write permission |
| 269 |
returned: success, path exists and user can read stats |
| 270 |
type: bool |
| 271 |
sample: False |
| 272 |
roth: |
| 273 |
description: Tells you if others have read permission |
| 274 |
returned: success, path exists and user can read stats |
| 275 |
type: bool |
| 276 |
sample: True |
| 277 |
xoth: |
| 278 |
description: Tells you if others have execute permission |
| 279 |
returned: success, path exists and user can read stats |
| 280 |
type: bool |
| 281 |
sample: True |
| 282 |
isuid: |
| 283 |
description: Tells you if the invoking user's id matches the owner's id |
| 284 |
returned: success, path exists and user can read stats |
| 285 |
type: bool |
| 286 |
sample: False |
| 287 |
isgid: |
| 288 |
description: Tells you if the invoking user's group id matches the owner's group id |
| 289 |
returned: success, path exists and user can read stats |
| 290 |
type: bool |
| 291 |
sample: False |
| 292 |
lnk_source: |
| 293 |
description: Target of the symlink normalized for the remote filesystem |
| 294 |
returned: success, path exists and user can read stats and the path is a symbolic link |
| 295 |
type: str |
| 296 |
sample: /home/foobar/21102015-1445431274-908472971 |
| 297 |
lnk_target: |
| 298 |
description: Target of the symlink. Note that relative paths remain relative |
| 299 |
returned: success, path exists and user can read stats and the path is a symbolic link |
| 300 |
type: str |
| 301 |
sample: ../foobar/21102015-1445431274-908472971 |
| 302 |
version_added: 2.4 |
| 303 |
checksum: |
| 304 |
description: hash of the file |
| 305 |
returned: success, path exists, user can read stats, path supports |
| 306 |
hashing and supplied checksum algorithm is available |
| 307 |
type: str |
| 308 |
sample: 50ba294cdf28c0d5bcde25708df53346825a429f |
| 309 |
pw_name: |
| 310 |
description: User name of owner |
| 311 |
returned: success, path exists, user can read stats, owner name can be looked up and installed python supports it |
| 312 |
type: str |
| 313 |
sample: httpd |
| 314 |
gr_name: |
| 315 |
description: Group name of owner |
| 316 |
returned: success, path exists, user can read stats, owner group can be looked up and installed python supports it |
| 317 |
type: str |
| 318 |
sample: www-data |
| 319 |
mimetype: |
| 320 |
description: file magic data or mime-type |
| 321 |
returned: success, path exists and user can read stats and |
| 322 |
installed python supports it and the O(get_mime) option was V(true), will |
| 323 |
return V(unknown) on error. |
| 324 |
type: str |
| 325 |
sample: application/pdf; charset=binary |
| 326 |
charset: |
| 327 |
description: file character set or encoding |
| 328 |
returned: success, path exists and user can read stats and |
| 329 |
installed python supports it and the O(get_mime) option was V(true), will |
| 330 |
return V(unknown) on error. |
| 331 |
type: str |
| 332 |
sample: us-ascii |
| 333 |
readable: |
| 334 |
description: Tells you if the invoking user has the right to read the path |
| 335 |
returned: success, path exists and user can read the path |
| 336 |
type: bool |
| 337 |
sample: False |
| 338 |
version_added: 2.2 |
| 339 |
writeable: |
| 340 |
description: Tells you if the invoking user has the right to write the path |
| 341 |
returned: success, path exists and user can write the path |
| 342 |
type: bool |
| 343 |
sample: False |
| 344 |
version_added: 2.2 |
| 345 |
executable: |
| 346 |
description: Tells you if the invoking user has execute permission on the path |
| 347 |
returned: success, path exists and user can execute the path |
| 348 |
type: bool |
| 349 |
sample: False |
| 350 |
version_added: 2.2 |
| 351 |
attributes: |
| 352 |
description: list of file attributes |
| 353 |
returned: success, path exists and user can execute the path |
| 354 |
type: list |
| 355 |
sample: [ immutable, extent ] |
| 356 |
version_added: 2.3 |
| 357 |
selinux_context: |
| 358 |
description: The SELinux context of a path |
| 359 |
returned: success, path exists and user can execute the path |
| 360 |
type: list |
| 361 |
sample: [ user, role, type, range ] |
| 362 |
version_added: '2.20' |
| 363 |
version: |
| 364 |
description: The version/generation attribute of a file according to the filesystem |
| 365 |
returned: success, path exists, user can execute the path, lsattr is available and filesystem supports |
| 366 |
type: str |
| 367 |
sample: "381700746" |
| 368 |
version_added: 2.3 |
| 369 |
disk_usage_bytes: |
| 370 |
description: The disk usage of a path in bytes |
| 371 |
returned: success, path exists, user can execute the path, filesystem supports st_blocks |
| 372 |
type: int |
| 373 |
sample: 1024 |
| 374 |
version_added: '2.21' |
| 375 |
""" |
| 376 |
|
| 377 |
import grp |
| 378 |
import os |
| 379 |
import pwd |
| 380 |
import stat |
| 381 |
|
| 382 |
# import module snippets |
| 383 |
from ansible.module_utils.basic import AnsibleModule |
| 384 |
from ansible.module_utils.common.text.converters import to_bytes |
| 385 |
|
| 386 |
|
| 387 |
def format_output(module, path, st): |
| 388 |
mode = st.st_mode |
| 389 |
|
| 390 |
# back to ansible |
| 391 |
output = dict( |
| 392 |
exists=True, |
| 393 |
path=path, |
| 394 |
mode="%04o" % stat.S_IMODE(mode), |
| 395 |
isdir=stat.S_ISDIR(mode), |
| 396 |
ischr=stat.S_ISCHR(mode), |
| 397 |
isblk=stat.S_ISBLK(mode), |
| 398 |
isreg=stat.S_ISREG(mode), |
| 399 |
isfifo=stat.S_ISFIFO(mode), |
| 400 |
islnk=stat.S_ISLNK(mode), |
| 401 |
issock=stat.S_ISSOCK(mode), |
| 402 |
uid=st.st_uid, |
| 403 |
gid=st.st_gid, |
| 404 |
size=st.st_size, |
| 405 |
inode=st.st_ino, |
| 406 |
dev=st.st_dev, |
| 407 |
nlink=st.st_nlink, |
| 408 |
atime=st.st_atime, |
| 409 |
mtime=st.st_mtime, |
| 410 |
ctime=st.st_ctime, |
| 411 |
wusr=bool(mode & stat.S_IWUSR), |
| 412 |
rusr=bool(mode & stat.S_IRUSR), |
| 413 |
xusr=bool(mode & stat.S_IXUSR), |
| 414 |
wgrp=bool(mode & stat.S_IWGRP), |
| 415 |
rgrp=bool(mode & stat.S_IRGRP), |
| 416 |
xgrp=bool(mode & stat.S_IXGRP), |
| 417 |
woth=bool(mode & stat.S_IWOTH), |
| 418 |
roth=bool(mode & stat.S_IROTH), |
| 419 |
xoth=bool(mode & stat.S_IXOTH), |
| 420 |
isuid=bool(mode & stat.S_ISUID), |
| 421 |
isgid=bool(mode & stat.S_ISGID), |
| 422 |
) |
| 423 |
|
| 424 |
# Platform dependent flags: |
| 425 |
for other in [ |
| 426 |
# Some Linux |
| 427 |
('st_blocks', 'blocks'), |
| 428 |
('st_blksize', 'block_size'), |
| 429 |
('st_rdev', 'device_type'), |
| 430 |
('st_flags', 'flags'), |
| 431 |
# Some Berkeley based |
| 432 |
('st_gen', 'generation'), |
| 433 |
('st_birthtime', 'birthtime'), |
| 434 |
# RISCOS |
| 435 |
('st_ftype', 'file_type'), |
| 436 |
('st_attrs', 'attrs'), |
| 437 |
('st_obtype', 'object_type'), |
| 438 |
# macOS |
| 439 |
('st_rsize', 'real_size'), |
| 440 |
('st_creator', 'creator'), |
| 441 |
('st_type', 'file_type'), |
| 442 |
]: |
| 443 |
if hasattr(st, other[0]): |
| 444 |
output[other[1]] = getattr(st, other[0]) |
| 445 |
if other[0] == 'st_blocks': |
| 446 |
output['disk_usage_bytes'] = st.st_blocks * 512 |
| 447 |
|
| 448 |
return output |
| 449 |
|
| 450 |
|
| 451 |
def main(): |
| 452 |
module = AnsibleModule( |
| 453 |
argument_spec=dict( |
| 454 |
path=dict(type='path', required=True, aliases=['dest', 'name']), |
| 455 |
follow=dict(type='bool', default=False), |
| 456 |
get_checksum=dict(type='bool', default=True), |
| 457 |
get_mime=dict(type='bool', default=True, aliases=['mime', 'mime_type', 'mime-type']), |
| 458 |
get_attributes=dict(type='bool', default=True, aliases=['attr', 'attributes']), |
| 459 |
get_selinux_context=dict(type='bool', default=False), |
| 460 |
checksum_algorithm=dict(type='str', default='sha1', |
| 461 |
choices=['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'], |
| 462 |
aliases=['checksum', 'checksum_algo']), |
| 463 |
), |
| 464 |
supports_check_mode=True, |
| 465 |
) |
| 466 |
|
| 467 |
path = module.params.get('path') |
| 468 |
b_path = to_bytes(path, errors='surrogate_or_strict') |
| 469 |
follow = module.params.get('follow') |
| 470 |
get_mime = module.params.get('get_mime') |
| 471 |
get_attr = module.params.get('get_attributes') |
| 472 |
get_checksum = module.params.get('get_checksum') |
| 473 |
checksum_algorithm = module.params.get('checksum_algorithm') |
| 474 |
get_selinux_context = module.params.get('get_selinux_context') |
| 475 |
|
| 476 |
# main stat data |
| 477 |
try: |
| 478 |
if follow: |
| 479 |
st = os.stat(b_path) |
| 480 |
else: |
| 481 |
st = os.lstat(b_path) |
| 482 |
except FileNotFoundError: |
| 483 |
output = {'exists': False} |
| 484 |
module.exit_json(changed=False, stat=output) |
| 485 |
except OSError as ex: |
| 486 |
module.fail_json(msg=ex.strerror, exception=ex) |
| 487 |
|
| 488 |
# process base results |
| 489 |
output = format_output(module, path, st) |
| 490 |
|
| 491 |
# resolved permissions |
| 492 |
for perm in [('readable', os.R_OK), ('writeable', os.W_OK), ('executable', os.X_OK)]: |
| 493 |
output[perm[0]] = os.access(b_path, perm[1]) |
| 494 |
|
| 495 |
# symlink info |
| 496 |
if output.get('islnk'): |
| 497 |
output['lnk_source'] = os.path.realpath(b_path) |
| 498 |
output['lnk_target'] = os.readlink(b_path) |
| 499 |
|
| 500 |
try: # user data |
| 501 |
pw = pwd.getpwuid(st.st_uid) |
| 502 |
output['pw_name'] = pw.pw_name |
| 503 |
except (TypeError, KeyError): |
| 504 |
pass |
| 505 |
|
| 506 |
try: # group data |
| 507 |
grp_info = grp.getgrgid(st.st_gid) |
| 508 |
output['gr_name'] = grp_info.gr_name |
| 509 |
except (KeyError, ValueError, OverflowError): |
| 510 |
pass |
| 511 |
|
| 512 |
# checksums |
| 513 |
if output.get('isreg') and output.get('readable'): |
| 514 |
if get_checksum: |
| 515 |
output['checksum'] = module.digest_from_file(b_path, checksum_algorithm) |
| 516 |
|
| 517 |
# try to get mime data if requested |
| 518 |
if get_mime: |
| 519 |
output['mimetype'] = output['charset'] = 'unknown' |
| 520 |
mimecmd = module.get_bin_path('file') |
| 521 |
if mimecmd: |
| 522 |
mimecmd = [mimecmd, '--mime-type', '--mime-encoding', b_path] |
| 523 |
try: |
| 524 |
rc, out, err = module.run_command(mimecmd) |
| 525 |
if rc == 0: |
| 526 |
mimetype, charset = out.rsplit(':', 1)[1].split(';') |
| 527 |
output['mimetype'] = mimetype.strip() |
| 528 |
output['charset'] = charset.split('=')[1].strip() |
| 529 |
except Exception: |
| 530 |
pass |
| 531 |
|
| 532 |
# try to get attr data |
| 533 |
if get_attr: |
| 534 |
output['version'] = None |
| 535 |
output['attributes'] = [] |
| 536 |
output['attr_flags'] = '' |
| 537 |
out = module.get_file_attributes(b_path) |
| 538 |
for x in ('version', 'attributes', 'attr_flags'): |
| 539 |
if x in out: |
| 540 |
output[x] = out[x] |
| 541 |
|
| 542 |
# try to get SELinux context |
| 543 |
if get_selinux_context: |
| 544 |
output['selinux_context'] = module.selinux_context(b_path) |
| 545 |
|
| 546 |
module.exit_json(changed=False, stat=output) |
| 547 |
|
| 548 |
|
| 549 |
if __name__ == '__main__': |
| 550 |
main() |