Ansible Roles:从目录结构到参数校验与依赖复用

原文:Roles;作者:Ansible 项目贡献者与社区文档团队(页面无个人署名)。中文全文译写与编者审核:未完纪。核对日期:2026-10-05。

依据 2026-10-05 抓取的 Ansible Community Documentation latest 页面整理。角色参数校验始于 2.11;roles 下 vars 的作用域在 ansible-core 2.15 前后不同。latest 是滚动文档,部署前应按实际 ansible-core 版本核对。

原创流程示意:角色目录经过查找、静态或动态载入、依赖和参数校验、任务执行,最后进入处理器阶段;去重发生在 play 范围。
图 1:角色从文件结构进入 play 的逻辑流程。参数校验和依赖的实际先后关系以正文说明为准。

Role(角色)按照约定的文件结构,自动载入相互关联的变量、文件、任务、处理器和其他 Ansible 内容。把一组运维操作组织成角色后,就可以在不同 playbook 中重复使用,也便于向其他使用者分发。角色既是一种内容组织方式,也是一套明确的载入规则;理解这些规则,比简单地把任务移进 roles 目录更重要。

一、角色目录结构

角色有七个主要的标准目录,每个角色至少包含其中一个;没有用到的目录可以省略。下例先列出 playbook,再列出同级 roles 目录中的多个角色。library、module_utils 和各类插件目录是额外扩展,并不属于必须齐备的七个目录。

playbook 文件

# playbooks
site.yml
webservers.yml
fooservers.yml

标准角色目录与可选扩展目录

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/               # ""

在多数目录中,Ansible 默认查找 main.yml,也接受 main.yaml 或不带扩展名的 main。各目录的意义如下。

路径 作用与边界
tasks/main.yml 提供角色执行的任务列表。
handlers/main.yml 导入父 play 的处理器,可被本角色、其他角色及 play 中的任务使用。
defaults/main.yml 提供很低优先级的默认变量。本角色自身默认值优先于其他角色默认值,但其他变量来源均可覆盖它。
vars/main.yml 向 play 提供高优先级变量,使用时要注意变量优先级与覆盖关系。
files/stuff.txt 供角色及其子内容引用的普通文件。
templates/something.j2 供角色及其子内容使用的模板。
meta/main.yml 依赖和 Galaxy 元数据,例如支持的平台。独立角色上传 Galaxy 需要此文件,play 中使用角色不强制要求。

上述具体文件本身都不是角色成立的必备条件。只提供 files/something.txt,或者只提供 vars/for_import.yml,仍然可以是有效角色。“独立角色”指可以单独安装、并非某个 collection 组成部分的角色;它还能携带 library/my_module.py 等自定义模块或插件。vars 与 defaults 中的变量公开到 play 作用域的行为,应结合 import_role/include_role 的 public 选项控制;不能把文件放进角色目录就视为私有数据。

部分目录可以增加其他 YAML 文件,但这些文件不会默认自动执行。可以显式 include/import,也可以在 include_role/import_role 中指定入口。常见做法是把平台相关任务拆开,由 tasks/main.yml 按操作系统选择:

按 Red Hat 与 Debian 系列系统导入安装任务

# roles/example/tasks/main.yml
- name: Install the correct web server for RHEL
  ansible.builtin.import_tasks: redhat.yml
  when: ansible_facts['os_family']|lower == 'redhat'

- name: Install the correct web server for Debian
  ansible.builtin.import_tasks: debian.yml
  when: ansible_facts['os_family']|lower == 'debian'

# roles/example/tasks/redhat.yml
- name: Install web server
  ansible.builtin.yum:
    name: "httpd"
    state: present

# roles/example/tasks/debian.yml
- name: Install web server
  ansible.builtin.apt:
    name: "apache2"
    state: present

也可以在载入角色时直接指定某个任务文件,从而绕过 tasks/main.yml:

通过 tasks_from 选择 apt.yml

- name: include apt tasks
  ansible.builtin.include_role:
      name: package_manager_bootstrap
      tasks_from: apt.yml
  when: ansible_facts['os_family'] == 'Debian'

defaults 和 vars 还允许嵌套目录。若作为变量入口的 main 是目录,Ansible 会按字母顺序读取其中的变量文件和子目录;同一层同时存在子目录与文件时,先处理目录。下面的 vars/main 展示这种布局。

变量入口也可以是含多个文件的目录

roles/
    common/          # this hierarchy represents a "role"
        vars/
            main/    #  <-- variables associated with this role
                first_nested_directory/
                    first_variables_file.yml
                second_nested_directory/
                    second_variables_file.yml
                third_variables_file.yml

二、存储角色与查找路径

默认情况下,Ansible 会在这些位置寻找角色:使用中的 collections、相对于 playbook 文件的 roles/ 目录、配置的 roles_path,以及 playbook 所在目录。roles_path 的默认值是 ~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles。

如果角色放在其他位置,应配置 roles_path。把共享角色放在一个统一位置,便于多个 playbook 使用;相关配置由 ansible.cfg 等 Ansible 配置机制管理。也可以直接给出角色的完整路径:

用绝对路径引用角色

---
- hosts: webservers
  roles:
    - role: '/path/to/my/roles/common'

三、在 playbook 中使用角色

角色有四种常见入口:在 play 层使用 roles;在 tasks 中用 include_role 动态载入;在 tasks 中用 import_role 静态导入;或作为另一个角色 meta/main.yml 中的依赖。选择入口时,需要同时考虑执行顺序、标签传播与变量范围。

在 play 层使用 roles

经典角色调用方式

---
- hosts: webservers
  roles:
    - common
    - webservers

