角色简化playbook

角色的定义

角色(roles)是ansible自1.2版本开始引入的新特性,用于层次性,结构化地组织playbook。roles能够根据层次型结构自动装载变量文件、tasks以及handlers等。要使用roles只需要在playbook中使用include指令即可。简单的说,roles就是通过分别将变量、文件、任务、模块及处理器放置于单独的目录中、并可以便捷地include他们的一种机制。角色一般用于基于主机构建服务的场景中、但也可以是用于构建守护进程等场景中。

Ansible角色具有下列优点:
  • 角色可以分组内容,从而与他人轻松共享代码
  • 可以编写角色来定义系统类型的基本要素:Web服务器、数据库服务器、Git存* 储库,或满足其他用途
  • 角色使得较大型项目更容易管理
  • 角色可以由不同的管理员并行开发

ansible角色子目录

子目录功能
defiayits此目录的mai.yml包含角色变量的默认值,使用角色可以覆盖默认值,变量比较低,应该在play中更改和自定义
files此目录包含角色任务引用的静态文件
handies此目录中的mai.yml 文件包含角色的处理程序
meta此目录中的mai.yml文件包含于角色相关的信息.作者,许可证
tasks此任务中的mai.yml文件包含与角色任务·定义
templates此目录包含角色任务引起jinja2模板
tests此目录可以包含角色清单和名为test.yml的playbook,可用于测试角色
vars此目录的mai.yml文件定义角色的变量值,这些变量常用与角色内部用途,这些变量的优先级较高,在playbook中使用时不应更改
红帽企业Linux系统角色

自RHEL7.4开始,操作系统随附了多个Ansible角色,作为rhel-system-roles软件包的一部分。在RHEL8中,该软件包可以从AppStream中获取。以下是每个角色的简要描述
红帽企业linux系统角色

名称状态角色描述
rhel-system-roles.kdump全面支持配置kddump崩溃恢复服务
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服务,以调优系统性能
安装系统角色
[root@localhost ~]# rpm -ivh /mnt/AppStream/Packages/rhel-system-roles-1.0-10.el8_1.noarch.rpm  
warning: /mnt/AppStream/Packages/rhel-system-roles-1.0-10.el8_1.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:rhel-system-roles-1.0-10.el8_1   ################################# [100%]
[root@localhost ~]# rpm -qa|grep rhel-system-roles
rhel-system-roles-1.0-10.el8_1.noarch
[root@localhost ~]# 

安装好的角色位于,RHEL系统角色位于/usr/share/ansible/roles目录中

[root@localhost ~]# ls /usr/share/ansible/roles/
linux-system-roles.kdump     rhel-system-roles.kdump
linux-system-roles.network   rhel-system-roles.network
linux-system-roles.postfix   rhel-system-roles.postfix
linux-system-roles.selinux   rhel-system-roles.selinux
linux-system-roles.storage   rhel-system-roles.storage
linux-system-roles.timesync  rhel-system-roles.timesync
[root@localhost ~]# 
访问RHEL系统角色的文档
[root@localhost ~]# ll /usr/share/doc/rhel-system-roles/
total 4
drwxr-xr-x 2 root root   57 Sep 15 15:04 kdump
drwxr-xr-x 2 root root 4096 Sep 15 15:04 network
drwxr-xr-x 2 root root   57 Sep 15 15:04 postfix
drwxr-xr-x 2 root root   93 Sep 15 15:04 selinux
drwxr-xr-x 2 root root   57 Sep 15 15:04 storage
drwxr-xr-x 2 root root  136 Sep 15 15:04 timesync
[root@localhost ~]# 

在playbook中使用ansible角色

时间同步角色
该角色的文档**/usr/share/doc/rhel-system-roles/timesync目录下的README.md**中

[root@localhost test]# vim playbook.yml
---
- hosts: 192.168.187.131
  vars:
      timesync_ntp_servers:
       - hostname: time1.aliyun.com
         iburst: yes
         minpoll: 3
  roles:
     - role: roles/timesync


[root@localhost ~]# ansible-playbook  playbook.yml 

PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [192.168.187.131]
ok: [192.168.187.130]
fatal: [192.168.187.129]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 192.168.187.129 port 22: No route to host", "unreachable": true}

TASK [install httpd] ***********************************************************
ok: [192.168.187.131]
changed: [192.168.187.130]

PLAY RECAP *********************************************************************
192.168.187.129            : ok=0    changed=0    unreachable=1    failed=0    skipped=0    rescued=0    ignored=0   
192.168.187.130            : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.187.131            : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@localhost ~]# date 
Tue Sep 15 15:53:32 CST 2020

控制执行顺序

