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服务,以调优系统性能 |
二、简化配置管理
- 举例而言,RHEL7的建议时间同步服务为chronyd服务。但在RHEL6中,建议的服务为ntpd服务。在混合了RHEL6和7主机的环境中,管理员必须管理这两个服务的配置文件
- 借助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️⃣:注意:
- 如果在当前Ansible配置文件中覆盖了roles_path,设置了环境变量ANSIBLE_ROLES_PATH
- 或者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_利用系统角色重用内容的更多相关文章
- 主攻ASP.NET.4.5.1 MVC5.0之重生:系统角色与权限(二)
系统角色篇 数据结构 用户管理 Controller代码 public class SystemUserController : Controller { //public void Log() // ...
- shell编程系列10--文本处理三剑客之sed利用sed查询特定内容
shell编程系列10--文本处理三剑客之sed利用sed查询特定内容 利用sed查找文件内容: pattern种类: .8p .,10p .,+5p ./regexp/p .,/regexp/p . ...
- Android 获取系统短信内容
//这里通过内容提供者获取系统短信内容 Uri uri = Uri.parse("content://sms/"); String[] projection = {"_i ...
- (八十一)利用系统自带App来实现导航
利用系统的地图App进行导航,只需要传入起点和终点.启动参数,调用MKMapItem的类方法openMapWithItems:launchOptions:来实现定位,调用此方法后会打开系统的地图App ...
- 利用post请求发送内容进行爬虫
利用post请求发送内容进行爬虫 import requests url = 'http://www.iqianyue.com/mypost' header = {} header['Accept-L ...
- 利用系统函数模拟实现nginx 系统脚本启动的特殊颜色专业效果
利用系统函数模拟实现nginx 系统脚本启动的特殊颜色专业效果/etc/init.d/nginxd {start/stop/restart/reload}利用if语句实现: ============= ...
- UIView封装动画--iOS利用系统提供方法来做转场动画
UIView封装动画--iOS利用系统提供方法来做转场动画 UIViewAnimationOptions option; if (isNext) { option=UIViewAnimationOpt ...
- UIView封装动画--iOS利用系统提供方法来做关键帧动画
iOS利用系统提供方法来做关键帧动画 ios7以后才有用. /*关键帧动画 options:UIViewKeyframeAnimationOptions类型 */ [UIView animateKey ...
- UIView封装动画--iOS 利用系统提供方法来做弹性运动
iOS 利用系统提供方法来做弹性运动 /*创建弹性动画 damping:阻尼,范围0-1,阻尼越接近于0,弹性效果越明显 velocity:弹性复位的速度 */ [UIView animateWith ...
随机推荐
- 201871030106-陈鑫莲 实验三 结对项目—《D{0-1}KP 实例数据集算法实验平台》项目报告
项目 内容 课程班级博客链接 班级博客 这个作业要求链接 作业要求 我的课程学习目标 1.学会结对学习,体会结对学习的快乐2.了解并实践结对编程 3.加深对D{0-1}问题的解法的理解4.复习并熟悉P ...
- MySQL数据库高级一:架构介绍
两天半就可以 严禁使用 精通 在简历上 了解的越多,越比他人有优势 linux的mysql需要使用中文字符集那么就要修改配置文件 1.mysql的linux版 安装和卸载不说了 2.逻辑架构 总体概况 ...
- Maven相关知识总结
目录 认识Maven Maven下载安装 Maven能用来做什么 Maven核心概念 开发目录 坐标和仓库 POM文件 POM文件内容 Maven依赖管理 构建生命周期 构建多模块系统 聚合 继承 聚 ...
- ASP调用SDK微信分享好友、朋友圈
ASP调用SDK微信分享好友.朋友圈需要用到sha1.asp,我先来上主代码,然后再附加sha1.asp,方便大家直接复制过去即可使用. 页面:shara.asp 1 <%@LANGUAGE=& ...
- 脱壳——修复加密IAT
脱壳--修复加密IAT 对两个练手程序进行脱壳修复加密IAT(其实是一个程序,只是用了几种不同的加壳方式) 第一个程序 Aspack.exe 下载链接:https://download.csdn.ne ...
- 07- Linux常用命令
cat命令 作用:将文件内容作为标准输出打印到终端. 格式:cat 文件名1 文件名2 例如: cat more命令: 作用:分页显示文本文件的内容 格式:more 文件名 实例:more he ...
- CTB-Locker敲诈者病毒下载器分析
一. 样本基本信息 样本名称:927354529512.scr 样本大小:110592 字节 病毒名称:Win32.Trojan.Ctb-locker.Auto 样本MD5值:3A6D7E551C13 ...
- SSDT表结构的深入学习
SSDT表的知识目录: A.了解SSDT结构 B.由SSDT索引号获取当前函数地址 C.如何获取索引号 D.获取起源地址-判断SSDT是否被HOOK E.如何向内核地址写入自己代码 A. ...
- 一枚Android "短信小偷" 病毒的分析
一.样本简介 样本来自于吾爱破解论坛链接地址为http://www.52pojie.cn/thread-410238-1-1.html,样本不是很复杂有空就分析了一下.Android病毒样本还是很有意 ...
- hdu3336 KMP + DP 前缀数组出现的次数
题意: 给你一个串,问你他的所有前缀子串在本串中的出现次数,注释:abc的前缀子串是 a ab abc; 思路: 还是利用了next数组,先对子串求出next数组,再开一个数组d ...