对于 roles 下的每个角色 x,Ansible 会查找 roles/x/tasks、handlers、vars、defaults、meta 的 main 入口。角色里的 copy、script、template 和 include 任务,可以直接引用对应 files、templates、tasks 目录中的文件,无须自行拼接绝对或相对路径。vars 和 defaults 的 main 也可以是目录。include_role/import_role 能选用不同的入口文件名,但 meta 目录不能这样自定义。

play 层 roles 被视为静态导入,并在解析 playbook 时处理。一个 play 的执行顺序是:

  1. 执行 pre_tasks。
  2. 执行 pre_tasks 通知的 handlers。
  3. 按 roles 列表顺序执行角色;每个角色的 meta/main.yml 依赖先执行,并受条件和标签过滤影响。
  4. 执行 play 中的 tasks。
  5. 执行 roles 或 tasks 通知的 handlers。
  6. 执行 post_tasks。
  7. 执行 post_tasks 通知的 handlers。

如果依赖 pre_tasks、post_tasks 或角色依赖来控制维护窗口、监控停报或负载均衡摘流,就要给这些步骤添加相应标签,并在筛选执行时一并选择。只给主体任务加标签,可能破坏原本的前后操作顺序。roles 条目还可以接受变量和标签:

给同一应用角色传递不同配置及标签

---
- hosts: webservers
  roles:
    - common
    - role: foo_app_instance
      vars:
        dir: '/opt/a'
        app_port: 5000
      tags: typeA
    - role: foo_app_instance
      vars:
        dir: '/opt/b'
        app_port: 5001
      tags: typeB

在 role 条目上添加的标签会应用到该角色的全部任务。需要注意版本差异:ansible-core 2.15 之前,roles 部分的 vars 会加入 play 变量,因此角色执行之前和之后的任务都可能看到它们;可通过 DEFAULT_PRIVATE_ROLE_VARS 改变旧行为。较新版本中,这里的 vars 不再泄漏到 play 变量作用域。这个版本说明与角色文件 defaults/vars 的 public 设置是不同问题。

include_role:动态复用

include_role 可以出现在 tasks 的任意位置,角色在运行到该任务时载入。与总是在普通 tasks 前执行的 play 级 roles 不同,动态角色前面的普通任务会先执行,后面的任务会后执行。

在普通任务之间动态载入角色

---
- hosts: webservers
  tasks:
    - name: Print a message
      ansible.builtin.debug:
        msg: "this task runs before the example role"

    - name: Include the example role
      ansible.builtin.include_role:
        name: example

    - name: Print a message
      ansible.builtin.debug:
        msg: "this task runs after the example role"

动态载入时也可以传入变量、标签等关键字:

动态角色上的 vars 与 tags

---
- hosts: webservers
  tasks:
    - name: Include the foo_app_instance role
      ansible.builtin.include_role:
        name: foo_app_instance
      vars:
        dir: '/opt/a'
        app_port: 5000
      tags: typeA
  # ...

include_role 任务上的标签只标记 include 语句本身,不会自动传播给角色所有任务。因此,使用 --tags 筛选时,角色内部需要执行的任务也应带有对应标签。以下示例则通过 when 只在 Red Hat 系列主机载入角色:

附带条件的动态载入

---
- hosts: webservers
  tasks:
    - name: Include the some_role role
      ansible.builtin.include_role:
        name: some_role
      when: "ansible_facts['os_family'] == 'RedHat'"

import_role:静态复用

import_role 也可以放在 tasks 的任意位置,但采用静态导入,其行为与 roles 关键字类似。导入语句所在的位置决定任务序列中的相对位置:

在任务列表中静态导入角色

---
- hosts: webservers
  tasks:
    - name: Print a message
      ansible.builtin.debug:
        msg: "before we run our role"

    - name: Import the example role
      ansible.builtin.import_role:
        name: example

    - name: Print a message
      ansible.builtin.debug:
        msg: "after we ran our role"

导入时可以附带变量和其他关键字。若在 import_role 语句上设置标签,标签会应用到角色中的全部任务,这与 include_role 的标签规则不同。

静态导入并传递变量

---
- hosts: webservers
  tasks:
    - name: Import the foo_app_instance role
      ansible.builtin.import_role:
        name: foo_app_instance
      vars:
        dir: '/opt/a'
        app_port: 5000
  # ...

四、角色参数校验

从 Ansible 2.11 起,可以用参数规范启用角色参数校验。规范放在 meta/argument_specs.yml,也支持 .yaml 扩展名。定义后,Ansible 会在角色执行起点插入校验任务,把传入参数与规范比较;校验不通过时,角色执行失败。

规范也可放在 meta/main.yml,但采用这种布局的角色不能在低于 2.11 的版本使用。为兼顾旧版本,原文建议使用单独的 meta/argument_specs.yml。若角色有依赖,依赖角色的参数验证先于当前角色进行,即使当前角色稍后校验失败也一样。自动插入的参数校验任务带 always 标签;静态导入时,它会执行,除非通过 --skip-tags 排除。

参数规范的格式

文件顶层必须是 argument_specs,所有字段名都使用小写。argument_specs 下每个键表示一个角色入口,随后描述该入口及其 options。

