一、红帽企业Linux系统角色

1、RHEL系统角色

名称 状态 角色描述
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服务,以调优系统性能

二、简化配置管理

  1. 举例而言,RHEL7的建议时间同步服务为chronyd服务。但在RHEL6中,建议的服务为ntpd服务。在混合了RHEL6和7主机的环境中,管理员必须管理这两个服务的配置文件
  2. 借助RHEL系统角色,管理员不再需要维护这两个服务的配置文件。管理员可以使用rhel-system-roles.timesync角色来配置RHEL6和7主机的时间同步。
    • 一个包含角色变量的简化YAML文件可以为这两种类型的主机定义时间同步配置。

三、安装RHEL系统角色

1、安装红帽系统角色

1️⃣: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

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

  • roles_path可以在/etc/ansible/ansible.cfg文件中指定

    [root@localhost ~]# grep roles_path /etc/ansible/ansible.cfg
    #roles_path = /etc/ansible/roles

3️⃣:注意:

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

四、访问RHEL系统角色文档

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

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

2、关于系统角色文档介绍

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

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

3️⃣:部分角色文档目录中含有示例playbook。首次使用某一角色时,请查看文档目录中的任何额外示例playbook

  • 简单实例:

    [root@localhost ~]# cd /usr/share/ansible/roles/
    [root@localhost roles]# cd rhel-system-roles.network/
    [root@localhost rhel-system-roles.network]# less README.md
    Examples of Options
    ------------------- Setting the same connection profile multiple times: ```yaml
    network_connections:
    - name: Wired0
    type: ethernet
    interface_name: eth0
    ip:
    dhcp4: yes - name: Wired0
    state: up、

五、RHEL系统角色演示实例

1、时间同步角色示例

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

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

3️⃣:演示实例:

 //查看playbook
[root@localhost project]# cat playbook.yaml
---
- hosts: all
vars:
timesync_ntp_servers:
- hostname: 192.168.121.81
iburst: yes roles:
- rhel-system-roles.timesync //查看控制节点的时间
[root@localhost project]# date
Thu Sep 17 03:13:00 CST 2020 //执行play
[root@localhost project]# ansible-playbook playbook.yaml PLAY [all] **************************************************************************************************************************************************************** TASK [Gathering Facts] ****************************************************************************************************************************************************
ok: [192.168.121.81] TASK [rhel-system-roles.timesync : Check if only NTP is needed] ***********************************************************************************************************
ok: [192.168.121.81] TASK [rhel-system-roles.timesync : Check if single PTP is needed] *********************************************************************************************************
skipping: [192.168.121.81] TASK [rhel-system-roles.timesync : Check if 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 1999 //执行play后,受控主机上的时间

4️⃣:此示例在play的vars部分中设置角色变量,但更好的做法可能是将它们配置为主机或主机组的清单变量

2、调用Selinux角色示例

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

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

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

3️⃣:演示实例:

  • 实例说明:使用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: 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.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

3、配置SELinux角色

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

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

3️⃣:selinux_booleans变量取一个要调整的SELinux布尔值的列表作为值。

  • 列表中的每一项是变量的散列/字典:布尔值的name、state(它应是on还是off),以及该设置是否应在重新引导后persistent

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

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

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

7️⃣:演示实例:

  • 演示实例一:将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 to enable 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.121.81 | CHANGED | rc=0 >>
    httpd_enable_homedirs (on , on) Allow httpd to enable 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:s0 file //查看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.121.81 | CHANGED | rc=0 >>
    system_u:object_r:admin_home_t:s0 anaconda-ks.cfg
    unconfined_u:object_r:httpd_sys_content_t:s0 file
  • 实例三:添加端口号777

      //查看受管主机上的httphttp端口
    [root@localhost project]# ansible all -m shell -a 'semanage port -l | grep http'
    192.168.121.81 | CHANGED | rc=0 >>
    http_cache_port_t tcp 8080, 8118, 8123, 10001-10010
    http_cache_port_t udp 3130
    http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
    pegasus_http_port_t tcp 5988
    pegasus_https_port_t tcp 5989 //查看playbook
    [root@localhost project]# cat playbook.yaml
    ---
    - hosts: all
    vars:
    selinux_ports:
    - ports: 777
    setype: http_port_t
    proto: tcp
    state: present tasks:
    - include_role:
    name: rhel-system-roles.selinux //执行play后,查看受管主机上端口
    [root@localhost project]# ansible all -m shell -a 'semanage port -l|grep http'
    192.168.121.81 | CHANGED | rc=0 >>
    http_cache_port_t tcp 8080, 8118, 8123, 10001-10010
    http_cache_port_t udp 3130
    http_port_t tcp 777, 80, 81, 443, 488, 8008, 8009, 8443, 9000
    pegasus_http_port_t tcp 5988
    pegasus_https_port_t tcp 59896u

Ansible_利用系统角色重用内容的更多相关文章

  1. 主攻ASP.NET.4.5.1 MVC5.0之重生:系统角色与权限(二)

    系统角色篇 数据结构 用户管理 Controller代码 public class SystemUserController : Controller { //public void Log() // ...

  2. shell编程系列10--文本处理三剑客之sed利用sed查询特定内容

    shell编程系列10--文本处理三剑客之sed利用sed查询特定内容 利用sed查找文件内容: pattern种类: .8p .,10p .,+5p ./regexp/p .,/regexp/p . ...

  3. Android 获取系统短信内容

    //这里通过内容提供者获取系统短信内容 Uri uri = Uri.parse("content://sms/"); String[] projection = {"_i ...

  4. (八十一)利用系统自带App来实现导航

    利用系统的地图App进行导航,只需要传入起点和终点.启动参数,调用MKMapItem的类方法openMapWithItems:launchOptions:来实现定位,调用此方法后会打开系统的地图App ...

  5. 利用post请求发送内容进行爬虫

    利用post请求发送内容进行爬虫 import requests url = 'http://www.iqianyue.com/mypost' header = {} header['Accept-L ...

  6. 利用系统函数模拟实现nginx 系统脚本启动的特殊颜色专业效果

    利用系统函数模拟实现nginx 系统脚本启动的特殊颜色专业效果/etc/init.d/nginxd {start/stop/restart/reload}利用if语句实现: ============= ...

  7. UIView封装动画--iOS利用系统提供方法来做转场动画

    UIView封装动画--iOS利用系统提供方法来做转场动画 UIViewAnimationOptions option; if (isNext) { option=UIViewAnimationOpt ...

  8. UIView封装动画--iOS利用系统提供方法来做关键帧动画

    iOS利用系统提供方法来做关键帧动画 ios7以后才有用. /*关键帧动画 options:UIViewKeyframeAnimationOptions类型 */ [UIView animateKey ...

  9. UIView封装动画--iOS 利用系统提供方法来做弹性运动

    iOS 利用系统提供方法来做弹性运动 /*创建弹性动画 damping:阻尼,范围0-1,阻尼越接近于0,弹性效果越明显 velocity:弹性复位的速度 */ [UIView animateWith ...

随机推荐

  1. 一文搞定zuul使用

    前言 在深入探讨Spring Cloud Gateway的细节之前,让我们了解有关反向代理和api网关模式的一些基础知识. 什么是反向代理? 反向代理是代表其他事物进行请求的事物.它的行为更像是简单的 ...

  2. lms框架分布式事务使用简介

    lms框架的分布式事务解决方案采用的TCC事务模型.在开发过程中参考和借鉴了hmily.使用AOP的编程思想,在rpc通信过程中通过拦截器的方式对全局事务或是分支事务进行管理和协调. 本文通过lms. ...

  3. (十六)Struts2的标签库

    一.简介 Struts2的标签库使用OGNL为基础,大大简化了数据的输出,也提供了大量标签来生成页面效果,功能非常强大. 在早期的web应用开发中,jsp页面主要使用jsp脚本来控制输出.jsp页面嵌 ...

  4. 无法Ping通阿里云服务器的公网IP地址的解决方法

    解决步骤: 1.打开控制台2.打开防火墙3.添加规则添加规则的详情页,可以添加全部TCP也可以自定义添加一定端口范围的TCP:

  5. 2021 年最值得推荐的 7 个 Angular 前端组件库 - DevUI

    摘要:DevUI 是一款面向企业中后台产品的开源前端解决方案,它倡导沉浸.灵活.至简的设计价值观,提倡设计者为真实的需求服务,为多数人的设计,拒绝哗众取宠.取悦眼球的设计.如果你正在开发 ToB 的工 ...

  6. python 迭代器,生成器,表达式

    1.迭代器 (1)什么是迭代器: #迭代器即迭代的工具,那什么是迭代呢?#迭代是一个重复的过程,每次重复即一次迭代,并且每次迭代的结果都是下一次迭代的初始值 while True: #只是单纯地重复, ...

  7. 2-7 Java基础数据类型之字符型

    代码中输入如下部分: /* char的取值范围0-65535 */ public class DataType06 { public static void main(String[]args){ c ...

  8. SQL 查询的执行顺序

    SELECT语句的完整语法如下 SELECT DISTINCT <select_list> FROM <left_table> <join_type> JOIN & ...

  9. window.location 与window.open区别

    window.location 与window.open区别 1.window.location是window对象的属性,而window.open是window对象的方法   window.locat ...

  10. PHP--date转成时间戳,time()获取的是秒数

    date("Y-m-d H:i:s");  //如果存成datetime型在MYSQL中 必须用这种格式 实现功能:获取某个日期的时间戳,或获取某个时间的时间戳.strtotime ...