角色结构、角色使用、角色结构创建方式

角色结构、角色使用、角色结构创建方式

1. 角色结构

  • ansible角色结构由子目录和文件的标准化结构定义
  • 子目录按照各个文件在角色中的用途进行命名
// 官方角色目录结构
site.yml
webservers.yml
fooservers.yml
roles/
  common/
      tasks/ 			    // tasks任务:(定义角色任务)
      handlers/             // files文件:(静态文件、配置文件、安装包)
      files/			    // template模板:(任务引用的jinja2模板直接写入)
      templates/		    // vars变量:(不想变量修改写在vars里面,想修改变量写在default默认里
      vars/				    // default默认:(变量可以覆盖)
      defaults/		        // handlers:(处理程序)main,yml
      meta/  			    // meta元数据:(对任务的一个说明)main.yml
      tests/      	        // tests测试:测试角色、功能		

  webservers/
      tasks/
      defaults/
      meta/

!!!注意:不是所有角色都拥有这些子目录//用到哪些功能就加哪些功能

2. 系统角色使用:

RHEL7.4后,操作系统有了多个ansible角色,作为rhel-system-roles软件包的一部分,可从AppStream中获取。

2.1 系统角色描述

名称状态角色描述
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服务,以调优系统性能
RHEL6建议ntpd服务,RHEL7建议时间同步chronyd服务,用了系统角色后,管理员不需要维护服务的配置文件,管理员可以使用timesync角色来弄时间同步。

2.2 安装系统角色

安装的系统角色默认放在/usr/share/ansible/roles下
[root@master2 ~]# yum -y install rhel-system-roles 
[root@master2 roles]# pwd
/usr/share/ansible/roles
[root@master2 roles]# ls
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

2.3 角色文档

每个角色都含一个文档,含角色的说明,角色的用法说明。
[root@master2 rhel-system-roles.timesync]# pwd
/usr/share/ansible/roles/rhel-system-roles.timesync
[root@master2 rhel-system-roles.timesync]# less README.md 
``yaml
- hosts: targets
  vars:
    timesync_ntp_servers:
      - hostname: foo.example.com
        iburst: yes                            // 文档的一个帮助示例   
      - hostname: bar.example.com
        iburst: yes
      - hostname: baz.example.com
        iburst: yes
  roles:
    - rhel-system-roles.timesync

2.4 timesync系统角色实例

常规时间同步设置

配置文件:/etc/chrony.conf

server example.com iburst
// 安装系统角色
[root@master2 ~]# yum -y install rhel-system-roles 

// 把目录下的timesync角色 复制到/etc/ansible/下 取名timesync
[root@master2 ansible]# cp /usr/share/ansible/roles/rhel-system-roles.timesync/ . timesync -r
[root@master2 ansible]# mv rhel-system-roles.timesync/ timesync

// 配置time play
[root@master2 ansible]# cat time.yml 
---
- hosts: "centos1"  
  vars:                         
    timesync_ntp_servers:            
      - hostname: time1.aliyum.com        
        iburst: yes                 
  roles:
    - timesync                       // 引用系统角色

2.5 控制执行程序

角色执行之前可定义一个pre_tasks,先执行特定的任务,再执行roles任务,有post_tasks则后执行,最后执行handlers任务通知任务.

也可以将普通任务加入到roles中,include_role动态导入 (2.3版本增加的) import_roles静态导入(2.4版本增加的)

2.6 selinux角色功能

该角色可以执行的任务:

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

selinux系统角色实例:

  • Lenforcing------>disabled
// 先查看被控主机selinux文件,以及显示的状态
[root@centos1 ~]# cat /etc/selinux/config 
# 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=enforing
# 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
[root@centos1 ~]# getenforce 0
Permissive


// 将selinux系统角色放置/etc/ansible/下并取名"selinux"
[root@master2 ansible]# cp /usr/share/ansible/roles/rhel-system-roles.selinux/  -r .
[root@master2 ansible]# mv rhel-system-roles.selinux  selinux

// 开始配置任务
[root@master2 ansible]# cat selinux.yml 
---
- hosts: centos1
  vars:
    selinux_policy: targeted
    selinux_state: disabled

  roles:
    - selinux
    
    
// 再次查看被控主机selinux状态
[root@centos1 ~]# getenforce 0
Disabled
# 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. 角色创建

在ansible中创建角色不需要特别的开发工具。创建和使用角色包含五个步骤:

  • 1.创建角色目录结构,构架角色的框架
  • 2.编写角色内容/定义角色内容
  • 3.定义角色依赖项,角色依赖项使得角色可以将其他角色作为依赖项包含在内
  • 4.在playbook中使用角色
  • 5.通过变量更改角色行为,让角色更加通用

角色的创建分为两种

第一种是通过手动创建roles服务目录,进入目录分别创建相应功能的目录

第二种是通过命令一键自动生成所有的目录
[root@master2 roles]# ansible-galaxy init apache_roles
- Role apache_roles was created successfully
[root@master2 roles]# tree apache_roles/
apache_roles/
├── defaults
│   └── main.yml
├── files                                  // 第二种方法
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

8 directories, 8 files
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值