Skip to content

Commit a4e3575

Browse files
authored
stat module, add option to return SELinux Context
Added get_selinux_context option
1 parent a1d25cc commit a4e3575

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- stat module - add SELinux context as a return value, and add a new option to trigger this return, which is False by default. (https://github.com/ansible/ansible/issues/85217).

lib/ansible/modules/stat.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@
4444
version_added: "2.3"
4545
get_checksum:
4646
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'
4755
extends_documentation_fragment:
4856
- action_common_attributes
4957
- checksum_common
@@ -346,6 +354,12 @@
346354
type: list
347355
sample: [ immutable, extent ]
348356
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'
349363
version:
350364
description: The version/generation attribute of a file according to the filesystem
351365
returned: success, path exists, user can execute the path, lsattr is available and filesystem supports
@@ -434,6 +448,7 @@ def main():
434448
get_checksum=dict(type='bool', default=True),
435449
get_mime=dict(type='bool', default=True, aliases=['mime', 'mime_type', 'mime-type']),
436450
get_attributes=dict(type='bool', default=True, aliases=['attr', 'attributes']),
451+
get_selinux_context=dict(type='bool', default=False),
437452
checksum_algorithm=dict(type='str', default='sha1',
438453
choices=['md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512'],
439454
aliases=['checksum', 'checksum_algo']),
@@ -448,6 +463,7 @@ def main():
448463
get_attr = module.params.get('get_attributes')
449464
get_checksum = module.params.get('get_checksum')
450465
checksum_algorithm = module.params.get('checksum_algorithm')
466+
get_selinux_context = module.params.get('get_selinux_context')
451467

452468
# main stat data
453469
try:
@@ -515,6 +531,10 @@ def main():
515531
if x in out:
516532
output[x] = out[x]
517533

534+
# try to get SELinux context
535+
if get_selinux_context:
536+
output['selinux_context'] = module.selinux_context(b_path)
537+
518538
module.exit_json(changed=False, stat=output)
519539

520540

test/integration/targets/stat/tasks/main.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,27 @@
177177
- "stat_result.changed == false"
178178
- "stat_result.stat.mimetype == 'text/plain'"
179179
- "stat_result.stat.charset == 'us-ascii'"
180+
181+
- name: check stat a file with get_selinux_context on
182+
stat:
183+
path: "{{ remote_tmp_dir }}/foo.txt"
184+
get_selinux_context: True
185+
register: stat_result
186+
187+
- debug: var=stat_result
188+
189+
- assert:
190+
that:
191+
- "'selinux_context' in stat_result.stat"
192+
193+
- name: check stat a file with get_selinux_context off
194+
stat:
195+
path: "{{ remote_tmp_dir }}/foo.txt"
196+
get_selinux_context: False
197+
register: stat_result
198+
199+
- debug: var=stat_result
200+
201+
- assert:
202+
that:
203+
- "'selinux_context' not in stat_result.stat"

0 commit comments

Comments
 (0)