Ansible 安装示例

您已经学习了剧本 (playbooks)、清单 (inventory)、角色 (roles) 和变量 (variables)。本节将结合所有这些元素,并概述一个用于自动化 Web 服务的示例设置。

该示例设置根据功能组织剧本、角色、清单和包含变量的文件。在 play 和任务级使用标签 (Tags) 可以提供更细的粒度和控制。这是一种强大且灵活的方法,但组织 Ansible 内容还有其他方式。您对 Ansible 的使用应符合您的需求,因此请随意修改此方法并相应地组织您的内容。

示例目录布局

此布局将大多数任务组织在角色中,每个环境使用单个清单文件,并在顶层目录中放置少量剧本

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                  # main playbook
webservers.yml            # playbook for webserver tier
dbservers.yml             # playbook for dbserver tier
tasks/                    # task files included from playbooks
    webservers-extra.yml  # <-- avoids confusing playbook with task files
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 and optional Galaxy info
        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 假设您的剧本存储在一个目录中,而角色存储在一个名为 roles/ 的子目录中。随着需要自动化的任务增加,您可以考虑将剧本移至名为 playbooks/ 的子目录中。如果这样做,您必须在 ansible.cfg 文件中使用 roles_path 设置来配置 roles/ 目录的路径。

备选目录布局

您也可以将每个清单文件及其对应的 group_vars/host_vars 放在单独的目录中。如果您在不同环境中的 group_vars/host_vars 共同点不多,这尤其有用。布局可以像这个示例一样

inventories/
   production/
      hosts               # inventory file for production servers
      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

   staging/
      hosts               # inventory file for staging environment
      group_vars/
         group1.yml       # here we assign variables to particular groups
         group2.yml
      host_vars/
         stagehost1.yml   # here we assign variables to particular systems
         stagehost2.yml

library/
module_utils/
filter_plugins/

site.yml
webservers.yml
dbservers.yml

roles/
    common/
    webtier/
    monitoring/
    fooapp/

这种布局为更大的环境提供了更多的灵活性,并且实现了不同环境之间清单变量的完全隔离。然而,这种方法更难维护,因为文件更多。有关组织组变量和主机变量的更多信息,请参阅 组织主机和组变量

示例组变量和主机变量

这些包含变量的示例组和主机文件包含了适用于每台机器或一组机器的值。例如,亚特兰大的数据中心有自己的 NTP 服务器。因此,在设置 ntp.conf 文件时,可以使用与此示例类似的代码

---
# file: group_vars/atlanta
ntp: ntp-atlanta.example.com
backup: backup-atlanta.example.com

同样,webservers 组中的主机具有一些不适用于数据库服务器的配置

---
# file: group_vars/webservers
apacheMaxRequestsPerChild: 3000
apacheMaxClients: 900

默认值或普适的值应放在名为 group_vars/all 的文件中

---
# file: group_vars/all
ntp: ntp-boston.example.com
backup: backup-boston.example.com

如有必要,您可以在 host_vars 目录中定义系统特定的硬件差异

---
# file: host_vars/db-bos-1.example.com
foo_agent_port: 86
bar_agent_port: 99

如果您使用 动态清单,Ansible 会自动创建许多动态组。因此,像 class:webserver 这样的标签会自动从 group_vars/ec2_tag_class_webserver 文件中加载变量。

注意

您可以使用名为 hostvars 的特殊变量来访问主机变量。请参阅 特殊变量 以获取这些变量的列表。hostvars 变量只能访问主机特定变量,不能访问组变量。

按功能组织的示例剧本

通过此设置,单个剧本可以定义整个基础设施。site.yml 剧本导入了另外两个剧本:一个用于 Web 服务器,一个用于数据库服务器

---
# file: site.yml
- import_playbook: webservers.yml
- import_playbook: dbservers.yml

同样位于顶层的 webservers.yml 剧本将 webservers 组的配置映射到与 webservers 组相关的角色

---
# file: webservers.yml
- hosts: webservers
  roles:
    - common
    - webtier

通过此设置,您可以通过运行 site.yml 来配置整个基础设施。或者,要仅配置基础设施的一部分,请运行 webservers.yml。这类似于 Ansible 的 --limit 参数,但更加明确

ansible-playbook site.yml --limit webservers
ansible-playbook webservers.yml

基于功能的角色中的示例任务和处理器文件

Ansible 会加载角色子目录中任何名为 main.yml 的文件。此示例 tasks/main.yml 文件配置了 NTP

---
# file: roles/common/tasks/main.yml

- name: be sure ntp is installed
  yum:
    name: ntp
    state: present
  tags: ntp

- name: be sure ntp is configured
  template:
    src: ntp.conf.j2
    dest: /etc/ntp.conf
  notify:
    - restart ntpd
  tags: ntp

- name: be sure ntpd is running and enabled
  ansible.builtin.service:
    name: ntpd
    state: started
    enabled: true
  tags: ntp

这是一个处理器 (handlers) 文件示例。处理器仅在某些任务报告更改时被触发。处理器在每个 play 结束时运行

---
# file: roles/common/handlers/main.yml
- name: restart ntpd
  ansible.builtin.service:
    name: ntpd
    state: restarted

有关更多信息,请参阅 角色

示例设置可实现的功能

上述基础组织结构支持许多不同的自动化选项。要重新配置整个基础设施:

ansible-playbook -i production site.yml

要重新配置所有设备的 NTP:

ansible-playbook -i production site.yml --tags ntp

仅重新配置 Web 服务器:

ansible-playbook -i production webservers.yml

仅重新配置波士顿的 Web 服务器:

ansible-playbook -i production webservers.yml --limit boston

仅重新配置波士顿的前 10 台 Web 服务器,然后是接下来的 10 台:

ansible-playbook -i production webservers.yml --limit boston[0:9]
ansible-playbook -i production webservers.yml --limit boston[10:19]

示例设置还支持基本的 ad hoc 命令

ansible boston -i production -m ping
ansible boston -i production -m command -a '/sbin/reboot'

要发现哪些任务将运行,或者哪些主机名将受到特定 Ansible 命令的影响:

# confirm what task names would be run if I ran this command and said "just ntp tasks"
ansible-playbook -i production webservers.yml --tags ntp --list-tasks

# confirm what hostnames might be communicated with if I said "limit to boston"
ansible-playbook -i production webservers.yml --limit boston --list-hosts

为部署或配置进行组织

示例设置展示了典型的配置拓扑。在进行多层部署时,您可能需要一些额外的剧本在各层之间跳转以部署应用程序。在这种情况下,您可以使用类似于 deploy_exampledotcom.yml 的剧本来增强 site.yml。不过,一般概念仍然适用。使用 Ansible,您可以使用相同的实用程序进行部署和配置。因此,您可能会重复使用组,并将操作系统配置与应用程序部署保存在不同的剧本或角色中。

可以将“剧本”看作一个体育比喻——您可以有一套针对所有基础设施的通用战术。然后,您还有在不同时间、出于不同目的使用的情境战术。

使用本地 Ansible 模块

如果剧本相对于其 YAML 文件有一个 ./library 目录,您可以使用此目录自动将 Ansible 模块添加到模块路径中。这将模块与剧本组织在一起。例如,请参阅本节开始时的目录结构。

另请参阅

YAML 语法

学习 YAML 语法

使用 Playbook

复习基础剧本功能

集合索引

浏览现有的集合、模块和插件

您应该开发模块吗?

学习如何通过编写自己的模块来扩展 Ansible

模式:定位主机和组

学习如何选择主机

交流方式

有疑问?需要帮助?想分享你的想法?请访问 Ansible 通信指南