在Ubuntu上使用Ansible管理SELinux(Security-Enhanced Linux)可以通过多种方式实现。以下是一些常见的方法:
Ansible提供了多个模块来管理SELinux,例如seboolean
、selinux
和file
模块。
首先,确保你已经在Ubuntu上安装了Ansible:
sudo apt update
sudo apt install ansible
创建一个Ansible inventory文件(例如hosts.ini
),并添加你的目标主机:
[selinux_servers]
server1 ansible_host=192.168.1.101
server2 ansible_host=192.168.1.102
seboolean
模块你可以使用seboolean
模块来管理SELinux布尔值。例如,启用或禁用某个布尔值:
---
- name: Enable HTTPD can_network_connect
hosts: selinux_servers
tasks:
- name: Set httpd_can_network_connect to on
seboolean:
name: httpd_can_network_connect
state: yes
persistent: yes
selinux
模块selinux
模块提供了更高级的SELinux管理功能,例如设置SELinux模式(enforcing或permissive):
---
- name: Set SELinux to permissive mode
hosts: selinux_servers
tasks:
- name: Set SELinux to permissive mode
selinux:
policy: targeted
state: permissive
file
模块你也可以使用file
模块来管理文件的安全上下文:
---
- name: Set security context for a file
hosts: selinux_servers
tasks:
- name: Set security context for /etc/passwd
file:
path: /etc/passwd
seclabel: "system_u:object_r:etc_t:s0"
你可以将上述任务组合成一个Ansible Playbook,以便更方便地管理和执行。
---
- name: Manage SELinux on Ubuntu servers
hosts: selinux_servers
become: yes
tasks:
- name: Enable HTTPD can_network_connect
seboolean:
name: httpd_can_network_connect
state: yes
persistent: yes
- name: Set SELinux to permissive mode
selinux:
policy: targeted
state: permissive
- name: Set security context for /etc/passwd
file:
path: /etc/passwd
seclabel: "system_u:object_r:etc_t:s0"
如果你有多个Playbook需要管理SELinux,可以考虑创建一个Ansible Role来封装这些任务。
ansible-galaxy init selinux_management
在selinux_management/tasks/main.yml
中添加你的任务:
---
- name: Enable HTTPD can_network_connect
seboolean:
name: httpd_can_network_connect
state: yes
persistent: yes
- name: Set SELinux to permissive mode
selinux:
policy: targeted
state: permissive
- name: Set security context for /etc/passwd
file:
path: /etc/passwd
seclabel: "system_u:object_r:etc_t:s0"
在你的Playbook中使用这个Role:
---
- name: Apply SELinux management tasks
hosts: selinux_servers
become: yes
roles:
- selinux_management
通过这些方法,你可以在Ubuntu上使用Ansible有效地管理SELinux。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:Ubuntu SELinux如何使用