系统角色的使用

系统角色的使用


安装红帽系统角色

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

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

安装完成后可以在/usr/share/ansible/roles目录下查看红帽系统角色
[root@localhost ~]# ls /usr/share/ansible/roles/
linux-system-roles.certificate      linux-system-roles.ssh             rhel-system-roles.metrics
linux-system-roles.crypto_policies  linux-system-roles.sshd            rhel-system-roles.nbde_client
linux-system-roles.ha_cluster       linux-system-roles.storage         rhel-system-roles.nbde_server
linux-system-roles.kdump            linux-system-roles.timesync        rhel-system-roles.network
linux-system-roles.kernel_settings  linux-system-roles.tlog            rhel-system-roles.postfix
linux-system-roles.logging          linux-system-roles.vpn             rhel-system-roles.selinux
linux-system-roles.metrics          rhel-system-roles.certificate      rhel-system-roles.ssh
linux-system-roles.nbde_client      rhel-system-roles.crypto_policies  rhel-system-roles.sshd
linux-system-roles.nbde_server      rhel-system-roles.ha_cluster       rhel-system-roles.storage
linux-system-roles.network          rhel-system-roles.kdump            rhel-system-roles.timesync
linux-system-roles.postfix          rhel-system-roles.kernel_settings  rhel-system-roles.tlog
linux-system-roles.selinux          rhel-system-roles.logging          rhel-system-roles.vpn

访问RHEL系统角色文档

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

[root@localhost ~]# ls /usr/share/doc/rhel-system-roles/
certificate      ha_cluster       logging      nbde_server  selinux  storage   vpn
collection       kdump            metrics      network      ssh      timesync
crypto_policies  kernel_settings  nbde_client  postfix      sshd     tlog

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

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


RHEL系统角色演示实例
timesync角色示例

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

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

# 查看playbook
[root@localhost project]# cat timesync.yml
---
- hosts: all
  vars:
    timesync_ntp_servers:
      -hostname: 192.168.10.102
        iburst:yes
 
  roles:
    - rhel-system-roles.timesync

# 查看控制节点的时间
[root@web01 ~]# date
Wed Jun 15 00:40:40 CST 2022
# 执行play
[root@localhost project]# ansible-playbook playbook.yaml
PLAY [all] ****************************************************************************************************************************************************************
 
TASK [Gathering Facts] ****************************************************************************************************************************************************
ok: [192.168.10.102]
 
TASK [rhel-system-roles.timesync : Checkif only NTP is needed] ***********************************************************************************************************
ok: [192.168.10.102]
 
TASK [rhel-system-roles.timesync : Checkif single PTP is needed] *********************************************************************************************************
skipping: [192.168.10.102]
 
TASK [rhel-system-roles.timesync : Checkif both NTP and PTP are needed] **************************************************************************************************
skipping: [192.168.10.102]
 
TASK [rhel-system-roles.timesync : Determine current NTP provider] ********************************************************************************************************
ok: [192.168.10.102]
 
TASK [rhel-system-roles.timesync : Select NTP provider] *******************************************************************************************************************
ok: [192.168.10.102]
 
TASK [rhel-system-roles.timesync : Install chrony] ************************************************************************************************************************
ok: [192.168.10.102]
# 查看受控主机上的原始时间
[root@web01 ~]# date
Wed Jun 15 00:40:40 CST 2022

调用Selinux角色示例

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

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

  • 设置enforcingpermissive模式
  • 对文件系统层次结构的各部分运行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.10.102 | CHANGED | rc=0 >>
 
# This file localhosts 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: all
  vars:
    selinux_policy: targeted
    selinux_state: disabled
  tasks:
    - name: apply selinux role
      block:
        - include_role:
            name: rhel-system-roles.selinux
 
      rescue:
        - name: required reboot
          fail:
          when: not selinux_reboot_required
 
        - name: reboot host
          reboot:
 
        - name: Reapply SELinux role to complete changes
          include_role:
            name: rhel-system-roles.selinux
 
 
# 执行play后,查看受管主机上的selinux状态
[root@localhost project]# ansible all -a 'cat /etc/selinux/config'
192.168.10.102 | CHANGED | rc=0 >>
 
# This file localhosts 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的运行模式。它可以设为enforcingpermissivedisabled。如果未设置,则不更改模式(在上一个实例中已经演示了)

selinux_booleans变量取一个要调整的SELinux布尔值的列表作为值。
列表中的每一项是变量的散列/字典:布尔值的namestate(它应是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.10.102 | CHANGED | rc=0 >>
httpd_enable_homedirs          (off  ,  off)  Allow httpd toenable homedirs
 
  
//查看playbook
[root@localhost project]# cat playbook.yaml
---
- hosts: all
  vars:
    selinux_booleans:
      - name: httpd_enable_homedirs
        state: on
        persistent:yes
 
  tasks:
    - 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.10.102 | 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.10.102 | 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: all
  vars:
    selinux_fcontexts:
      - target:/root/file(/.*)?
        setype: httpd_sys_content_t
        state: present
     
    selinux_restore_dirs:
      -/root/file
 
  tasks:
    - include_role:
        name: rhel-system-roles.selinux
 
  
# 执行play后,查看你受管主机上file的安全上下文属性
[root@localhost project]# ansible all -a 'ls -Z'
192.168.10.102 | CHANGED | rc=0 >>
system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
unconfined_u:object_r:httpd_sys_content_t:s0file
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值