Ansible 在 Ansible Galaxy 分享 Roles(上)

2018-07-25 11:05 更新

在「Ansible 用 Roles 部署 LNMP 网页应用程式(下)」里,我们写好了部署 TestLink 的 Playbooks,接下来冻仁将分享上传至 Galaxy 前的注意事项。

automate_with_ansible_practice-28.jpg

怎么把 Playbooks 打包成 Roles?

范例 lab/ch23/testlink 其实就是个把 Playbooks 拆开的 Role,它真正的程式进入点为 tasks/main.yml,而 setup.yml 只是为了测试 Playbooks 的正确性而存在的。这也是为什么冻仁会用 setup.yml include tasks/main.yml,再用 tasks/main.yml include 其它 tasks 的原因。

在「Ansible 维护大型的 Playbooks」一文时,冻仁建议大家把 Playbooks 给拆开,为的就是要把它打包成 Roles。

$ tree -L 2
.
├── README.md
├── Vagrantfile
├── defaults
│   └── main.yml
├── handlers
│   └── main.yml
├── requirements.yml
├── setup.yml
├── tasks
│   ├── check.yml
│   ├── main.yml
│   ├── setting_nginx.yml
│   ├── setting_php-fpm.yml
│   ├── setting_testlink.yml
│   └── setup_testlink.yml
└── templates
    ├── config_db.inc.php.j2
    ├── nginx-testlink.conf.j2
    ├── php7-cli.ini.j2
    └── php7-fpm.ini.j2

4 directories, 16 files
  • 此例的 requirements.ymlsetup.yml 只有在作为 Playbooks 用时才会被使用。

好的 Roles 应具备什么?

  1. 在 README.md 里提供清楚的说明文件。
  2. 在 Meta Data (meta/main.yml) 提供正确的资讯。
  3. 在 Meta Data (meta/main.yml) 写入 Roles 的相依性 (Dependencies) 设定。
  4. 命名变数时,使用 Role 的名称作为变数的前缀。
  5. 借由 Travis CI 整合测试 Roles。(more)

    就上面的例子而言,我们会用 setup.yml 来进行测试。

README.md

  1. 使用 ansible-galaxy init 指令建立 Role 时会一并产生的档案之一。
  2. 使用 Markdown 格式编写。
  3. 内容会显示在 README 页面里:在 Galaxy 上看到的样子与 GitHub 相同。
$ cat README.md
Role Name
=========

A brief description of the role goes here.

Requirements
------------

Any pre-requisites that may not be covered by Ansible itself or the role should be mentioned here. For instance, if the role uses the EC2 module, it may be a good idea to mention in this section that the boto package is required.

Role Variables
--------------

A description of the settable variables for this role should go here, including any variables that are in defaults/main.yml, vars/main.yml, and any variables that can/should be set via parameters to the role. Any variables that are read from other roles and/or the globalscope (ie. hostvars, group vars, etc.) should be mentioned here as well.

Dependencies
------------

A list of other roles hosted on Galaxy should go here, plus any details in regards to parameters that may need to be set for other roles, or variables that are used from other roles.

Example Playbook
----------------

Including an example of how to use your role (for instance, with variables passed in as parameters) is always nice for users too:

    - hosts: servers
      roles:
         - { role: username.rolename, x: 42 }

License
-------

BSD

Author Information
------------------

An optional section for the role authors to include contact information, or a website (HTML is not allowed).

▲ 此为 ansible-galaxy init 产生的 READNE.md 范例。

Meta Data (meta/main.yml)

  1. 使用 ansible-galaxy init 指令建立 Role 时会一并产生的档案之一。
  2. 使用 YAML 格式编写。
  3. 內容会显示在 Details 页面里面。
$ cat meta/main.yml
galaxy_info:
  author: chusiang
  description: Deploy TestLink with Nginx, PHP 7 (php-fpm) and MySQL 5.6 on Ubuntu and Debian.
  company: commandp Inc.
  license: MIT
  min_ansible_version: 2.1.2.0
  platforms:
  - name: Ubuntu
    versions:
    - trusty
  - name: Debian
    versions:
    - jessie

  galaxy_tags:
    - testlink
    - cms

dependencies:
  - williamyeh.nginx
  - chusiang.php7
  - geerlingguy.mysql

▲ 此为 chusiang.testlink role 的 meta/main.yml

Dependencies

  1. 若相依于其它的 role,请于 meta/main.yml 的 dependencies 下方补上。
  2. 使用 YAML 格式编写。
  3. 关于 Role Dependencies 可参考 Playbook Roles and Include Statements | Ansible Documentation 一文。
$ cat meta/main.yml
...
dependencies:
  - williamyeh.nginx
  - chusiang.php7
  - geerlingguy.mysql

▲ 此为 chusiang.testlink role 的 meta/main.yml

以上就是要上传到 Galaxy 前该特别留意的地方,下章冻仁将讲解怎么把 Roles 上传至 Galaxy。

相关连结


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号