ansible自动化-系统角色实例<十>

1ansible角色介绍

ansible角色提供了一种功能,让用户以通用的方式轻松地重复利用ansible代码,可以在传统目录结构中打包所有的环境,文件,变量,模板等其它资源,只需要复制一个目录,roles角色调用即可。

ansible角色可以从playbook中传递调整其行为的变量,设置相关的主机名。ip地址。用户名等详细信息,数据库自定义的其它参数就是,设定合理的default目录下的默认值。

简单来说:roles根据目录层次,分别将变量,文件,模块处理器,放置单独的目录中,可以include它们的一种机制。

1.2ansible角色优点
  • 角色可以把内容分组,从而与其他人共享代码
  • 可以编写角色定义系统类型的基本要素,web服务器,数据库服务器,git存储库等
  • 角色使用大项目容易管理
  • 角色项目可由不同人员

可以从Ansible Galaxy网站获取由社区提供支持的许多角色

地址:https://galaxy.ansible.com/

列出所有安装的galaxy :ansible-galaxy list


1.3官方角色目录结构
      site.yml
      webservers.yml
      fooservers.yml
      roles/
        common/
         tasks/ 			#定义角色的任务列表,此文件可以调用include对play包含,有一个主要main.yml
        handlers/           #定义角色中触发条件时执行的动作,包含一个main.yml
         files/			    #存放copy模块或script模块调用的文件
         templates/		    #存放jinjia2模块,template模块会自动在此目录中寻找j2模块
         vars/				#定义角色用到的变量,包含一个main.yml
         defaults/		    #当前角色设定的默认变量,当没定义变量时,使用此默认变量,含一个main,yml
         meta/  			#定义角色的特殊设定(作者,许可证,平台信息)以及依赖关系,含一个main.yml

 ​        tests/    			#此目录包含清单或者测试的play,用于测试角色	

        webservers/
         tasks/
         defaults/
         meta/

1.4定义变量和默认值

角色变量文件通常是/vars/main.yml文件定义,角色变量在yml文件中引用:{{ VAR_NAME }},优先级较高,给角色内部使用。

默认变量是为play设置默认变量,通过default/main.yml文件定义,优先级最低,这些变量能让角色准确自定义或控制,正确配置相应任务。

注意:

1.角色是自由共享的,调用的,不应该写入不相关的无用信息

2.角色内不要含任何机密,密码,私钥

3.play角色变量可以提供机密


1.5变量的优先级
优先级从高到低
1.角色内嵌参数,事实,include_vars加载的变量,注册的变量
2.vars/main.yml中定义的变量
3.playbook中vars部分定义的变量
4.主机变量,主机组变量
5.default/main.yml中定义的变量
1.6角色的简单使用
---
- hosts: examplte.com
    remote_user: root
  roles:
    - mysql
    - nginx
    - memcached

针对每个角色,角色任务,角色处理程序,角色变量等会按照顺序导入playbook内,角色内的copy模块,script脚本,template模块,或者导入包含的任务(import_tasks include_tasks)都可引入角色中,不需要绝对和相对路径

在角色内使用的变量,会将vars/main.yml和defaults/main.yml覆盖

注意:不要重复使用设置在play中任何其它位置的任何角色变量的名称

1.7传递变量角色

---
- hosts: examplte.com
  remote_user: root
  roles:
    - role: mysql
    - role: apache
      username: username
      package: package

2.系统角色

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

2.1RHEL系统角色描述

名称| 状态| 角色描述|

rhel-system-roles.kdump全面支持配置kdump崩溃恢复服务
rhel-system-roles.timesync全面支持使用网络时间协议或精确时间协议配置时间同步
rhel-system-roles.selinux全面支持配置和管理SELinux自定义, 包括SELinux模式、文件和端口上下文、 布尔值设置以及SELinux用户
rhel-system-roles.firewall开发中配置主机的防火墙
rhel-system-roles.network全面支持配置网络接口
rhel-system-roles.tuned开发中配置tuned服务,以调优系统性能系统角色的目的是在多个版本之间标准化红帽企业Linux子系统的配置。使用系统角色来配置版本6.10及以上的任何红帽企业Linux主机。
rhel-system-roles.postfix技术预览使用Postfix服务将每个主机配置为邮件传输代理
rhel-system-roles.storage技术预览硬盘

RHEL6建议ntpd服务,RHEL7建议时间同步chronyd服务,用了系统角色后,管理员不需要维护服务的配置文件,管理员可以使用timesync角色来弄时间同步。

2.2安装系统角色

安装的系统角色默认放在/usr/share/ansible/roles下

