community.general.sefcontext 模块 – 管理 SELinux 文件上下文映射定义

注意

此模块是 community.general 集合 (版本 10.1.0) 的一部分。

如果您使用的是 ansible 软件包,则可能已安装此集合。它不包含在 ansible-core 中。要检查它是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用: ansible-galaxy collection install community.general。您需要其他要求才能使用此模块,有关详细信息,请参见 要求

要在 playbook 中使用它,请指定: community.general.sefcontext

概要

  • 管理 SELinux 文件上下文映射定义。

  • 类似于 semanage fcontext 命令。

要求

执行此模块的主机需要以下要求。

  • libselinux-python

  • policycoreutils-python

参数

参数

注释

ftype

字符串

应应用 SELinux 上下文的文文件类型。

可以使用以下文件类型选项

a 代表所有文件,

b 代表块设备,

c 代表字符设备,

d 代表目录,

f 代表普通文件,

l 代表符号链接,

p 代表命名管道,

s 代表套接字文件。

选项

  • "a" ← (默认)

  • "b"

  • "c"

  • "d"

  • "f"

  • "l"

  • "p"

  • "s"

ignore_selinux_state

布尔值

对于无法获取真实 SELinux 状态的场景(chroot 环境)非常有用。

选项

  • false ← (默认)

  • true

reload

布尔值

提交后重新加载 SELinux 策略。

请注意,这不会将 SELinux 文件上下文应用于现有文件。

选项

  • false

  • true ← (默认)

selevel

别名:serange

字符串

指定 target 的 SELinux 范围。

对于新的文件上下文默认为 s0,修改文件上下文时默认为现有值。

setype

字符串

指定 target 的 SELinux 类型。

seuser

字符串

指定 target 的 SELinux 用户。

对于新的文件上下文默认为 system_u,修改文件上下文时默认为现有值。

state

字符串

SELinux 文件上下文必须为 absent 还是 present

在不使用 setypesubstitute 的情况下指定 absent 会删除与 target 匹配的 SELinux 类型或路径替换映射。

选项

  • "absent"

  • "present" ← (默认)

substitute

别名:equal

字符串

在 community.general 6.4.0 中添加

用于替换指定 target 的文件上下文(s) 的路径。target 子树的上下文标记将与该路径等效。

这也被称为 SELinux 文件上下文等效性,它实现了 SELinux 管理工具的 equal 功能。

目标

别名:path

字符串 / 必填

目标路径(表达式)。

属性

属性

支持

描述

check_mode

支持:完全支持

可以在 check_mode 下运行,并在不修改目标的情况下返回更改状态预测。

diff_mode

支持:完全支持

在差异模式下,将返回有关已更改内容(或在 check_mode 下可能需要更改的内容)的详细信息。

平台

平台: Linux

可以对其进行操作的目标操作系统/系列。

备注

注意

  • 这些更改在重启后仍然有效。

  • setypesubstitute 是互斥的。

  • 如果 state=present,则 setypesubstitute 中的一个是必填的。

  • community.general.sefcontext 模块不会将现有文件修改为新的 SELinux 上下文,因此建议先创建 SELinux 文件上下文,然后再创建文件,或者手动为需要新 SELinux 文件上下文的现有文件运行 restorecon

  • 不将 SELinux fcontext 应用于现有文件是一个深思熟虑的决定,因为不清楚报告的更改将带来什么影响,而且不能保证应用 SELinux fcontext 不会拾取其他无关的先前更改。

示例

- name: Allow apache to modify files in /srv/git_repos
  community.general.sefcontext:
    target: '/srv/git_repos(/.*)?'
    setype: httpd_sys_rw_content_t
    state: present

- name: Substitute file contexts for path /srv/containers with /var/lib/containers
  community.general.sefcontext:
    target: /srv/containers
    substitute: /var/lib/containers
    state: present

- name: Delete file context path substitution for /srv/containers
  community.general.sefcontext:
    target: /srv/containers
    substitute: /var/lib/containers
    state: absent

- name: Delete any file context mappings for path /srv/git
  community.general.sefcontext:
    target: /srv/git
    state: absent

- name: Apply new SELinux file context to filesystem
  ansible.builtin.command: restorecon -irv /srv/git_repos

作者

  • Dag Wieers (@dagwieers)