Ansible角色提供了一种方法,让用户能以通用的方式更加轻松地重复利用Ansible代码。通过打包的方式将任务归档至一起,更加灵活的调用
Ansible角色具有下列优点:
[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 定义角色的变量值
//用到roles模块---- hosts: remote.example.com roles: - role1 - 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主机
通过系统角色实现不同版本主机配置
[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
从创建到使用分三步
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
添加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)
[root@node0 project]# vim test.yml ---- hosts: node1 gather_facts: no remote_user: root roles: - test name: tom state: true
[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)
[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
联系客服