入口字段 要求
entry-point-name 入口名称;默认入口为 main,对应任务文件的基本名,不含 .yml/.yaml。
short_description 一行短描述,最好是短语,不以句号结束;必须是字符串,不能是列表。会出现在 ansible-doc -t role -l 以及角色文档标题中,支持 Ansible markup。
description 较长的描述,可以是字符串或字符串列表;列表每项成为一个段落,支持 Ansible markup。
version_added 入口新增时的角色版本,以字符串表示,例如 ‘2.1’,不能误写成浮点数;collection 中填写新增入口的 collection 版本,例如 1.0.0。
author 入口作者,使用单个字符串或字符串列表;多人时每人一项。
options 入口参数定义;每个键是参数名称,其下定义数据类型、默认值和其他约束。
参数字段 要求
description 用完整句子解释参数;字符串或字符串列表均可,列表每项是一个段落,支持 Ansible markup。
version_added 仅在参数晚于角色/入口初次发布时需要;字符串格式,collection 中填写 collection 版本。
type 参数数据类型,默认 str;允许值以 Argument spec 参考为准。list 应同时给出 elements。
required 仅在 true 时有必要写出;省略表示非必需。
default required 为 false 或省略时可填写,省略默认视为 null;必须与角色 defaults 中实际默认值一致。规范中的 default 不会取代角色 defaults 的运行时默认值。
choices 可选值列表;没有可选值限制时不要写空列表。
elements type 为 list 时,其列表元素的数据类型。
options 字典或字典列表的嵌套结构定义。

default 不应重复写进说明文字,除非需要补充适用条件。布尔值使用 true/false,以兼容 ansible-lint。类型校验是“可转换即通过”的校验:传入整数 1,而规范要求字符串,也可能通过验证;然而角色真正拿到的仍是原始整数 1,并非字符串 "1"。需要固定类型时,在角色逻辑中显式使用 | string、| bool 等过滤器。

完整参数规范示例

下面保留原文的完整 myapp 示例:main 入口覆盖整数、字符串、字符串列表、带默认值的字典列表和带子选项的字典;alternate 入口展示另一个任务文件及不同默认值。示例中的作者 Daniel Ziegenberg 是该参数示例的署名,不应据此误认其为整个文档的唯一作者。

roles/myapp/meta/argument_specs.yml:包括全部嵌套 options 与 alternate 入口

# roles/myapp/meta/argument_specs.yml
---
argument_specs:
  # roles/myapp/tasks/main.yml entry point
  main:
    short_description: Main entry point for the myapp role
    description:
      - This is the main entrypoint for the C(myapp) role.
      - Here we can describe what this entrypoint does in lengthy words.
      - Every new list item is a new paragraph. You can have multiple sentences
        per paragraph.
    author:
      - Daniel Ziegenberg
    options:
      myapp_int:
        type: "int"
        required: false
        default: 42
        description:
          - "The integer value, defaulting to 42."
          - "This is a second paragraph."

      myapp_str:
        type: "str"
        required: true
        description: "The string value"

      myapp_list:
        type: "list"
        elements: "str"
        required: true
        description: "A list of string values."
        version_added: 1.3.0

      myapp_list_with_dicts:
        type: "list"
        elements: "dict"
        required: false
        default:
          - myapp_food_kind: "meat"
            myapp_food_boiling_required: true
            myapp_food_preparation_time: 60
          - myapp_food_kind: "fruits"
            myapp_food_preparation_time: 5
        description: "A list of dicts with a defined structure and with a default value."
        options:
          myapp_food_kind:
            type: "str"
            choices:
              - "vegetables"
              - "fruits"
              - "grains"
              - "meat"
            required: false
            description: "A string value with a limited list of allowed choices."

          myapp_food_boiling_required:
            type: "bool"
            required: false
            default: false
            description: "Whether the kind of food requires boiling before consumption."

          myapp_food_preparation_time:
            type: int
            required: true
            description: "Time to prepare a dish in minutes."

      myapp_dict_with_suboptions:
        type: "dict"
        required: false
        default:
          myapp_host: "bar.foo"
          myapp_exclude_host: true
          myapp_path: "/etc/myapp"
        description: "A dict with a defined structure and default values."
        options:
          myapp_host:
            type: "str"
            choices:
              - "foo.bar"
              - "bar.foo"
              - "ansible.foo.bar"
            required: true
            description: "A string value with a limited list of allowed choices."

          myapp_exclude_host:
            type: "bool"
            required: true
            description: "A boolean value."

          myapp_path:
            type: "path"
            required: true
            description: "A path value."

          original_name:
            type: list
            elements: "str"
            required: false
            description: "An optional list of string values."

  # roles/myapp/tasks/alternate.yml entry point
  alternate:
    short_description: Alternate entry point for the myapp role
    description:
      - This is the alternate entrypoint for the C(myapp) role.
    version_added: 1.2.0
    options:
      myapp_int:
        type: "int"
        required: false
        default: 1024
        description: "The integer value, defaulting to 1024."

五、在同一个 play 多次运行角色

在 play 级 roles 复用中,即使一个角色被列出多次,Ansible 默认也只执行一次,除非各次角色定义具有不同参数。以下列表中的 foo 只执行一次:

默认去重的角色调用

---
- hosts: webservers
  roles:
    - foo
    - bar
    - foo

要强制重复,有两种常用方式。第一种是传递不同的角色参数。改变变量值不等同于改变角色参数;这种示例必须使用 roles,因为 import_role 和 include_role 不接受这里所说的角色参数写法。

内联映射形式:不同 message 使 foo 执行两次

---
- hosts: webservers
  roles:
    - { role: foo, message: "first" }
    - { role: foo, message: "second" }

展开映射形式:与上一例等效

---
- hosts: webservers
  roles:
    - role: foo
      message: "first"
    - role: foo
      message: "second"

第二种方式是在需要重复执行的角色 meta/main.yml 中设置 allow_duplicates: true。此时即使两次条目相同,foo 也会执行两次。

显式允许角色重复

# playbook.yml
---
- hosts: webservers
  roles:
    - foo
    - foo

# roles/foo/meta/main.yml
---
allow_duplicates: true

六、角色依赖

角色依赖会在使用当前角色时自动带入其他角色。这里的 dependency 更准确地说是执行先决条件:角色之间并不存在真正的父子关系;包括依赖列表中角色在内,所有角色的父对象都是 play。Ansible 载入列出的角色,先执行 dependencies,再执行声明依赖的角色。

