ansible管理大项目之系统角色的简单使用

红帽企业Linux系统角色

RHEL7.4开始,操作系统随附了多个Ansible角色,作为rhel-system-roles软件包的一部分。在RHEL8中,该软件包可以从AppStream中获取。

名称状态角色描述
rhel-system-roles.kdump全面支持配置kdump崩溃恢复服务
rhel-system-roles.network全面支持配置网络接口
rhel-system-roles.selinux全面支持配置和管理SELinux自定义, 包括SELinux模式、文件和端口上下文、 布尔值设置以及SELinux用户
rhel-system-roles.timesync全面支持使用网络时间协议或精确时间协议配置时间同步
rhel-system-roles.postfix技术预览使用Postfix服务将每个主机配置为邮件传输代理
rhel-system-roles.firewall开发中配置主机的防火墙
rhel-system-roles.tuned开发中配置tuned服务,以调优系统性能

有利于在在多个版本之间标准化红帽企业Linux子系统的配置.

举例而言,RHEL7的建议时间同步服务为chronyd服务。但在RHEL6中,建议的服务为ntpd服务。在混合了RHEL6和7主机的环境中,管理员必须管理这两个服务的配置文件,不怎么方便。

借助RHEL系统角色,管理员不再需要维护这两个服务的配置文件。管理员可以使用rhel-system-roles.timesync角色来配置RHEL6和7主机的时间同步。一个包含角色变量的简化YAML文件可以为这两种类型的主机定义时间同步配置。

安装系统角色上一篇文章讲过,此处不再赘述。

需要注意的是红帽企业Linux中的默认roles_path在路径中包含**/usr/share/ansible/roles**,因此在playbook引用这些角色时Ansible可以很轻松的找到它们。

注意 如果在当前Ansible配置文件中覆盖了roles_path,设置了环境变量ANSIBLE_ROLES_PATH,或者roles_path中更早列出的目录下存在另一个同名的角色,则Ansible可能无法找到系统角色。

利用角色配置时间同步

时间同步可以利用rhel-system-roles.timesync,该角色的详细记录位于**/usr/share/doc/rhel-system-roles/timesync目录下的README.md**中。此文件说明了影响角色行为的所有变量,还包含演示了不同时间同步配置的三个playbook代码片段。

为了手动配置NTP服务器,该角色具有一个名为timesync_ntp_servers的变量。此变量取一个要使用的NTP服务器的列表作为值。列表中的每一项均由一个或多个属性构成。两个关键属性如下:

timesync_ntp_servers属性

属性用途
hostname要与其同步的NTP服务器的主机名。
iburst一个布尔值,用于启用或禁用快速初始同步。在角色中默认为no,但通常应该将属性设为yes.
[root@ansible ansible]# cat time.yml 
---
- name: Time Synchronization Play
  hosts: apache
  vars:
    timesync_ntp_servers:
      - hostname: time1.aliyun.com
        inburst: yes
    timezone: UTC

    roles: timesync
[root@ansible ansible]# ansible-playbook time.yml 
[WARNING]: Found variable using reserved name: roles

PLAY [Time Synchronization Play] ******************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************
ok: [192.168.216.172]

PLAY RECAP ****************************************************************************************************************************************************************************************
192.168.216.172            : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

SELINUX角色示例

rhel-system-roles.selinux角色可以简化SELinux配置设置的管理。它通过利用SELinux相关的Ansible模块来实施。与自行编写任务相比,使用此角色的优势是它能让用户摆脱编写这些任务的职责。取而代之,用户将为角色提供变量以对其进行配置,且角色中维护的代码将确保应用用户需要的SELinux配置。

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

  • 设置enforcing或permissive模式
  • 对文件系统层次结构的各部分运行restorecon
  • 设置SELinux布尔值
  • 永久设置SELinux文件上下文
  • 设置SELinux用户映射
[root@ansible ansible]# cat selinux.yml 
---
- name: selinux stop
  hosts: apache
  vars:
    - selinux_policy: targeted
    - selinux_state: disabled
  roles:
    - rhel-system-roles.selinux
[root@ansible ansible]# ansible-playbook selinux.yml 

PLAY [selinux stop] *******************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************
ok: [192.168.216.172]

TASK [rhel-system-roles.selinux : Install SELinux python2 tools] **********************************************************************************************************************************
ok: [192.168.216.172]

TASK [rhel-system-roles.selinux : Install SELinux python3 tools] **********************************************************************************************************************************
skipping: [192.168.216.172]

TASK [rhel-system-roles.selinux : refresh facts] **************************************************************************************************************************************************
ok: [192.168.216.172]

TASK [rhel-system-roles.selinux : Install SELinux tool semanage on Fedora] ************************************************************************************************************************
skipping: [192.168.216.172]

TASK [rhel-system-roles.selinux : Set permanent SELinux state if enabled] *************************************************************************************************************************
skipping: [192.168.216.172]

TASK [rhel-system-roles.selinux : Set permanent SELinux state if disabled] ************************************************************************************************************************
ok: [192.168.216.172]

TASK [rhel-system-roles.selinux : Set ansible facts if needed] ************************************************************************************************************************************
ok: [192.168.216.172]

TASK [rhel-system-roles.selinux : Fail if reboot is required] *************************************************************************************************************************************
skipping: [192.168.216.172]

TASK [rhel-system-roles.selinux : debug] **********************************************************************************************************************************************************
ok: [192.168.216.172] => {
    "msg": "SELinux is disabled on system - some SELinux modules can crash"
}

TASK [rhel-system-roles.selinux : Drop all local modifications] ***********************************************************************************************************************************
skipping: [192.168.216.172]

