zoukankan      html  css  js  c++  java
  • 第十八章 AnsibleplaybookRole基础介绍

    一、Ansible Roles概述

    roles不管是Ansible还是saltstack,我在写一键部署的时候,都不可能把所有的步骤全部写入到一个'剧本'文件当中,我们肯定需要把不同的工作模块,拆分开来,解耦,那么说到解耦,我们就需要用到roles官方推荐,因为roles的目录结构层次更加清晰。
    
    例如:我们之前推荐大家写一个base.yml里面写所有基础优化的项目,其实把所有东西摞进去也是很鸡肋的,不如我们把这些功能全部拆分开,谁需要使用,就调用即可。
    
    建议:每个roles最好只使用一个tasks这样方便我们去调用,能够很好的做到解耦。(SOA)
    

    二、Ansible Roles目录结构

    角色期望文件位于某些目录名称中。角色必须至少包含其中一个目录,但是排除任何未使用的目录是完全正确的。在使用时,每个目录必须包含一个main.yml文件,其中包含相关内容:
    production                # inventory file for production servers
    staging                   # inventory file for staging environment
    
    group_vars/
       group1.yml             # here we assign variables to particular groups
       group2.yml
    host_vars/
       hostname1.yml          # here we assign variables to particular systems
       hostname2.yml
    
    library/                  # if any custom modules, put them here (optional)
    module_utils/             # if any custom module_utils to support modules, put them here (optional)
    filter_plugins/           # if any custom filter plugins, put them here (optional)
    
    site.yml                  # master playbook
    webservers.yml            # playbook for webserver tier
    dbservers.yml             # playbook for dbserver tier
    
    roles/
        common/               # this hierarchy represents a "role"
            tasks/            #
                main.yml      #  <-- tasks file can include smaller files if warranted
            handlers/         #
                main.yml      #  <-- handlers file
            templates/        #  <-- files for use with the template resource
                ntp.conf.j2   #  <------- templates end in .j2
            files/            #
                bar.txt       #  <-- files for use with the copy resource
                foo.sh        #  <-- script files for use with the script resource
            vars/             #
                main.yml      #  <-- variables associated with this role
            defaults/         #
                main.yml      #  <-- default lower priority variables for this role
            meta/             #
                main.yml      #  <-- role dependencies
            library/          # roles can also include custom modules
            module_utils/     # roles can also include custom module_utils
            lookup_plugins/   # or other types of plugins, like lookup in this case
    
        webtier/              # same kind of structure as "common" was above, done for the webtier role
        monitoring/           # ""
        fooapp/               # ""
    
    site:是ansible的统一入口,就行调用的安装服务总配置
    webservers.yaml:主要是对ansible里一些能做功能,yum等
    roles:角色目录
    common:公共的roles目录
    nginx:角色的软件目录
    tasks:包含角色要执行的主要任务列表
    handlers:包含处理程序,可以由此角色使用,甚至可以在此角色之外的任何位置使用
    defaults:角色默认的变量
    vars:角色其他的变量
    files:包含可以通过此角色部署的文件
    templates:包含可以通过此角色部署的模板
    meta:角色定义的一些元数据
    

    三、Ansible Roles目录创建

    1.手动创建

    [root@m01 ~]# mkdir /project
    [root@m01 ~]# cd /project/
    [root@m01 /project]# touch site.yml
    [root@m01 /project]# mkdir roles
    [root@m01 /project]# cd roles/
    [root@m01 /project/roles]# mkdir {nginx,php,myriadb,nfs-server,nfs-client}
    

    2.使用命令创建

    [root@m01 /project/roles]# ansible-galaxy init nginx
    - Role nginx was created successfully
    [root@m01 /project/roles]# tree ./
    ./
    ├── mariadb
    ├── nfs-client
    ├── nfs-server
    ├── nginx
    │   ├── defaults
    │   │   └── main.yml
    │   ├── files
    │   ├── handlers
    │   │   └── main.yml
    │   ├── meta
    │   │   └── main.yml
    │   ├── README.md
    │   ├── tasks
    │   │   └── main.yml
    │   ├── templates
    │   ├── tests
    │   │   ├── inventory
    │   │   └── test.yml
    │   └── vars
    │       └── main.yml
    └── php
    
    13 directories, 8 files
    [root@m01 /project/roles]#
    

    四、Ansible Roles 依赖

    roles允许你再使用roles时自动引入其他的roles。role依赖关系存储在roles目录中meta/main.yml文件中。
    
    例如:推送wordpress并解压,前提条件,必须要安装nginx和php,把服务跑起来,才能运行wordpress的页面,此时我们就可以在wordpress的roles中定义依赖nginx和php的roles
    
    [root@m01 roles]# vim /etc/ansible/roles/wordpress/meta/main.yml
    dependencies:
      - { role: nginx }
      - { role: php }
      
    如果编写了meta目录下的main.yml文件,那么Ansible会自动先执行meta目录中main.yml文件中的dependencies文件,如上所示,就会先执行nginx和php的安装。
    
  • 相关阅读:
    上下伸展的JS菜单
    [ZZ]Debug VBScript with Visual Studio
    面试总结之杂题
    [ZZ]9 Confusing Naming Conventions for Beginners
    Robocopy
    [ZZ]什么是Alpha,Beta,RC,RTM,CTP版
    使用位运算交换两个值,不用临时变量
    学习笔记之编程之美微软技术面试心得(一)
    C#中如何获取系统环境变量
    学习笔记之SQL教程 from W3School
  • 原文地址:https://www.cnblogs.com/jhno1/p/15723301.html
Copyright © 2011-2022 走看看