依赖放在角色目录 meta/main.yml 中,内容是应先执行的角色及其参数列表:

myapp 对 common、apache 和 postgres 的依赖

# roles/myapp/meta/main.yml
---
dependencies:
  - role: common
    vars:
      some_parameter: 3
  - role: apache
    vars:
      apache_port: 80
  - role: postgres
    vars:
      dbname: blarg
      other_parameter: 12

使用 roles 关键字时,依赖按递归顺序处理。如果 foo 依赖 bar,而 bar 依赖 baz,实际次序就是 baz、bar、foo。

让依赖重复执行

依赖与 roles 列表使用类似的去重逻辑。两个角色都依赖同一个角色时,默认只运行这项依赖一次;若参数、标签或 when 条件不同,或者在需重复的角色上设置 allow_duplicates: true,则可能重复执行。去重不参考父角色的调用签名。

原文特别提醒:在依赖示例中,用 vars 代替角色参数会改变变量作用域。如果把下面各次 n 的参数改写成 vars,可能使 n 在整个 play 中都成为 4,包括较早调用的角色。去重还发生在变量求值之前,因此惰性求值可能让看似不同的调用被视为相同。不要仅凭最终值判断两个角色调用一定不同。

例如,car 依赖四个参数不同的 wheel:

car 的 meta/main.yml

---
dependencies:
  - role: wheel
    n: 1
  - role: wheel
    n: 2
  - role: wheel
    n: 3
  - role: wheel
    n: 4

wheel 又依赖 tire 和 brake:

wheel 的 meta/main.yml

---
dependencies:
  - role: tire
  - role: brake

在 tire 和 brake 各自的 meta/main.yml 中允许重复:

tire 与 brake 的元数据

---
allow_duplicates: true

由此得到的顺序如下:每个 wheel 前都有其 tire 和 brake,最后才运行 car。

示意执行顺序,省略号表示其余车轮按相同规则继续

tire(n=1)
brake(n=1)
wheel(n=1)
tire(n=2)
brake(n=2)
wheel(n=2)
...
car

allow_duplicates 应写在 dependencies 所引用、希望重复执行的角色中,而不是写在引用它的父级角色中。本例 wheel 自身无需该开关,因为 car 已经为每次 wheel 传入不同的 n。去重仅发生在一个 play 内;同一 playbook 中不同 play 可以再次执行同一角色。

七、在独立角色中嵌入模块和插件

这一机制仅适用于独立角色。collection 中的角色不支持这样嵌入插件,应使用 collection 的 plugins 结构分发。对于内部软件的专用模块,可以把模块随角色分发,从而不必让每个使用者手工配置 Ansible 的模块库路径。

自定义模块放在与 tasks、handlers 同级的 library 目录。下例角色提供 module1 和 module2:

在 library 中放置自定义模块

roles/
    my_custom_modules/
        library/
            module1
            module2

这些模块可用于该角色自身,也可用于随后调用的角色:

先载入模块角色,再使用其模块

---
- hosts: webservers
  roles:
    - my_custom_modules
    - some_other_role_using_my_custom_modules
    - yet_another_role_using_my_custom_modules

必要时也能把 Ansible 核心模块的修改版嵌入角色,例如在正式发行前使用开发版实现。原文明确要求谨慎:核心 API 签名可能变化,这种临时方法没有兼容性保证。

插件按对应的插件目录结构分发。例如过滤器插件放在 filter_plugins:

分发自定义过滤器

roles/
    my_custom_filter/
        filter_plugins
            filter1
            filter2

在 my_custom_filter 之后调用的角色,就可以在 Jinja 模板中使用这些过滤器。模块和插件是代码执行边界;应确认来源和实际逻辑,再赋予主机操作权限。

八、通过 Ansible Galaxy 分享角色

Ansible Galaxy 提供社区角色的查找、下载、评分与评审。Ansible 自带 ansible-galaxy 客户端,既可下载角色,也为新角色生成默认结构。创建、发布和管理角色的进一步细节应查阅 Galaxy 用户指南。

继续学习时,可以沿原文引用查阅 YAML 语法、playbook 基础、变量、条件、循环、标签和 collection 索引;开发自定义功能时阅读模块与插件开发指南;交流问题则使用 Ansible 社区沟通渠道。角色复用不会自动保证幂等性、安全性或跨版本兼容,真正上线前仍需以实际环境验证。

来源、署名与许可

原文版权归 Ansible 项目贡献者;文档源码仓库 COPYING 采用 GNU General Public License version 3(GPL-3.0)。本中文译写保留原署名、来源与GPL-3.0许可;改编内容按相同许可提供。完整许可附于本文末尾,并保留原 COPYING 链接。

代码保留原有技术结构,英文标识符、代码注释和演示文本保留,编者补充已明确标注。原创流程图用于解释正文,不代表实测结果。

版权与许可全文

以下保留本页涉及的来源材料或示例代码的版权、许可条件与免责声明;各自适用范围依原声明。中文翻译及编辑标注:未完纪,2026-10-05。

LICENSE-GPL-3.0.txt

                    GNU GENERAL PUBLIC LICENSE
                       Version 3, 29 June 2007

 Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
 Everyone is permitted to copy and distribute verbatim copies
 of this license document, but changing it is not allowed.

                            Preamble

  The GNU General Public License is a free, copyleft license for
software and other kinds of works.

  The licenses for most software and other practical works are designed
to take away your freedom to share and change the works.  By contrast,
the GNU General Public License is intended to guarantee your freedom to
share and change all versions of a program--to make sure it remains free
software for all its users.  We, the Free Software Foundation, use the
GNU General Public License for most of our software; it applies also to
any other work released this way by its authors.  You can apply it to
your programs, too.

  When we speak of free software, we are referring to freedom, not