在角色添加到play中后,角色任务将添加到任务列表的开头。如果play中包含第二个角色,其任务列表添加到第一个角色之后。
角色处理程序添加到play中的方式与角色任务添加到play中相同。每个play定义一个处理程序列表。角色处理程序先添加到处理程序列表,然后将play中的对应的handlers部分添加至任务列表。
在某些情形中,可能需要在角色之前执行一些play任务。若要支持这样的情形,可以为play配置pre_tasks部分。列在此部分中的所有任务将在执行任何角色之前执行。如果这些任务中有任何一个通知了处理程序,则这些处理程序任务也在角色或普通任务之前执行。play也支持post_tasks关键字。这些任务在play的普通任务和它们通知的任何处理程序运行之后执行。
一个play中通常不会同时包含所有这些部分

[root@localhost test]# vim playbook.yml
[root@localhost test]# ansible-playbook playbook.yml 

PLAY [all] *********************************************************************

TASK [pre_task] ****************************************************************
ok: [192.168.187.131] => {
    "msg": "hehe"
}

TASK [task] ********************************************************************
changed: [192.168.187.131]

RUNNING HANDLER [insert data] **************************************************
changed: [192.168.187.131]

TASK [post_task] ***************************************************************
fatal: [192.168.187.131]: FAILED! => {"changed": false, "cmd": "echopost", "msg": "[Errno 2] No such file or directory", "rc": 2}

PLAY RECAP *********************************************************************
192.168.187.131            : ok=3    changed=2    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

[root@localhost test]# vim playbook.yml

---
- hosts: all
---
- hosts: all
  gather_facts: no
  pre_tasks:
- hosts: all
  gather_facts: no
  pre_tasks:
    - name: pre_task
      debug:
         msg: "hehe"

      notify:
         - insert data
  tasks:
    - name: task
      command: "echo  'hehe'"
      notify:
        - insert data

  post_tasks:
    - name: post_task
      command: "echo'post'"
      notify:
        - insert data


  handlers:
       - name: insert data
         shell: "echo 123 >> /tmp/abc"
[root@localhost test]# mkdir roles
[root@localhost test]# cd roles/
[root@localhost roles]#  cp -a /usr/share/ansible/roles/rhel-system-roles.ti
[root@localhost roles]# ls
rhel-system-roles.timesync
[root@localhost roles]# cp -a /usr/share/ansible/roles/rhel-system-roles.network ./network
[root@localhost roles]# cd network/
[root@localhost network]# ls
defaults  LICENSE  module_utils  README.html  tasks  tox.ini
library   meta     pylintrc      README.md    tests
[root@localhost network]# less README.md 

timesync_ntp_servers属性
属性用途
hostname要与其同步的ntp服务器的主机
iburst一个布尔值,用于启用或安装禁用快速初始同步,在角色默认为no但通常应该将属性设为yes
时间同步角色
[root@localhost timesync]# less example-timesync-playbook.yml 

---
- hosts: "{{ target }}"
  vars:
    timesync_ntp_servers:
      - hostname: 0.pool.ntp.org
        iburst: yes
      - hostname: 1.pool.ntp.org
        iburst: yes
      - hostname: 2.pool.ntp.org
        iburst: yes
      - hostname: 3.pool.ntp.org
        iburst: yes
  roles:
    - rhel-system-roles.timesync
example-timesync-playbook.yml (END)
调用seliunx角色

seliunx角色必须确保重新引导受管主机,以来能够完整应用其更改。但是。他本身不会重新引导主机。如此一来。用户便可以控制重新引导的处理方式。

工作方式为 ,seliun角色将一个布尔值seliunx_reboot_required设为true,如果需要重新引导,则失败 可以使用block/rescre结构来从失败中恢复,具体操作:如果该变量m

[root@localhost test]# vim playbook.yml 

---
- hosts: all
  vars:
    selinux_policy: targeted
    selinux_state: enforcing
  tasks:
    - name: apply selinux
      block:
        - include_role:
              name: rhel-system-roles.selinux
      rescue:
         - name: jddh
           fail:
           when: not selinux_reboot_requirred

         - name: reboot
           reboot:

         - include_role:
              name: rhel-system-roles.selinux


[root@localhost test]# ansible-playbook playbook.yml 

PLAY [all] ********************************************************************************************

TASK [Gathering Facts] ********************************************************************************
ok: [192.168.187.130]

TASK [include_role : rhel-system-roles.selinux] *******************************************************

TASK [rhel-system-roles.selinux : Install SELinux python2 tools] **************************************
ok: [192.168.187.130]

TASK [rhel-system-roles.selinux : Install SELinux python3 tools] **************************************
skipping: [192.168.187.130]

TASK [rhel-system-roles.selinux : refresh facts] ******************************************************
ok: [192.168.187.130]

TASK [rhel-system-roles.selinux : Install SELinux tool semanage on Fedora] ****************************
skipping: [192.168.187.130]

TASK [rhel-system-roles.selinux : Set permanent SELinux state if enabled] *****************************
changed: [192.168.187.130]

TASK [rhel-system-roles.selinux : Set permanent SELinux state if disabled] ****************************
skipping: [192.168.187.130]