[root@clq ~]# yum -y install rhel-system-roles 
[root@clq roles]# pwd
/usr/share/ansible/roles
[root@clq 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@clq rhel-system-roles.timesync]# pwd
/usr/share/ansible/roles/rhel-system-roles.timesync
[root@clq 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.4timesync系统角色实例

常规时间同步设置

配置文件:/etc/chrony.conf

server example.com iburst

#安装系统角色
[root@clq ~]# yum -y install rhel-system-roles 
#将目录下的timesync角色 复制到/etc/ansible/下 取名timesync
[root@clq ansible]# cp /usr/share/ansible/roles/rhel-system-roles.timesync/ . timesync -r
[root@clq ansible]# mv rhel-system-roles.timesync/ timesync
#配置time play
[root@clq ansible]# cat time.yml 
---
- hosts: "web"   #对端主机
  vars:                         
    timesync_ntp_servers:            #引用变量 README.md内能查看到
      - hostname: time1.aliyum.com   #同步的NTP服务器的主机名        
        iburst: yes                  #布尔值,用于启动或禁用快速初始同步 角色中默认为no
   
  roles:
    - timesync                       #引用系统角色

2.5控制执行顺序

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

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


2.6selinux角色功能

该角色可以执行的任务:

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

enforcing------>disabled实例

#先查看受管本机selinux文件,以及显示的状态
[root@mysql01 ~]# 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@mysql01 ~]# getenforce 0
Permissive


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

#配置selinux角色任务
[root@clq ansible]# cat selinux.yml 
---
- hosts: web
  vars:
    selinux_policy: targeted
    selinux_state: disabled

  roles:
    - selinux
    
    
 #再次查看受管主机selinux状态
[root@mysql01 ~]# 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

disabled------>enforcing实例

[root@master ansible]# cat selinux.yml 
---
- hosts: 192.168.136.145
  vars:
    selinux_state: enforcing
  tasks:
    - name: selinux 
      block:
        - include_role:
            name: selinux
      rescue:
        - name: failed reason require reboot
          fail:
          when: not selinux_reboot_required

        - name: reboot
          reboot:
        - name: config selinux
          include_role:
             name: selinux 
#查看受管主机状态            
[root@servera ~]# getenforce 0
Enforcing

httpd改变端口,不关闭防火墙实现访问实例:

[root@master ansible]# cat httpd.yml 
---
- hosts: web
  vars:
    selinux_state: enforcing     #引入角色变量
    selinux_ports:
      - ports: "{{ PORT }}"          #变量中的变量端口
        setype: 'httpd_port_t'       #设置类型
        state: 'present'             #开启
        proto: 'tcp'                 #协议类型
    PORT: 84                      #变量端口数字
  tasks:
    - name: install httpd
      yum: 
        name: httpd
        state: present

    - name: config httpd.j2
      template:
        src: files/httpd.conf.j2
        dest: /etc/httpd/conf/httpd.conf
      notify: restart httpd                #触发重启服务更新
     
    - name: selinux for httpd    
      block:              #block语句,此任务错误才会执行rescue语句
        include_role:                         #引入selinux系统角色
          name: selinux
      rescue:                                    
        - name: failed reason require reboot        #失败则会重启,因为disabled会重启
          fail:
          when: not selinux_reboot_required

        - name: reboot
          reboot:

      - name: config selinux
        include_roles:
          name: selinux        
    - name: service for httpd
      service:
        name: httpd
        state: started
        enabled: yes
  handlers:
    - name: restart httpd
      service:
        name: httpd
        state: restarted
 #查看j2模块       
cat /etc/ansible/files/httpd.conf.j2
#Listen 12.34.56.78:80
Listen {{ PORT }}

注意:permissive-------->disabled 需要重启
​ permissive--------->enforcing不需要重启


2.7存储系统角色实现硬盘分区
#受管主机准备一块10G硬盘
[root@mysql01 ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   20G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   19G  0 part 
  ├─cs-root 253:0    0   17G  0 lvm  /
  └─cs-swap 253:1    0    2G  0 lvm  [SWAP]
sdb           8:16   0   20G  0 disk 
sr0          11:0    1  9.1G  0 rom  
#控制主机配置    
[root@clq ansible]# cp /usr/share/ansible/roles/rhel-system-roles.storage/README.md .
[root@clq ansible]# mv README.md storage.yml
[root@clq ansible]# vim storage.yml              #前面1-192行全部删掉
  roles:
    - name: rhel-system-roles.storage 
      storage_pools:                                 #存储池
        - name: APP                               
          disks:                                     #磁盘任务类型
            - sdb
          volumes:                                   #卷含{大小,名字,挂载点,格式化类型}
            - name: sdb01           
              size: "8 GiB"
              mount_point: "/mnt/APP/sdb01"                    
              fs_type: xfs
              state: present
            - name: vdb02
              size: "8 GiB"
              mount_point: "/mnt/APP/sdb02"
              fs_type: ext4
              state: present
#运行storage角色
[root@clq ansible]# ansible-playbook storage.yml 
#受管主机查看分区情况
  [root@mysql01 ~]# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0   20G  0 disk 
├─sda1        8:1    0    1G  0 part /boot
└─sda2        8:2    0   19G  0 part 
  ├─cs-root 253:0    0   17G  0 lvm  /
  └─cs-swap 253:1    0    2G  0 lvm  [SWAP]
sdb           8:16   0   20G  0 disk 
├─APP-vdb02 253:2    0    8G  0 lvm  /mnt/APP/sdb02
└─APP-sdb01 253:3    0    8G  0 lvm  /mnt/APP/sdb01
sr0          11:0    1  9.1G  0 rom  
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神慕蔡蔡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值