price.  Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
them if you wish), that you receive source code or can get it if you
want it, that you can change the software or use pieces of it in new
free programs, and that you know you can do these things.

  To protect your rights, we need to prevent others from denying you
these rights or asking you to surrender the rights.  Therefore, you have
certain responsibilities if you distribute copies of the software, or if
you modify it: responsibilities to respect the freedom of others.

  For example, if you distribute copies of such a program, whether
gratis or for a fee, you must pass on to the recipients the same
freedoms that you received.  You must make sure that they, too, receive
or can get the source code.  And you must show them these terms so they
know their rights.

  Developers that use the GNU GPL protect your rights with two steps:
(1) assert copyright on the software, and (2) offer you this License
giving you legal permission to copy, distribute and/or modify it.

  For the developers' and authors' protection, the GPL clearly explains
that there is no warranty for this free software.  For both users' and
authors' sake, the GPL requires that modified versions be marked as
changed, so that their problems will not be attributed erroneously to
authors of previous versions.

  Some devices are designed to deny users access to install or run
modified versions of the software inside them, although the manufacturer
can do so.  This is fundamentally incompatible with the aim of
protecting users' freedom to change the software.  The systematic
pattern of such abuse occurs in the area of products for individuals to
use, which is precisely where it is most unacceptable.  Therefore, we
have designed this version of the GPL to prohibit the practice for those
products.  If such problems arise substantially in other domains, we
stand ready to extend this provision to those domains in future versions
of the GPL, as needed to protect the freedom of users.

  Finally, every program is threatened constantly by software patents.
States should not allow patents to restrict development and use of
software on general-purpose computers, but in those that do, we wish to
avoid the special danger that patents applied to a free program could
make it effectively proprietary.  To prevent this, the GPL assures that
patents cannot be used to render the program non-free.

  The precise terms and conditions for copying, distribution and
modification follow.

                       TERMS AND CONDITIONS

  0. Definitions.

  "This License" refers to version 3 of the GNU General Public License.

  "Copyright" also means copyright-like laws that apply to other kinds of
works, such as semiconductor masks.

  "The Program" refers to any copyrightable work licensed under this
License.  Each licensee is addressed as "you".  "Licensees" and
"recipients" may be individuals or organizations.

  To "modify" a work means to copy from or adapt all or part of the work
in a fashion requiring copyright permission, other than the making of an
exact copy.  The resulting work is called a "modified version" of the
earlier work or a work "based on" the earlier work.

  A "covered work" means either the unmodified Program or a work based
on the Program.

  To "propagate" a work means to do anything with it that, without
permission, would make you directly or secondarily liable for
infringement under applicable copyright law, except executing it on a
computer or modifying a private copy.  Propagation includes copying,
distribution (with or without modification), making available to the
public, and in some countries other activities as well.

  To "convey" a work means any kind of propagation that enables other
parties to make or receive copies.  Mere interaction with a user through
a computer network, with no transfer of a copy, is not conveying.

  An interactive user interface displays "Appropriate Legal Notices"
to the extent that it includes a convenient and prominently visible
feature that (1) displays an appropriate copyright notice, and (2)
tells the user that there is no warranty for the work (except to the
extent that warranties are provided), that licensees may convey the
work under this License, and how to view a copy of this License.  If
the interface presents a list of user commands or options, such as a
menu, a prominent item in the list meets this criterion.

  1. Source Code.

  The "source code" for a work means the preferred form of the work
for making modifications to it.  "Object code" means any non-source
form of a work.

  A "Standard Interface" means an interface that either is an official
standard defined by a recognized standards body, or, in the case of
interfaces specified for a particular programming language, one that
is widely used among developers working in that language.

  The "System Libraries" of an executable work include anything, other
than the work as a whole, that (a) is included in the normal form of
packaging a Major Component, but which is not part of that Major
Component, and (b) serves only to enable use of the work with that
Major Component, or to implement a Standard Interface for which an
implementation is available to the public in source code form.  A
"Major Component", in this context, means a major essential component
(kernel, window system, and so on) of the specific operating system
(if any) on which the executable work runs, or a compiler used to
produce the work, or an object code interpreter used to run it.

  The "Corresponding Source" for a work in object code form means all
the source code needed to generate, install, and (for an executable
work) run the object code and to modify the work, including scripts to
control those activities.  However, it does not include the work's
System Libraries, or general-purpose tools or generally available free
programs which are used unmodified in performing those activities but
which are not part of the work.  For example, Corresponding Source
includes interface definition files associated with source files for
the work, and the source code for shared libraries and dynamically
linked subprograms that the work is specifically designed to require,
such as by intimate data communication or control flow between those
subprograms and other parts of the work.

  The Corresponding Source need not include anything that users
can regenerate automatically from other parts of the Corresponding
Source.

  The Corresponding Source for a work in source code form is that
same work.

  2. Basic Permissions.

  All rights granted under this License are granted for the term of
copyright on the Program, and are irrevocable provided the stated
conditions are met.  This License explicitly affirms your unlimited
permission to run the unmodified Program.  The output from running a
covered work is covered by this License only if the output, given its
content, constitutes a covered work.  This License acknowledges your
rights of fair use or other equivalent, as provided by copyright law.

  You may make, run and propagate covered works that you do not
convey, without conditions so long as your license otherwise remains
in force.  You may convey covered works to others for the sole purpose
of having them make modifications exclusively for you, or provide you
with facilities for running those works, provided that you comply with
the terms of this License in conveying all material for which you do
not control copyright.  Those thus making or running the covered works
for you must do so exclusively on your behalf, under your direction
and control, on terms that prohibit them from making any copies of
your copyrighted material outside their relationship with you.

  Conveying under any other circumstances is permitted solely under
