ansible中的roles角色的使用

ansible中的roles角色的使用

roles角色

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

建议使用roles在复杂的运维场景,代码的复用度高

roles目录结构

img

roles各目录作用

  • /roles/*:项目名称,有各个子项目

  • files/ :存放由copy或script模块等调用的文件

  • templates/:template模块查找所需要模板文件的目录

  • tasks/:定义task,role的基本元素,至少应该包含一个名为main.yml的文件;其它的文件需要在main.yml文件中通过include进行包含

  • handlers/:至少应该包含一个名为main.yml的文件;其它的文件需要在此文件中通过include进行包含

  • vars/:定义变量,至少应该包含一个名为main.yml的文件;其它的文件需要在此文件中通过include进行包含

  • meta/:定义当前角色的特殊设定及其依赖关系,至少应该包含一个名为main.yml的文件,其它文件需在此文件中通过include进行包含

  • default/:设定默认变量时使用此目录中的main.yml文件

创建role

创建role的步骤

(1) 创建以roles命名的目录

(2) 在roles目录中分别创建以各角色名称命名的目录,如webservers等

(3) 在每个角色命名的目录中分别创建files、handlers、meta、tasks、templates和vars目录;用不到
的目录可以创建为空目录,也可以不创建

(4) 在playbook文件中,调用各角色

roles中tags的使用

#nginx-role.yml
---
- hosts: websrvs
remote_user: root
roles:
- { role: nginx ,tags: [ 'nginx', 'web' ] ,when:ansible_distribution_major_version == "6“ }  #给nginx角色创建nginx和web两个标签大,但只在centos6上才能运行
- { role: httpd ,tags: [ 'httpd', 'web' ] }  #给httpd创建httpd和web两个标签
- { role: mysql ,tags: [ 'mysql', 'db' ] }   #为mysql角色创建mysql和db两个标签
- { role: mariadb ,tags: [ 'mariadb', 'db' ] }  #为mariadb角色创建nariadb和db两个标签

运行ansible-playbook -t httpd nginx-role.yml时表示只运行httpd角色

运行ansible-playbook -t db nginx-role.yml表示运行mysqlmariadb两个角色

运行ansible-playbook nginx-role.yml表示运行所有角色

范例

范例一:实现httpd角色

ansible主控端:192.168.38.102 centos8

远程服务器node1:192.168.38.103 centos8

远程服务器node2:192.168.38.107 centos7

创建角色目录:

在ansible服务器创建角色相关目录,角色目录可以放任意目录下,yum安装的ansible,会有一个初始角色路径,/etc/ansible/roles

[root@centos8 ~]# mkdir -pv /data/ansible/roles/httpd/{tasks,handlers,files,templates,vars}
mkdir: created directory '/data/ansible'
mkdir: created directory '/data/ansible/roles'
mkdir: created directory '/data/ansible/roles/httpd'
mkdir: created directory '/data/ansible/roles/httpd/tasks'
mkdir: created directory '/data/ansible/roles/httpd/handlers'
mkdir: created directory '/data/ansible/roles/httpd/files'
mkdir: created directory '/data/ansible/roles/httpd/templates'
mkdir: created directory '/data/ansible/roles/httpd/vars'
[root@centos8 ~]# cd /data/ansible/
[root@centos8 ansible]# tree 
.
└── roles
    └── httpd
        ├── files
        ├── handlers
        ├── tasks
        ├── templates
        └── vars

7 directories, 0 files
创建角色tasks文件
  • 首先第一个为main.yml文件,指定要引用角色的顺序:
[root@centos8 httpd]# vim tasks/main.yml

- include: install.yml
- include: config.yml
- include: index.yml
- include: service.yml
  • 创建install.yml文件,此文件为安装htppd
[root@centos8 httpd]# vim tasks/install.yml

- name: install httpd packge
  yum: name=httpd
  • 创建config.yml文件,此文件为修改httpd配置文件,添加notify实现修改配置文件,自动重启服务
[root@centos8 httpd]# vim tasks/config.yml

- name: config file
  copy: src=httpd.conf dest=/etc/httpd/conf/ backup=yes
  notify: restart
  • 创建index.html文件
[root@centos8 httpd]# vim tasks/index.yml

- name: index.html
  copy: src=index.html dest=/var/www/html/
  • 创建service.yml文件启动httpd服务
[root@centos8 httpd]# vim tasks/service.yml

- name: start service
  service: name=httpd state=started enabled=yes
  • 创建handlers文件,实现修改配置后自动重启
[root@centos8 httpd]# vim handlers/main.yml

- name: restart
  service: name=httpd state=restarted
准备files目录下的文件
  • 准备httpd.conf文件
[root@centos8 httpd]# yum install httpd -y
[root@centos8 httpd]# cp /etc/httpd/conf/httpd.conf files/
[root@centos8 httpd]# vim files/httpd.conf 
#修改端口
Listen 8080
  • 准备index.html文件
[root@centos8 httpd]# vim files/index.html

<h1>www.wj.com</h1>

完整文件结构:

[root@centos8 httpd]# tree /data/ansible/roles/httpd/
/data/ansible/roles/httpd/
├── files
│   ├── httpd.conf
│   └── index.html
├── handlers
│   └── main.yml
├── tasks
│   ├── config.yml
│   ├── index.yml
│   ├── install.yml
│   ├── main.yml
│   └── service.yml
├── templates
└── vars

5 directories, 8 files
在playbook中调用角色
[root@centos8 httpd]# vim /data/ansible/role_httpd.yml

---
- hosts: nodes
  remote_user: root
  
  roles:
    - role: httpd
运行piaybook
[root@centos8 httpd]# ansible-playbook /data/ansible/role_httpd.yml 

PLAY [nodes] *******************************************************************

TASK [Gathering Facts] *********************************************************
ok: [192.168.38.103]
ok: [192.168.38.107]

TASK [httpd : install httpd packge] ********************************************
changed: [192.168.38.103]
changed: [192.168.38.107]

TASK [httpd : config file] *****************************************************
changed: [192.168.38.107]
changed: [192.168.38.103]

TASK [httpd : index.html] ******************************************************
changed: [192.168.38.107]
changed: [192.168.38.103]

TASK [httpd : start service] ***************************************************
changed: [192.168.38.103]
changed: [192.168.38.107]

RUNNING HANDLER [httpd : restart] **********************************************
changed: [192.168.38.107]
changed: [192.168.38.103]

PLAY RECAP *********************************************************************
192.168.38.103             : ok=6    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.38.107             : ok=6    changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@centos8 httpd]# 
测试
[root@centos8 httpd]# curl 192.168.38.103:8080
<h1>www.wj.com</h1>
[root@centos8 httpd]# curl 192.168.38.107:8080
<h1>www.wj.com</h1>
[root@centos8 httpd]# 
Ansible ,你可以使用标签(tags)和角色roles)来组织和管理你的任务和配置。 1. 标签(Tags):标签是用来给任务或者一组任务打上标记,以便在运行时选择性地执行这些任务。你可以在 playbook 为每个任务指定一个或多个标签。例如: ``` - name: 安装和配置 Apache hosts: web_servers tasks: - name: 安装 Apache yum: name: httpd state: present tags: - install - web - name: 配置 Apache template: src: apache.conf.j2 dest: /etc/httpd/conf/httpd.conf tags: - config - web ``` 在运行 playbook 时,你可以使用 `--tags` 参数来指定要运行的标签。例如,`ansible-playbook playbook.yml --tags "install"` 将只运行带有 "install" 标签的任务。 2. 角色Roles):角色是一种更高级别的组织方式,可以将相关的任务、变量文件组合成一个可重用的模块。一个角色可以包含多个任务和文件,这样你就可以将复杂的配置逻辑封装起来,并在多个 playbook 重用。通常,一个角色会有一个独立的目录结构,包含 `tasks`、`vars`、`files` 等子目录。 首先,你需要创建一个角色目录结构。可以使用 `ansible-galaxy` 命令来初始化一个新的角色目录,例如: ``` ansible-galaxy init myrole ``` 然后,在 playbook 使用 `roles` 关键字来引用一个角色。例如: ``` - name: 应用角色到服务器 hosts: web_servers roles: - myrole ``` 当运行 playbook 时,Ansible 将自动查找和执行该角色的任务和配置。 希望这些信息对你有所帮助!
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值