TASK [rhel-system-roles.selinux : Purge all SELinux boolean local modifications] ******************************************************************************************************************
skipping: [192.168.216.172]

TASK [rhel-system-roles.selinux : Purge all SELinux file context local modifications] *************************************************************************************************************
skipping: [192.168.216.172]

TASK [rhel-system-roles.selinux : Purge all SELinux port local modifications] *********************************************************************************************************************
skipping: [192.168.216.172]

TASK [rhel-system-roles.selinux : Purge all SELinux login local modifications] ********************************************************************************************************************
skipping: [192.168.216.172]

TASK [rhel-system-roles.selinux : Reload SELinux policy] ******************************************************************************************************************************************
skipping: [192.168.216.172]

TASK [rhel-system-roles.selinux : Set SELinux booleans] *******************************************************************************************************************************************

TASK [rhel-system-roles.selinux : Set SELinux file contexts] **************************************************************************************************************************************

TASK [rhel-system-roles.selinux : Restore SELinux labels on filesystem tree] **********************************************************************************************************************

TASK [rhel-system-roles.selinux : Restore SELinux labels on filesystem tree in check mode] ********************************************************************************************************

TASK [rhel-system-roles.selinux : Set an SELinux label on a port] *********************************************************************************************************************************

TASK [rhel-system-roles.selinux : Set linux user to SELinux user mapping] *************************************************************************************************************************

PLAY RECAP ****************************************************************************************************************************************************************************************
192.168.216.172            : ok=6    changed=0    unreachable=0    failed=0    skipped=16   rescued=0    ignored=0   

有时候,SELinux角色必须确保重新引导受管主机,以便能够完整应用其更改。但是,它本身从不会重新引导主机。如此一来,用户便可以控制重新引导的处理方式。

其工作方式为,该角色将一个布尔值变量selinux_reboot_required设为True,如果需要重新引导,则失败。你可以使用block/rescure结构来从失败中恢复,具体操作为:如果该变量未设为true,则让play失败,如果值是true,则重新引导受管主机并重新运行该角色.

[root@ansible ~]# cat selinux1.yml 
---
- hosts: apache
  tasks:
    - name: Apply Selinux role
      block:
        - name: role use
          include_role:
            name: rhel-system-roles.selinux
      rescue:
        - name: Check for failure for other reasons than  required reboot
          fail:
          when: not selinux_reboot_required

        - name: Restart managed host
          reboot:

        - name: Reapply Selinux role to cpmplete changes
          include_role:
            name: rhel-system-roles.selinux
[root@ansible ~]# ansible-playbook selinux1.yml 

PLAY [apache] *************************************************************************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************************************************************************
ok: [192.168.216.172]

TASK [role use] ***********************************************************************************************************************************************************************************

TASK [rhel-system-roles.selinux : Install SELinux python2 tools] **********************************************************************************************************************************
ok: [192.168.216.172]

TASK [rhel-system-roles.selinux : Install SELinux python3 tools] **********************************************************************************************************************************
skipping: [192.168.216.172]

TASK [rhel-system-roles.selinux : refresh facts] **************************************************************************************************************************************************
ok: [192.168.216.172]

TASK [rhel-system-roles.selinux : Install SELinux tool semanage on Fedora] ************************************************************************************************************************
skipping: [192.168.216.172]

TASK [rhel-system-roles.selinux : Set permanent SELinux state if enabled] *************************************************************************************************************************
skipping: [192.168.216.172]

TASK [rhel-system-roles.selinux : Set permanent SELinux state if disabled] ************************************************************************************************************************
skipping: [192.168.216.172]

TASK [rhel-system-roles.selinux : Set ansible facts if needed] ************************************************************************************************************************************
ok: [192.168.216.172]

TASK [rhel-system-roles.selinux : Fail if reboot is required] *************************************************************************************************************************************
skipping: [192.168.216.172]

TASK [rhel-system-roles.selinux : debug] **********************************************************************************************************************************************************
ok: [192.168.216.172] => {
    "msg": "SELinux is disabled on system - some SELinux modules can crash"
}

TASK [rhel-system-roles.selinux : Drop all local modifications] ***********************************************************************************************************************************
skipping: [192.168.216.172]

TASK [rhel-system-roles.selinux : Purge all SELinux boolean local modifications] ******************************************************************************************************************
skipping: [192.168.216.172]

TASK [rhel-system-roles.selinux : Purge all SELinux file context local modifications] *************************************************************************************************************
skipping: [192.168.216.172]

TASK [rhel-system-roles.selinux : Purge all SELinux port local modifications] *********************************************************************************************************************
skipping: [192.168.216.172]

TASK [rhel-system-roles.selinux : Purge all SELinux login local modifications] ********************************************************************************************************************
skipping: [192.168.216.172]

TASK [rhel-system-roles.selinux : Reload SELinux policy] ******************************************************************************************************************************************
skipping: [192.168.216.172]

TASK [rhel-system-roles.selinux : Set SELinux booleans] *******************************************************************************************************************************************

TASK [rhel-system-roles.selinux : Set SELinux file contexts] **************************************************************************************************************************************

TASK [rhel-system-roles.selinux : Restore SELinux labels on filesystem tree] **********************************************************************************************************************

TASK [rhel-system-roles.selinux : Restore SELinux labels on filesystem tree in check mode] ********************************************************************************************************

TASK [rhel-system-roles.selinux : Set an SELinux label on a port] *********************************************************************************************************************************

TASK [rhel-system-roles.selinux : Set linux user to SELinux user mapping] *************************************************************************************************************************

PLAY RECAP ****************************************************************************************************************************************************************************************
192.168.216.172            : ok=5    changed=0    unreachable=0    failed=0    skipped=17   rescued=0    ignored=0    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值