zoukankan      html  css  js  c++  java
  • Ansible_利用系统角色重用内容

    一、红帽企业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角色可以执行的任务包括:

    • 设置enforcingpermissive模式
    • 对文件系统层次结构的各部分运行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、permissivedisabled。如果未设置,则不更改模式(在上一个实例中已经演示了)

    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
  • 相关阅读:
    finalShell 文件上传拖拽失败
    centos6.x 启动docker报错
    笔记本查看当前登录用户
    保存文件到D盘时显示“你没有权限在此文件夹中保存文件,请联系管理员“其他文件夹正常?
    关于MongoDB配置文件的一个小细节
    ubuntu: mongoDB安装,无需下载
    Java 连接虚拟机中MongoDB 所需依赖
    信息知识竞赛刷题助手
    python超多常用知识记录
    python字典键或值去重
  • 原文地址:https://www.cnblogs.com/itwangqiang/p/13678219.html
Copyright © 2011-2022 走看看