the conditions stated below.  Sublicensing is not allowed; section 10
makes it unnecessary.

  3. Protecting Users' Legal Rights From Anti-Circumvention Law.

  No covered work shall be deemed part of an effective technological
measure under any applicable law fulfilling obligations under article
11 of the WIPO copyright treaty adopted on 20 December 1996, or
similar laws prohibiting or restricting circumvention of such
measures.

  When you convey a covered work, you waive any legal power to forbid
circumvention of technological measures to the extent such circumvention
is effected by exercising rights under this License with respect to
the covered work, and you disclaim any intention to limit operation or
modification of the work as a means of enforcing, against the work's
users, your or third parties' legal rights to forbid circumvention of
technological measures.

  4. Conveying Verbatim Copies.

  You may convey verbatim copies of the Program's source code as you
receive it, in any medium, provided that you conspicuously and
appropriately publish on each copy an appropriate copyright notice;
keep intact all notices stating that this License and any
non-permissive terms added in accord with section 7 apply to the code;
keep intact all notices of the absence of any warranty; and give all
recipients a copy of this License along with the Program.

  You may charge any price or no price for each copy that you convey,
and you may offer support or warranty protection for a fee.

  5. Conveying Modified Source Versions.

  You may convey a work based on the Program, or the modifications to
produce it from the Program, in the form of source code under the
terms of section 4, provided that you also meet all of these conditions:

    a) The work must carry prominent notices stating that you modified
    it, and giving a relevant date.

    b) The work must carry prominent notices stating that it is
    released under this License and any conditions added under section
    7.  This requirement modifies the requirement in section 4 to
    "keep intact all notices".

    c) You must license the entire work, as a whole, under this
    License to anyone who comes into possession of a copy.  This
    License will therefore apply, along with any applicable section 7
    additional terms, to the whole of the work, and all its parts,
    regardless of how they are packaged.  This License gives no
    permission to license the work in any other way, but it does not
    invalidate such permission if you have separately received it.

    d) If the work has interactive user interfaces, each must display
    Appropriate Legal Notices; however, if the Program has interactive
    interfaces that do not display Appropriate Legal Notices, your
    work need not make them do so.

  A compilation of a covered work with other separate and independent
works, which are not by their nature extensions of the covered work,
and which are not combined with it such as to form a larger program,
in or on a volume of a storage or distribution medium, is called an
"aggregate" if the compilation and its resulting copyright are not
used to limit the access or legal rights of the compilation's users
beyond what the individual works permit.  Inclusion of a covered work
in an aggregate does not cause this License to apply to the other
parts of the aggregate.

  6. Conveying Non-Source Forms.

  You may convey a covered work in object code form under the terms
of sections 4 and 5, provided that you also convey the
machine-readable Corresponding Source under the terms of this License,
in one of these ways:

    a) Convey the object code in, or embodied in, a physical product
    (including a physical distribution medium), accompanied by the
    Corresponding Source fixed on a durable physical medium
    customarily used for software interchange.

    b) Convey the object code in, or embodied in, a physical product
    (including a physical distribution medium), accompanied by a
    written offer, valid for at least three years and valid for as
    long as you offer spare parts or customer support for that product
    model, to give anyone who possesses the object code either (1) a
    copy of the Corresponding Source for all the software in the
    product that is covered by this License, on a durable physical
    medium customarily used for software interchange, for a price no
    more than your reasonable cost of physically performing this
    conveying of source, or (2) access to copy the
    Corresponding Source from a network server at no charge.

    c) Convey individual copies of the object code with a copy of the
    written offer to provide the Corresponding Source.  This
    alternative is allowed only occasionally and noncommercially, and
    only if you received the object code with such an offer, in accord
    with subsection 6b.

    d) Convey the object code by offering access from a designated
    place (gratis or for a charge), and offer equivalent access to the
    Corresponding Source in the same way through the same place at no
    further charge.  You need not require recipients to copy the
    Corresponding Source along with the object code.  If the place to
    copy the object code is a network server, the Corresponding Source
    may be on a different server (operated by you or a third party)
    that supports equivalent copying facilities, provided you maintain
    clear directions next to the object code saying where to find the
    Corresponding Source.  Regardless of what server hosts the
    Corresponding Source, you remain obligated to ensure that it is
    available for as long as needed to satisfy these requirements.

    e) Convey the object code using peer-to-peer transmission, provided
    you inform other peers where the object code and Corresponding
    Source of the work are being offered to the general public at no
    charge under subsection 6d.

  A separable portion of the object code, whose source code is excluded
from the Corresponding Source as a System Library, need not be
included in conveying the object code work.

  A "User Product" is either (1) a "consumer product", which means any
tangible personal property which is normally used for personal, family,
or household purposes, or (2) anything designed or sold for incorporation
into a dwelling.  In determining whether a product is a consumer product,
doubtful cases shall be resolved in favor of coverage.  For a particular
product received by a particular user, "normally used" refers to a
typical or common use of that class of product, regardless of the status
of the particular user or of the way in which the particular user
actually uses, or expects or is expected to use, the product.  A product
is a consumer product regardless of whether the product has substantial
commercial, industrial or non-consumer uses, unless such uses represent
the only significant mode of use of the product.

  "Installation Information" for a User Product means any methods,
procedures, authorization keys, or other information required to install
and execute modified versions of a covered work in that User Product from
a modified version of its Corresponding Source.  The information must
suffice to ensure that the continued functioning of the modified object
code is in no case prevented or interfered with solely because
modification has been made.

  If you convey an object code work under this section in, or with, or
specifically for use in, a User Product, and the conveying occurs as
part of a transaction in which the right of possession and use of the
User Product is transferred to the recipient in perpetuity or for a
fixed term (regardless of how the transaction is characterized), the
Corresponding Source conveyed under this section must be accompanied
by the Installation Information.  But this requirement does not apply
if neither you nor any third party retains the ability to install
modified object code on the User Product (for example, the work has
been installed in ROM).

  The requirement to provide Installation Information does not include a
