zoukankan      html  css  js  c++  java
  • Ansible--roles

    简介

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

    目录结构

    roles的模块目录分类:
    files 目录          # 存放copy和script模块等调用的文件
    templates 目录      # template模块查找所需要的模板文件的目录
    tasks 目录          # 定义tasks,role的基本元素,至少应该包含一个名为main.yml的文件;其他的文件通过include的进行调用
    handlers 目录       # 至少包含一个main.yml的文件;其他的文件通过include的进行调用
    vars 目录           # 定义变量,至少包含一个main.yml的文件;其他的文件通过include的进行调用
    meta 目录           # 定义当前角色的特殊的设定和一下依赖关系,至少包含一个main.yml的文件;其他的文件通过include的进行调用
    default 目录        # 设定默认变量时使用此目录中的main.yml文件
    

    示例

    这里使用nginx为例介绍一下角色(roles)的使用

    目录结构

    目录结构
    [root@ansible ansible]# tree /etc/ansible/
    /etc/ansible/
    ├── ansible.cfg                            # ansible的配置文件
    ├── hosts                                  # ansible的主机清单文件
    ├── nginx_role.yml                         # nginx角色的执行文件
    └── roles                                  # 角色目录(安装ansible后就生成了,可以在其他目录下) 
        └── nginx                              # 角色目录(nginx为例) 
            ├── files                          # 需要拷贝或者其他的一些文件目录
            │   └── index.html                 # 给nginx拷贝一个index.html文件
            ├── handlers                       # 任务触发器目录
            │   └── main.yml                   # 触发器触发后执行的动作
            ├── tasks                          # 具体任务路径 
            │   ├── copyfile.yml               # copy index文件的任务剧本
            │   ├── group.yml                  # 创建nginx组的任务剧本
            │   ├── main.yml                   # 综合每个任务,并指定任务剧本的执行顺序 
            │   ├── start.yml                  # 启动nginx任务剧本 
            │   ├── templ.yml                  # 拷贝nginx模板文件任务剧本
            │   ├── user.yml                   # 创建nginx用户任务剧本 
            │   └── yum.yml                    # 安装nginx任务剧本 
            ├── templates                      # 模板文件目录 
            │   └── nginx.conf.j2              # nginx模板配置文件(必须是.j2结尾文件) 
            └── vars                           # 环境变量目录 
                └── main.yml                    # nginx所需的环境变量 
    

    下面介绍每个目录每个文件具体内容和含义

    vars目录

    vars目录:
    main.yml文件: 定义一些变量,给nginx的模板文件使用,内容如下:
    nginx_port: 123
    nginx_user: brian
    

    files目录

    files目录:
    index.html文件:给nginx的主页文件使用copy模块拷贝到被管理主机,内容如下:
    Welcome to Brian Ansible
    

    templates目录

    templates目录:
    nginx.conf.j2文件 就是在ansible主机上安装nginx后,把/etc/nginx/nginx.conf这个文件copy过来改名为nginx.conf.j2
    主要修改如下:
    user nginx; 改为: user {{ nginx_user }};
    worker_processes auto; 改为: worker_processes {{ ansible_processor_vcpus*4 }};
    listen       {{ nginx_port }} default_server;改为: listen       {{ nginx_port }} default_server;
    listen       [::]:{{ nginx_port }} default_server;改为: listen       [::]:{{ nginx_port }} default_server;
    

    tasks目录

    tasks目录:
    copyfile.yml文件:copy index.html文件给远程主机的nginx,内容如下:
    - name: copy nginx index file
      copy: src=/etc/ansible/roles/nginx/files/index.html dest=/usr/share/nginx/html/
    
    group.yml文件:创建nginx组的剧本,内容如下:
    - name: create nginx group
      group: name=brian 
    
    user.yml文件:创建nginx用户的剧本,内容如下:
    - name: create nginx user for nginx group
      user: name=brian group=brian system=yes shell=/sbin/nologin
    
    yum.yml文件: 安装nginx软件包(使用yum的方式安装),内容如下:
    - name: yum install nginx package
      yum: name=nginx
    
    templ.yml文件: copy nginx的模板文件,内容如下:
    - name: copy templates nginx
      template: src=/etc/ansible/roles/nginx/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf
      notify: restart nginx
    
    start.yml: 启动nginx,内容如下:
    - name: start nginx
      service: name=nginx state=started enabled=yes
    
    main.yml文件:使用include调用tasks目录的剧本并指定执行的顺序,内容如下:
    - include: group.yml
    - include: user.yml
    - include: yum.yml
    - include: templ.yml
    - include: copyfile.yml 
    - include: start.yml
    

    handlers目录

    handlers目录:
    main.yml文件:修改了配置文件然后重启服务(在tasks目录中的temlp.yml定义的notify),内容如下:
    - name: restart nginx
      service: name=nginx state=restarted
    

    执行主文件

    nginx_role.yml文件: 执行角色的总文件(必须和roles目录同一个级别),内容如下:
    ---
    - hosts: web
      remote_user: root
    
      roles:
        - nginx
    

    执行

    [root@ansible ansible]# ansible-playbook nginx_role.yml
    

    结果

    查看服务是否启动
    [root@ansible ~]# ansible all -m shell -a "netstat -lntup | grep nginx" 
    192.168.192.129 | CHANGED | rc=0 >>
    tcp        0      0 0.0.0.0:123             0.0.0.0:*               LISTEN      6102/nginx: master  
    tcp6       0      0 :::123                  :::*                    LISTEN      6102/nginx: master 
    
    查看启动用户及进程数
    [root@ansible ~]# ansible all -m shell -a "ps aux | grep nginx"                               
    192.168.192.129 | CHANGED | rc=0 >>
    root       6102  0.0  0.1 125092  2256 ?        Ss   10:55   0:00 nginx: master process /usr/sbin/nginx
    brian      6103  0.0  0.3 129348  7036 ?        S    10:55   0:00 nginx: worker process
    brian      6104  0.0  0.3 129348  7036 ?        S    10:55   0:00 nginx: worker process
    brian      6105  0.0  0.3 129348  7036 ?        S    10:55   0:00 nginx: worker process
    brian      6106  0.0  0.3 129348  7036 ?        S    10:55   0:00 nginx: worker process
    brian      6107  0.0  0.3 129348  7036 ?        S    10:55   0:00 nginx: worker process
    brian      6108  0.0  0.3 129348  7036 ?        S    10:55   0:00 nginx: worker process
    brian      6109  0.0  0.3 129348  7036 ?        S    10:55   0:00 nginx: worker process
    brian      6110  0.0  0.3 129348  7036 ?        S    10:55   0:00 nginx: worker process
    root       6377 45.0  0.0 113172  1196 pts/1    S+   10:58   0:00 /bin/sh -c ps aux | grep nginx
    root       6379  0.0  0.0 112704   940 pts/1    S+   10:58   0:00 grep nginx
    
    尝试访问nginx
    [root@ansible ansible]# curl 192.168.192.129:123
    Welcome to Brian Ansible
    

     其他使用方法

    以上面的示例为例:

    1) 定义多个角色调用
    在nginx_role.yml文件中定义
    ---
    - hosts: web
      remote_user: root
    
      roles:
        - nginx     # nginx角色
        - mysql     # MySQL角色
    
    
    2) 一个角色引用另一个角色的任务
    tasks目录的main文件:
    - include: group.yml
    - include: user.yml
    - include: yum.yml
    - include: /etc/ansible/mysql/tasks/yum.yml    # 调用MySQL角色的yum任务
    - include: templ.yml
    - include: copyfile.yml 
    - include: start.yml
    
    
    3) 标签(通过标签指定要指定那个角色)
    在nginx_role.yml文件中定义
    ---
    - hosts: web
      remote_user: root
    
      roles:
        - { role: nginx, tags: ['web','http'] }      
        - { role: mysql, tags: ['db','mysql'] }
    
    使用(使用 -t 标签名)执行
    [root@ansible ansible]# ansible-playbook -t web /etc/ansible/nginx_role.yml
    
    4) 使用流程控制语句
    在nginx_role.yml文件中定义
    ---
    - hosts: web
      remote_user: root
    
      roles:
        - { role: nginx, tags: ['web','http'] }      
        - { role: mysql, tags: ['db','mysql'], when ansible_distribution_major_version == "7" }
    

    总结

    温馨提示:
    通过上面的一个nginx的示例,我们可以基本了解了ansible中role中的使用方法和规范,例子很简单但是包含了常用的东西
    上面的示例只是为了学习和理解,并不适合在生产使用,后续我会退出一下在生产用的roles的文章,也可以在https://galaxy.ansible.com/这个网站上去下载

  • 相关阅读:
    [MFC]LPSTR LPCSTR LPWSTR LPCWSTR
    [Color]ARGB各个含义
    IE9 : DOM Exception: INVALID_CHARACTER_ERR (5)
    深入理解JavaScript系列(49):Function模式(上篇)
    如何提升JavaScript的递归效率
    深入理解JavaScript系列(47):对象创建模式(上篇)
    Jquery autocomplete插件的使用
    jQuery data(key, value)函数 在匹配的元素上随心所欲的存放数据 (2
    如何提升JavaScript函数的运行速度
    jQuery UI Autocomplete是jQuery UI的自动完成组件
  • 原文地址:https://www.cnblogs.com/brianzhu/p/10207676.html
Copyright © 2011-2022 走看看