如何使用 Ansible 的 selinux_permissive 模块自动化启用或禁用 SELinux 的 Permissive 域?以下是一个示例,展示如何通过 Ansible Playbook 将特定进程(如 httpd_t)设置为 Permissive 模式,同时保持整个系统处于 Enforcing 模式,并确保更改在重启后仍然有效。
SELinux Permissive 域
SELinux Permissive 域允许管理员将单个进程(域)设置为 Permissive 模式,而不是将整个系统设置为 Permissive 模式。
- Enforcing 模式:SELinux 强制执行安全策略。
- Permissive 模式:SELinux 记录违反策略的行为,但不强制执行。
- Permissive 域:仅将特定进程设置为 Permissive 模式,其余系统仍处于 Enforcing 模式。
Ansible 模块 selinux_permissive 概述
模块名称:community.general.selinux_permissive
功能:更改 SELinux 策略中的 Permissive 域
community.general.selinux_permissive 是 Ansible 社区维护的模块集合的一部分,用于管理 SELinux 策略。
该模块支持多种 Linux 发行版,需要目标系统上安装 policycoreutils-python-utils 包以使用 semanage 工具。
参数介绍
name(字符串):SELinux 域的名称。permissive(布尔值):是否将该域设置为 Permissive 模式(true或false)。no_reload(布尔值):是否在更改设置后重新加载策略,默认为false,即更改后重新加载策略。
示例:使用 Ansible Playbook 启用或禁用 SELinux Permissive 域
Playbook 文件:selinux_permissivedomain.yml
---
- name: selinux_permissive module demo
hosts: all
become: true
tasks:
- name: 确保 semanage 工具已安装
ansible.builtin.package:
name: "policycoreutils-python-utils"
state: present
- name: 将 httpd_t 域设置为 Permissive 模式
community.general.selinux_permissive:
name: httpd_t
permissive: true
执行命令
$ ansible-playbook -i virtualmachines/demo/inventory selinux/selinux_permissivedomain.yml
执行结果
PLAY [selinux_permissive module demo] ****************************************************
TASK [Gathering Facts] *****************************************************
ok: [demo.example.com]
TASK [确保 semanage 工具已安装] *****************************************************
changed: [demo.example.com]
TASK [将 httpd_t 域设置为 Permissive 模式] *****************************************************
changed: [demo.example.com]
PLAY RECAP *********************************************************************
demo.example.com : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
执行前后对比
执行前:
$ ssh devops@demo.example.com
[devops@demo ~]$ sudo su
[root@demo devops]# sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
Memory protection checking: actual (secure)
Max kernel policy version: 33
[root@demo devops]# semanage permissive -l
bash: semanage: command not found
[root@demo devops]#
执行后:
$ ssh devops@demo.example.com
[devops@demo ~]$ sudo su
[root@demo devops]# sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
Memory protection checking: actual (secure)
Max kernel policy version: 33
[root@demo devops]# semanage permissive -l
Builtin Permissive Types
Customized Permissive Types
httpd_t
[root@demo devops]# reboot
Connection to demo.example.com closed by remote host.
Connection to demo.example.com closed.
$ ssh devops@demo.example.com
[devops@demo ~]$ sudo su
[root@demo devops]# sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
Memory protection checking: actual (secure)
Max kernel policy version: 33
[root@demo devops]# semanage permissive -l
Builtin Permissive Types
Customized Permissive Types
httpd_t
[root@demo devops]#
注意事项
- 依赖包:确保目标系统已安装
policycoreutils-python-utils包。 - 持久化:更改后,Permissive 域的设置将在重启后仍然有效。
- 幂等性:如果域的状态未发生变化,任务将不会重复执行,确保 Playbook 的幂等性。
希望这个示例对您有帮助!