打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
ansible之利用角色简化playbook

角色结构描述

Ansible角色提供了一种方法,让用户能以通用的方式更加轻松地重复利用Ansible代码。通过打包的方式将任务归档至一起,更加灵活的调用

Ansible角色具有下列优点:

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

角色结构目录

  • role目录结构
[root@localhost roles]# tree user.example/user.example/├── defaults #默认值,可以修改的变量│   └── main.yml├── files #调用的静态文件(安装包等)├── handlers│   └── main.yml #处理程序定义├── meta│   └── main.yml #角色相关信息├── README.md├── tasks│   └── main.yml #角色任务定义├── templates #角色引用的模板定义├── tests│   ├── inventory│   └── test.yml # 角色里test.yml(查看运行是否正常,给用户演示如何使用)└── vars    └── main.yml 定义角色的变量值

定义变量和默认值

  • vars/main.yml定义变量
  • defaults/main.yml 定义刻意需要覆盖的变量

调用角色

  • 调用角色
//用到roles模块---- hosts: remote.example.com  roles:    - role1    - role2
  • 设置角色role2变量
---- hosts: remote.example.com  roles:    - role: role1    - role: role2      var1: val1      var2: val2
  • 和上面等效的语法
---- hosts: remote.example.com  roles:    - role: role1    - { role: role2, var1: val1, var2: val2 }

执行顺序

  • 按照任务列表执行
- name: Play to illustrate order of execution  hosts: remote.example.com  pre_tasks:    - debug:      msg: 'pre-task'      notify: my handler  roles:    - role1  tasks:    - debug:      msg: 'first task'      notify: my handler #notify任务是整个task运行完毕后触发  post_tasks:    - debug:      msg: 'post-task'      notify: my handler  handlers:    - name: my handler      debug:        msg: Running my handler
  • 动态包含角色,更灵活
- name: Execute a role as a task  hosts: remote.example.com  tasks:    - name: A normal task      debug:        msg: 'first task'    - name: A task to include role2 here      include_role: role2 #ansible2.3新增include_role,2.4新增import_role

红帽企业自带系统角色

自RHEL7.4开始,操作系统随附了多个Ansible角色,作为rhel-system-roles软件包的一部分。在RHEL8中,该软件包可以从AppStream中获取。

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服务,以调优系统性能

目的

在多个版本之间标准化红帽企业Linux子系统的配置。使用系统角色来配置版本6.10及以上的任何红帽企业Linux主机

简化配置管理

通过系统角色实现不同版本主机配置

安装rhel系统角色

  • 安装
[root@node0 ~]# yum -y install rhel-system-roles...Installed:  rhel-system-roles-1.0-20.el8.noarch                                                                                                            Complete!
  • 查看已安装的系统角色
[root@node0 ~]# ls /usr/share/ansible/roles/linux-system-roles.certificate      linux-system-roles.network     rhel-system-roles.kdump            rhel-system-roles.postfixlinux-system-roles.kdump            linux-system-roles.postfix     rhel-system-roles.kernel_settings  rhel-system-roles.selinuxlinux-system-roles.kernel_settings  linux-system-roles.selinux     rhel-system-roles.logging          rhel-system-roles.storagelinux-system-roles.logging          linux-system-roles.storage     rhel-system-roles.metrics          rhel-system-roles.timesynclinux-system-roles.metrics          linux-system-roles.timesync    rhel-system-roles.nbde_client      rhel-system-roles.tloglinux-system-roles.nbde_client      linux-system-roles.tlog        rhel-system-roles.nbde_serverlinux-system-roles.nbde_server      rhel-system-roles.certificate  rhel-system-roles.network

默认路径包含/usr/share/ansible/roles/,playbook引用时就能找到

[root@node0 ~]# vim /etc/ansible/ansible.cfg ...#roles_path    = /etc/ansible/roles #取消注释后更改路径就可能找不到了

访问系统角色文档

