当前位置: 代码迷 >> 综合 >> 系统角色的使用(timesync selinux)
  详细解决方案

系统角色的使用(timesync selinux)

热度:100   发布时间:2023-12-02 21:23:30.0

系统角色的使用

安装红帽系统角色

RHEL系统角色由rhel-system-roles软件包提供,该软件包可从AppStream获取。在Ansible控制节点上安装该软件包。

安装红帽系统角色

[root@localhost ~]# yum install -y rhel-system-roles

安装完成后可以在/usr/share/ansible/roles目录下查看红帽系统角色

[root@localhost ~]# ls /usr/share/ansible/roles/
linux-system-roles.kdump    linux-system-roles.postfix  linux-system-roles.storage   rhel-system-roles.kdump    rhel-system-roles.postfix  rhel-system-roles.storage
linux-system-roles.network  linux-system-roles.selinux  linux-system-roles.timesync  rhel-system-roles.network  rhel-system-roles.selinux  rhel-system-roles.timesync

访问RHEL系统角色文档

在安装完成rhel-system-roles包后,查看系统角色文档

[root@localhost ~]# ls /usr/share/doc/rhel-system-roles/
kdump  network  postfix  selinux  storage  timesync

关于系统角色文档介绍

每个角色的文档目录均包含一个README.md文件。README.md文件含有角色的说明,以及角色用法信息(.md是指用markdown写的文件)

README.md文件也会说明影响角色行为的角色变量。通常,README.md文件中含有一个playbook代码片段,用于演示常见配置场景的变量设置

RHEL系统角色演示实例
时间同步角色示例
RHEL系统中时间同步角色的playbook是:rhel-system-rolses.timesync

首次使用时间同步角色可以在rhel-system-roles.timesync/README.md查看使用的帮助文档;里面包含使用的示例

//查看playbook
[root@localhost project]# cat playbook.yaml

---
- hosts: allvars:timesync_ntp_servers:-hostname: 192.168.121.81iburst:yesroles:- rhel-system-roles.timesync//查看控制节点的时间[root@localhost project]# date
Aug Sep 04 03:13:00 CST 2021//执行play[root@localhost project]# ansible-playbook playbook.yamlPLAY [all] ****************************************************************************************************************************************************************TASK [Gathering Facts] ****************************************************************************************************************************************************
ok: [192.168.121.81]TASK [rhel-system-roles.timesync : Checkif only NTP is needed] ***********************************************************************************************************
ok: [192.168.121.81]TASK [rhel-system-roles.timesync : Checkif single PTP is needed] *********************************************************************************************************
skipping: [192.168.121.81]TASK [rhel-system-roles.timesync : Checkif both NTP and PTP are needed] **************************************************************************************************
skipping: [192.168.121.81]TASK [rhel-system-roles.timesync : Determine current NTP provider] ********************************************************************************************************
ok: [192.168.121.81]TASK [rhel-system-roles.timesync : Select NTP provider] *******************************************************************************************************************
ok: [192.168.121.81]TASK [rhel-system-roles.timesync : Install chrony] ************************************************************************************************************************
ok: [192.168.121.81]//查看受控主机上的原始时间[root@localhost ~]# date
Fri Jan  1 08:44:28 CST 2020//执行play后,受控主机上的时间

调用Selinux角色示例

rhel-system-roles.selinux角色可以简化SELinux配置设置的管理。它通过利用SELinux相关的Ansible模块来实施

Selinux角色可以执行的任务包括:

设置enforcing或permissive模式
对文件系统层次结构的各部分运行restorecon
设置SELinux布尔值
永久设置SELinux文件上下文
设置SELinux用户映射

使用rhel-system-roles.selinux角色配置修改selinux的属性时,不能立即完整更改,需要重启;
其工作方式为,该角色将一个布尔值变量selinux_reboot_required设为True,如果需要重新引导,则失败。
你可以使用block/rescure结构来从失败中恢复,具体操作为:如果该变量未设为true,则让play失败,如果值是true,则重新引导受管主机并重新运行该角色

//查看受管主机上的selinux状态
[root@localhost project]# ansible all -a 'cat /etc/selinux/config'
192.168.121.81 | CHANGED | rc=0 >># This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted//查看playbook
[root@localhost project]# cat playbook.yaml
---
- hosts: allvars:selinux_policy: targetedselinux_state: disabledtasks:- name: apply selinux roleblock:- include_role:name: rhel-system-roles.selinuxrescue:- name: required rebootfail:when: not selinux_reboot_required- name: reboot hostreboot:- name: Reapply SELinux role to complete changesinclude_role:name: rhel-system-roles.selinux//执行play后,查看受管主机上的selinux状态
[root@localhost project]# ansible all -a 'cat /etc/selinux/config'
192.168.121.81 | CHANGED | rc=0 >># This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

配置SELinux角色

用于配置rhel-system-roles.selinux角色的变量的详细记录位于其README.md文件中

selinux_state变量设置SELinux的运行模式。它可以设为enforcing、permissive或disabled。如果未设置,则不更改模式(在上一个实例中已经演示了)

selinux_booleans变量取一个要调整的SELinux布尔值的列表作为值。
列表中的每一项是变量的散列/字典:布尔值的name、state(它应是on还是off),以及该设置是否应在重新引导后persistent

selinux_fcontext变量取一个要永久设置(或删除)的文件上下文的列表作为值。它的工作方式与selinux fcontent命令非常相似

selinux_restore_dirs变量指定要对其运行restorecon的目录的列表

selinux_ports变量取应当具有特定SELinux类型的端口的列表作为值

将httpd_enable_homedirs永久设为on

//查看受管主机上的httpd_enable_homedirs

[root@localhost project]# ansible all -m shell -a 'semanage boolean -l | grep httpd_enable_homedirs'
192.168.121.81 | CHANGED | rc=0 >>
httpd_enable_homedirs          (off  ,  off)  Allow httpd toenable homedirs//查看playbook
[root@localhost project]# cat playbook.yaml
---
- hosts: allvars:selinux_booleans:- name: httpd_enable_homedirsstate: onpersistent:yestasks:- include_role:name: rhel-system-roles.selinux//执行paly后,查看受管主机上的httpd_enable_homedirs
[root@localhost project]# ansible all -m shell -a 'semanage boolean -l | grep httpd_enable_homedirs'
192.168.121.81 | CHANGED | rc=0 >>
httpd_enable_homedirs          (on   ,   on)  Allow httpd toenable homedirs

设置/root/file的默认安全上下文的属性为httpd_sys_content_t

 //查看/root/file的默认安全上下文
[root@localhost project]# ansible all -a "ls -Z"
192.168.121.81 | CHANGED | rc=0 >>system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
unconfined_u:object_r:admin_home_t:s0file//查看playbook
[root@localhost project]# cat playbook.yaml
---
- hosts: allvars:selinux_fcontexts:- target:/root/file(/.*)?setype: httpd_sys_content_tstate: presentselinux_restore_dirs:-/root/filetasks:- include_role:name: rhel-system-roles.selinux//执行play后,差看你受管主机上file的安全上下文属性
[root@localhost project]# ansible all -a 'ls -Z'
192.168.121.81 | CHANGED | rc=0 >>system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
unconfined_u:object_r:httpd_sys_content_t:s0file