requirement to continue to provide support service, warranty, or updates
for a work that has been modified or installed by the recipient, or for
the User Product in which it has been modified or installed.  Access to a
network may be denied when the modification itself materially and
adversely affects the operation of the network or violates the rules and
protocols for communication across the network.

  Corresponding Source conveyed, and Installation Information provided,
in accord with this section must be in a format that is publicly
documented (and with an implementation available to the public in
source code form), and must require no special password or key for
unpacking, reading or copying.

  7. Additional Terms.

  "Additional permissions" are terms that supplement the terms of this
License by making exceptions from one or more of its conditions.
Additional permissions that are applicable to the entire Program shall
be treated as though they were included in this License, to the extent
that they are valid under applicable law.  If additional permissions
apply only to part of the Program, that part may be used separately
under those permissions, but the entire Program remains governed by
this License without regard to the additional permissions.

  When you convey a copy of a covered work, you may at your option
remove any additional permissions from that copy, or from any part of
it.  (Additional permissions may be written to require their own
removal in certain cases when you modify the work.)  You may place
additional permissions on material, added by you to a covered work,
for which you have or can give appropriate copyright permission.

  Notwithstanding any other provision of this License, for material you
add to a covered work, you may (if authorized by the copyright holders of
that material) supplement the terms of this License with terms:

    a) Disclaiming warranty or limiting liability differently from the
    terms of sections 15 and 16 of this License; or

    b) Requiring preservation of specified reasonable legal notices or
    author attributions in that material or in the Appropriate Legal
    Notices displayed by works containing it; or

    c) Prohibiting misrepresentation of the origin of that material, or
    requiring that modified versions of such material be marked in
    reasonable ways as different from the original version; or

    d) Limiting the use for publicity purposes of names of licensors or
    authors of the material; or

    e) Declining to grant rights under trademark law for use of some
    trade names, trademarks, or service marks; or

    f) Requiring indemnification of licensors and authors of that
    material by anyone who conveys the material (or modified versions of
    it) with contractual assumptions of liability to the recipient, for
    any liability that these contractual assumptions directly impose on
    those licensors and authors.

  All other non-permissive additional terms are considered "further
restrictions" within the meaning of section 10.  If the Program as you
received it, or any part of it, contains a notice stating that it is
governed by this License along with a term that is a further
restriction, you may remove that term.  If a license document contains
a further restriction but permits relicensing or conveying under this
License, you may add to a covered work material governed by the terms
of that license document, provided that the further restriction does
not survive such relicensing or conveying.

  If you add terms to a covered work in accord with this section, you
must place, in the relevant source files, a statement of the
additional terms that apply to those files, or a notice indicating
where to find the applicable terms.

  Additional terms, permissive or non-permissive, may be stated in the
form of a separately written license, or stated as exceptions;
the above requirements apply either way.

  8. Termination.

  You may not propagate or modify a covered work except as expressly
provided under this License.  Any attempt otherwise to propagate or
modify it is void, and will automatically terminate your rights under
this License (including any patent licenses granted under the third
paragraph of section 11).

  However, if you cease all violation of this License, then your
license from a particular copyright holder is reinstated (a)
provisionally, unless and until the copyright holder explicitly and
finally terminates your license, and (b) permanently, if the copyright
holder fails to notify you of the violation by some reasonable means
prior to 60 days after the cessation.

  Moreover, your license from a particular copyright holder is
reinstated permanently if the copyright holder notifies you of the
violation by some reasonable means, this is the first time you have
received notice of violation of this License (for any work) from that
copyright holder, and you cure the violation prior to 30 days after
your receipt of the notice.

  Termination of your rights under this section does not terminate the
licenses of parties who have received copies or rights from you under
this License.  If your rights have been terminated and not permanently
reinstated, you do not qualify to receive new licenses for the same
material under section 10.

  9. Acceptance Not Required for Having Copies.

  You are not required to accept this License in order to receive or
run a copy of the Program.  Ancillary propagation of a covered work
occurring solely as a consequence of using peer-to-peer transmission
to receive a copy likewise does not require acceptance.  However,
nothing other than this License grants you permission to propagate or
modify any covered work.  These actions infringe copyright if you do
not accept this License.  Therefore, by modifying or propagating a
covered work, you indicate your acceptance of this License to do so.

  10. Automatic Licensing of Downstream Recipients.

  Each time you convey a covered work, the recipient automatically
receives a license from the original licensors, to run, modify and
propagate that work, subject to this License.  You are not responsible
for enforcing compliance by third parties with this License.

  An "entity transaction" is a transaction transferring control of an
organization, or substantially all assets of one, or subdividing an
organization, or merging organizations.  If propagation of a covered
work results from an entity transaction, each party to that
transaction who receives a copy of the work also receives whatever
licenses to the work the party's predecessor in interest had or could
give under the previous paragraph, plus a right to possession of the
Corresponding Source of the work from the predecessor in interest, if
the predecessor has it or can get it with reasonable efforts.

  You may not impose any further restrictions on the exercise of the
rights granted or affirmed under this License.  For example, you may
not impose a license fee, royalty, or other charge for exercise of
rights granted under this License, and you may not initiate litigation
(including a cross-claim or counterclaim in a lawsuit) alleging that
any patent claim is infringed by making, using, selling, offering for
sale, or importing the Program or any portion of it.

  11. Patents.

  A "contributor" is a copyright holder who authorizes use under this
License of the Program or a work on which the Program is based.  The
work thus licensed is called the contributor's "contributor version".

  A contributor's "essential patent claims" are all patent claims