TASK [rhel-system-roles.selinux : Set ansible facts if needed] ****************************************
ok: [192.168.187.130]

TASK [rhel-system-roles.selinux : Fail if reboot is required] *****************************************
skipping: [192.168.187.130]

TASK [rhel-system-roles.selinux : debug] **************************************************************
skipping: [192.168.187.130]

TASK [rhel-system-roles.selinux : Drop all local modifications] ***************************************
skipping: [192.168.187.130]

TASK [rhel-system-roles.selinux : Purge all SELinux boolean local modifications] **********************
skipping: [192.168.187.130]

TASK [rhel-system-roles.selinux : Purge all SELinux file context local modifications] *****************
skipping: [192.168.187.130]

TASK [rhel-system-roles.selinux : Purge all SELinux port local modifications] *************************
skipping: [192.168.187.130]

TASK [rhel-system-roles.selinux : Purge all SELinux login local modifications] ************************
skipping: [192.168.187.130]

TASK [rhel-system-roles.sel inux : Reload SELinux policy] **********************************************
changed: [192.168.187.130]

TASK [rhel-system-roles.selinux : Set SELinux booleans] ***********************************************

TASK [rhel-system-roles.selinux : Set SELinux file contexts] ******************************************

TASK [rhel-system-roles.selinux : Restore SELinux labels on filesystem tree] **************************

TASK [rhel-system-roles.selinux : Restore SELinux labels on filesystem tree in check mode] ************

TASK [rhel-system-roles.selinux : Set an SELinux label on a port] *************************************

TASK [rhel-system-roles.selinux : Set linux user to SELinux user mapping] *****************************

PLAY RECAP ********************************************************************************************
192.168.187.130            : ok=6    changed=2    unreachable=0    failed=0    skipped=16   rescued=0    ignored=0  
[root@localhost ~]# getenforce 
Enforcing
[root@localhost ~]# 

[root@localhost ~]# 
[root@localhost ~]# getenforce 
Permissive
[root@localhost ~]# 

[root@localhost ~]# setenforce 1
[root@localhost ~]# getenforce 
Enforcing
[root@localhost ~]# 
root@localhost ~]# cd /usr/share/doc/rhel-system-roles/
[root@localhost rhel-system-roles]# ls
kdump  network  postfix  selinux  storage  timesync
[root@localhost rhel-system-roles]# cd timesync/
[root@localhost timesync]# ls
COPYING  example-timesync-playbook.yml  example-timesync-pool-playbook.yml  README.html  README.md
[root@localhost timesync]# vim README.md 
[root@localhost timesync]# tree README.md 
timesync_ntp_servers属性
属性用途
hostname要与其同步的ntp服务器的主机
iburst一个布尔值,用于启用或安装禁用快速初始同步,在角色默认为no但通常应该将属性设为

selinux_fcontext变量取一个要永久设置(或删除)的文件上下文的列表作为值。它的工作方式与selinux fcontent命令非常相似。
selinux_restore_dirs变量指定要对其运行restorecon的目录的列表

[root@localhost test]# vim play.yml 

---
- hosts: all
  vars:
     selinux_booleans:
       - name: httpd_enable_homedirs
         state: on
         persistent: yes
  roles:
     - role: rhel-system-roles.selinux
     - 
[root@localhost test]# ansible-playbook --syntax-check play.yml    //检查语法

playbook: play.yml

[root@localhost test]# ansible-playbook play.yml 

 

[root@localhost ~]# semanage boolean -l| grep -i  "httpd_enable_homedirs"
httpd_enable_homedirs          (on   ,   on)  Allow httpd to enable homedirs
[root@localhost ~]# 

查看受管主机上/opt/abscd目录下文件的安全上下文

[root@localhost test]# vim play.yml 

---
- hosts: all
  vars:
     selinux_fcontexts:
       - target: /opt/abscd/wo
         setype: httpd_sys_content_t
         state: present
     selinux_restore_dis:
  roles:
     - role: rhel-system-roles.selinux

[root@localhost ~]# ls -Z /opt/abscd /wo
-rw-r--r--. root root unconfined_u:object_r:usr_t:s0   /opt/abscd
-rw-r--r--. root root unconfined_u:object_r:etc_runtime_t:s0 /wo

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

[root@localhost test]# vim play.yml 

---
- hosts: all
  vars:
    selinux_ports:
       - setype: http_port_t
         ports: 82
         state: present
         proto: tcp

  roles:
        - role: rhel-system-roles.selinux
//查看受管主机上的http_port_t的端口号
[root@localhost ~]# semanage port -l |grep http
http_cache_port_t              tcp      8080, 8118, 8123, 10001-10010
http_cache_port_t              udp      3130
http_port_t                    tcp      82, 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t            tcp      5988
pegasus_https_port_t           tcp      5989
[root@localhost ~]# 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值