[root@node0 ~]# ll /usr/share/doc/rhel-system-roles/total 4drwxr-xr-x. 2 root root   57 Feb 23 22:45 certificatedrwxr-xr-x. 2 root root   57 Feb 23 22:45 kdumpdrwxr-xr-x. 2 root root   72 Feb 23 22:45 kernel_settingsdrwxr-xr-x. 2 root root   72 Feb 23 22:45 loggingdrwxr-xr-x. 2 root root   57 Feb 23 22:45 metricsdrwxr-xr-x. 2 root root   57 Feb 23 22:45 nbde_clientdrwxr-xr-x. 2 root root   57 Feb 23 22:45 nbde_serverdrwxr-xr-x. 2 root root 4096 Feb 23 22:45 networkdrwxr-xr-x. 2 root root   57 Feb 23 22:45 postfixdrwxr-xr-x. 2 root root   93 Feb 23 22:45 selinuxdrwxr-xr-x. 2 root root   57 Feb 23 22:45 storagedrwxr-xr-x. 2 root root  136 Feb 23 22:45 timesyncdrwxr-xr-x. 2 root root   57 Feb 23 22:45 tlog

创建角色

从创建到使用分三步

  1. 创建角色目录结构
  2. 定义角色内容
  3. 在playbook中使用角色

创建角色目录结构

ansible-galaxy init命令创建新角色的目录结构

[root@node0 project]# cd roles/[root@node0 roles]# ansible-galaxy init test #创建一个名为“测试”的角色- Role test was created successfully[root@node0 roles]# lltotal 0drwxr-xr-x. 10 root root 154 Feb 24 10:34 test[root@node0 roles]# tree test/ #查看test的目录结构test/├── defaults│   └── main.yml├── files├── handlers│   └── main.yml├── meta│   └── main.yml├── README.md├── tasks│   └── main.yml├── templates├── tests│   ├── inventory│   └── test.yml└── vars    └── main.yml

定义角色内容

[root@node0 test]# vim tasks/main.yml ---# tasks file for test 定义创建用户任务- name: useradd  user:    name: {{ name }}  #引用变量1    system: {{ state }} #引用变量2

在playbook中使用角色

添加tom用户

[root@node0 project]# cat test.yml ---- hosts: node1   gather_facts: no  remote_user: root  vars:     name: tom #作为变量嵌套在play的vars关键字中定义    state: true  roles:  #roles模块引用test角色    - test//运行[root@node0 project]# ansible-playbook test.yml [WARNING]: Found variable using reserved name: namePLAY [node1] ************************************************************************************************************************************TASK [test : useradd] ***************************************************************************************************************************changed: [node1]PLAY RECAP **************************************************************************************************************************************node1                      : ok=1    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
//node1验证[root@node1 ~]# id tom uid=991(tom) gid=988(tom) groups=988(tom)

通过变量更改角色行为

  • 在play的roles关键字中包含该角色时作为变量定义
[root@node0 project]# vim test.yml ---- hosts: node1  gather_facts: no  remote_user: root  roles:    - test      name: tom      state: true
  • 作为变量嵌套在play的vars关键字中定义
[root@node0 project]# cat test.yml ---- hosts: node1   gather_facts: no  remote_user: root  vars:     name: tom     state: true  roles:      - test
  • 在清单文件中定义,作为主机变量或组变量
[root@node0 project]# vim inventory [lamp]node1 name=tom state=falsenode2node3[root@node0 project]# vim test.yml ---- hosts: node1  gather_facts: no  remote_user: root  roles:    - test[root@node0 project]# ansible-playbook test.yml [root@node1 ~]# id tomuid=1000(tom) gid=1000(tom) groups=1000(tom)
  • 在playbook项目的group_vars或host_vars目录下的YAML文件中定义
[root@node0 project]# mkdir host_vars[root@node0 project]# vim host_vars/node1.ymlname: tomstate: true[root@node0 project]# ansible-playbook test.yml [root@node1 ~]# id tomuid=991(tom) gid=988(tom) groups=988(tom)
来源:https://www.icode9.com/content-4-868401.html
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Ansible
自动化运维工具Ansible之Roles角色详解
集群运维自动化工具ansible之使用playbook安装zabbix客户端
Jenkins+Ansible+GitLab持续交付平台搭建-第4篇
243张图片为你解析Linux轻量级自动运维化工具Ansible
Ansible超详细使用指南
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服