Ansible自动化运维|第3章:roles 详解

  • 原创
  • Madman
  • /
  • /
  • 0
  • 9666 次阅读

ansible-min.png

Synopsis: Ansible 在 1.2 版本以后开始支持 role,它与我们日常使用的 playbook 没有什么区别,只不过对目录结构进行了一些规范。我们可以对一个很复杂的 playbook 进行拆分,比如将任务都放到 tasks/ 目录中、将触发处理程序都放到 handlers/ 目录中等。如果以后需要对 role 进行修改或者调整,只需修改相应的文件即可。如果还想把这个 role 分享给其它人,也只需要分享整个目录即可,从而实现重用的目的。role 只是关于一个功能的集合,我们使用它时,只需要编写一个 playbook 通过 roles 关键字去调用我们写好的 role 即可

1. 创建 role

可以通过 ansible-galaxy init 命令使用 role 模板来初始化一个新的 role,默认会帮你创建出 tasks/handlers/vars/ 等目录

[root@CentOS ~]# ansible-galaxy init --offline /etc/ansible/roles/nginx
- /etc/ansible/roles/nginx was created successfully

[root@CentOS ~]# tree /etc/ansible/roles/nginx
/etc/ansible/roles/nginx
├── defaults
│   └── main.yml
├── files
├── handlers
│   └── main.yml
├── meta
│   └── main.yml
├── README.md
├── tasks
│   └── main.yml
├── templates
├── tests
│   ├── inventory
│   └── test.yml
└── vars
    └── main.yml

8 directories, 8 files

上面的示例中是将 nginx role 存放到 /etc/ansible/roles 目录中,当然你也可以在当前目录 ${PWD} 下新建 roles/ 目录

role 的搜索路径:

  • Ansible 首先会从当前目录下的 roles/ 目录中查找有没有指定名称的 role
  • 如果没找到的话,就继续到角色的默认位置 /etc/ansible/roles 目录中查找(可以通过修改配置文件 /etc/ansible/ansible.cfg 中的 roles_path 参数来更改角色的默认保存位置)

2. role 目录结构

  • tasks: 包含角色要执行的主要任务列表
  • handlers: 包含任务的触发处理程序
  • defaults: 角色的默认变量
  • vars: 角色的其它变量
  • files: 包含可以通过此角色部署的静态文件,比如由 copyscript 模块调用的文件
  • templates: 包含可以通过此角色部署的模板,比如由 template 模块调用的模板文件
  • meta: 为此角色定义一些元数据,比如将角色分享到 Ansible Galaxy 时的说明信息 galaxy_info(作者名、角色描述、License 等)、角色的依赖关系 dependencies(允许您在使用角色时自动引入其他角色)

注意:filestemplates 以外的每个目录必须包含一个 main.yml 文件,如果上述某些目录你不会用到时,可以不创建或者创建为空目录

3. 快速预览

nginx role 完整角色已上传到 Github: https://github.com/wangy8961/ansible-role-nginx

在前两篇文章中,如果我们要安装并启动 nginx 服务,会使用如下的 playbook:

---
- hosts: webservers
  remote_user: root
  tasks:
    - name: Install Nginx
      yum: name=nginx state=latest
    - name: Copy main configure file (change workers number)
      template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
      notify:
        - restart nginx
        - check nginx process
    - name: Copy vhost configure file
      copy: src=default.conf dest=/etc/nginx/conf.d/default.conf
      notify:
        - restart nginx
        - check nginx process
    - name: Copy index.html file
      copy: src=index.html dest=/var/www/html/index.html
    - name: Start Nginx service
      service: name=nginx state=started enabled=yes
  handlers:
    - name: restart nginx
      service: name=nginx state=restarted
    - name: check nginx process
      shell: killall -0 nginx > /tmp/nginx.log

现在我们将它转换成 nginx role,大概的目录结构如下:

site.yml
roles/
  nginx/
    tasks/
    handlers/
    files/
    templates/
    vars/
    defaults/
    meta/

3.1 tasks

将所有任务 tasks 都移到 /etc/ansible/roles/nginx/tasks/ 目录下,可以全部放到 main.yml 中:

[root@CentOS ~]# cat /etc/ansible/roles/nginx/tasks/main.yml
---
- name: Install Nginx
  yum: name=nginx state=latest
- name: Copy main configure file (change workers number)
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
  notify:
    - restart nginx
    - check nginx process
- name: Copy vhost configure file
  copy: src=default.conf dest=/etc/nginx/conf.d/default.conf
  notify:
    - restart nginx
    - check nginx process
- name: Copy index.html file
  copy: src=index.html dest=/var/www/html/index.html
- name: Start Nginx service
  service: name=nginx state=started enabled=yes

或者,将任务进行更细的拆分到多个 YAML 文件中,然后在 main.yml 中通过 import_tasksinclude_tasks 来导入或包含,它俩的区别为:

  • All import* statements are pre-processed at the time playbooks are parsed,属于 静态(static) 导入,比如 import_tasksimport_playbookimport_role
  • All include* statements are processed as they are encountered during the execution of the playbook,属于 动态(dynamic) 包含,比如 include_tasksinclude_playbookinclude_role

注意: 在 Ansible 2.4 之前,只能使用 include 关键字来包含

1. install.yml
[root@CentOS ~]# cat /etc/ansible/roles/nginx/tasks/install.yml
---
- name: Install Nginx
  yum: name=nginx state=latest

2. main-configure.yml
[root@CentOS ~]# cat /etc/ansible/roles/nginx/tasks/main-configure.yml
---
- name: Copy main configure file (change workers number)
  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
  notify:
    - restart nginx
    - check nginx process

3. vhost-configure.yml
[root@CentOS ~]# cat /etc/ansible/roles/nginx/tasks/vhost-configure.yml
---
- name: Copy vhost configure file
  copy: src=default.conf dest=/etc/nginx/conf.d/default.conf
  notify:
    - restart nginx
    - check nginx process

4. index-page.yml
[root@CentOS ~]# cat /etc/ansible/roles/nginx/tasks/index-page.yml
---
- name: Copy index.html file
  copy: src=index.html dest=/var/www/html/index.html

5. service.yml
[root@CentOS ~]# cat /etc/ansible/roles/nginx/tasks/service.yml
---
- name: Start Nginx service
  service: name=
                                
                            
  • 1815207013
  • alice
  • 31787219
  • tenglongfly
  • 13373413
  • henry
  • baudilio
  • frederick
  • waunita
  • september
  • stepheno
  • walsie
  • anastasiya
  • jametra
  • daana
  • jaali
  • mally
  • alicya
  • stokes
  • jandell
  • dvejra
  • calvina
  • brysonn
  • kilburn
未经允许不得转载: LIFE & SHARE - 王颜公子 » Ansible自动化运维|第3章:roles 详解

分享

作者

作者头像

Madman

如需 Linux / Python 相关问题付费解答,请按如下方式联系我

0 条评论

暂时还没有评论.

专题系列