最佳实践

概述

参考:

Playbook 目录结构的最佳示例

参考:

production                # 适用于 production 的 Inventory 文件
staging                   # 适用于 staging 的 Inventory 文件

group_vars/               # 在这里定义组的变量
   group1.yml             # 文件名以组名命名,group1.yml 是适用于 group1 组的变量
   group2.yml
host_vars/                # 在这里定义主机变量
   hostname1.yml          # 文件名以主机名命名,hostname1.yml 是适用于 hostname1 主机的变量
   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

# 当需要管理多个 Role 时,可以在 roles/ 目录中
roles/
    common/               # 名为 common 的角色
        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/              # 名为 webtier 的角色,其内的机构与 common 相同
        ......
    monitoring/           # 同上
        ......
    fooapp/               # 同上
        ......

group_vars 与 host_vars 目录

组变量与主机变量的文件除了可以放在 Palybook 的根目录,还可以放在存放 Inventory 文件的目录中,比如:

inventories/       # 这里存放 Inventory 目录,通过在命令行中使用 -i 选项以指定 Inventory 文件
   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/

至于 group_vars 与 host_vars 在不同目录的优先级可以参考 Ansible Variables - 变量的优先级 部分

Inventory 的变量实践

一、指定多个 inventory,并使用 –limit 限定主机

ansible-playbook -i inventory/fj-server.yaml -i inventory/fj-client.yaml deploy-monitoring.yaml --limit FJ-BS101-JMR-Monitor

模板使用方式(直接使用 groups, hostvars 这两个变量)

{% for target in groups['intf'] %}
  {{ hostvars[target]['ansible_host'] }}
{% endfor %}

二、使用 include_vars 加载

deploy-test.yaml

- hosts: fj-server
  gather_facts: no
  pre_tasks:
    - name: "加载 client inventory"
      ansible.builtin.include_vars:
        file: inventory/fj-client.yaml # 读取该文件,将其中内容作为变量使用
        name: client_inventory # 这些变量的父级字段名称
  roles:
    - test

tasts/main.yaml

- name: "检查变量"
  ansible.builtin.debug:
    msg: "{{ item['ansible_host'] }}"
  with_items:
    # 要使用 valuse() 函数
    - "{{ client_inventory['intf'].hosts.values() }}"

最后修改 January 22, 2026: ansible (695b6e20)