owned or controlled by the contributor, whether already acquired or
hereafter acquired, that would be infringed by some manner, permitted
by this License, of making, using, or selling its contributor version,
but do not include claims that would be infringed only as a
consequence of further modification of the contributor version.  For
purposes of this definition, "control" includes the right to grant
patent sublicenses in a manner consistent with the requirements of
this License.

  Each contributor grants you a non-exclusive, worldwide, royalty-free
patent license under the contributor's essential patent claims, to
make, use, sell, offer for sale, import and otherwise run, modify and
propagate the contents of its contributor version.

  In the following three paragraphs, a "patent license" is any express
agreement or commitment, however denominated, not to enforce a patent
(such as an express permission to practice a patent or covenant not to
sue for patent infringement).  To "grant" such a patent license to a
party means to make such an agreement or commitment not to enforce a
patent against the party.

  If you convey a covered work, knowingly relying on a patent license,
and the Corresponding Source of the work is not available for anyone
to copy, free of charge and under the terms of this License, through a
publicly available network server or other readily accessible means,
then you must either (1) cause the Corresponding Source to be so
available, or (2) arrange to deprive yourself of the benefit of the
patent license for this particular work, or (3) arrange, in a manner
consistent with the requirements of this License, to extend the patent
license to downstream recipients.  "Knowingly relying" means you have
actual knowledge that, but for the patent license, your conveying the
covered work in a country, or your recipient's use of the covered work
in a country, would infringe one or more identifiable patents in that
country that you have reason to believe are valid.

  If, pursuant to or in connection with a single transaction or
arrangement, you convey, or propagate by procuring conveyance of, a
covered work, and grant a patent license to some of the parties
receiving the covered work authorizing them to use, propagate, modify
or convey a specific copy of the covered work, then the patent license
you grant is automatically extended to all recipients of the covered
work and works based on it.

  A patent license is "discriminatory" if it does not include within
the scope of its coverage, prohibits the exercise of, or is
conditioned on the non-exercise of one or more of the rights that are
specifically granted under this License.  You may not convey a covered
work if you are a party to an arrangement with a third party that is
in the business of distributing software, under which you make payment
to the third party based on the extent of your activity of conveying
the work, and under which the third party grants, to any of the
parties who would receive the covered work from you, a discriminatory
patent license (a) in connection with copies of the covered work
conveyed by you (or copies made from those copies), or (b) primarily
for and in connection with specific products or compilations that
contain the covered work, unless you entered into that arrangement,
or that patent license was granted, prior to 28 March 2007.

  Nothing in this License shall be construed as excluding or limiting
any implied license or other defenses to infringement that may
otherwise be available to you under applicable patent law.

  12. No Surrender of Others' Freedom.

  If conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License.  If you cannot convey a
covered work so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you may
not convey it at all.  For example, if you agree to terms that obligate you
to collect a royalty for further conveying from those to whom you convey
the Program, the only way you could satisfy both those terms and this
License would be to refrain entirely from conveying the Program.

  13. Use with the GNU Affero General Public License.

  Notwithstanding any other provision of this License, you have
permission to link or combine any covered work with a work licensed
under version 3 of the GNU Affero General Public License into a single
combined work, and to convey the resulting work.  The terms of this
License will continue to apply to the part which is the covered work,
but the special requirements of the GNU Affero General Public License,
section 13, concerning interaction through a network will apply to the
combination as such.

  14. Revised Versions of this License.

  The Free Software Foundation may publish revised and/or new versions of
the GNU General Public License from time to time.  Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.

  Each version is given a distinguishing version number.  If the
Program specifies that a certain numbered version of the GNU General
Public License "or any later version" applies to it, you have the
option of following the terms and conditions either of that numbered
version or of any later version published by the Free Software
Foundation.  If the Program does not specify a version number of the
GNU General Public License, you may choose any version ever published
by the Free Software Foundation.

  If the Program specifies that a proxy can decide which future
versions of the GNU General Public License can be used, that proxy's
public statement of acceptance of a version permanently authorizes you
to choose that version for the Program.

  Later license versions may give you additional or different
permissions.  However, no additional obligations are imposed on any
author or copyright holder as a result of your choosing to follow a
later version.

  15. Disclaimer of Warranty.

  THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
APPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.

  16. Limitation of Liability.

  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.

  17. Interpretation of Sections 15 and 16.

  If the disclaimer of warranty and limitation of liability provided
above cannot be given local legal effect according to their terms,
reviewing courts shall apply local law that most closely approximates
an absolute waiver of all civil liability in connection with the
Program, unless a warranty or assumption of liability accompanies a
copy of the Program in return for a fee.

                     END OF TERMS AND CONDITIONS

            How to Apply These Terms to Your New Programs

  If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.

  To do so, attach the following notices to the program.  It is safest
to attach them to the start of each source file to most effectively
state the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.

    <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) <year>  <name of author>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

Also add information on how to contact you by electronic and paper mail.

  If the program does terminal interaction, make it output a short
notice like this when it starts in an interactive mode:

    <program>  Copyright (C) <year>  <name of author>
    This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
    This is free software, and you are welcome to redistribute it
    under certain conditions; type `show c' for details.

The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License.  Of course, your program's commands
might be different; for a GUI interface, you would use an "about box".

  You should also get your employer (if you work as a programmer) or school,
if any, to sign a "copyright disclaimer" for the program, if necessary.
For more information on this, and how to apply and follow the GNU GPL, see
<https://www.gnu.org/licenses/>.

  The GNU General Public License does not permit incorporating your program
into proprietary programs.  If your program is a subroutine library, you
may consider it more useful to permit linking proprietary applications with
the library.  If this is what you want to do, use the GNU Lesser General
Public License instead of this License.  But first, please read
<https://www.gnu.org/licenses/why-not-lgpl.html>.

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容