【发布时间】:2019-11-08 21:19:35
【问题描述】:
问题
我有一本想要使用特定标签运行的剧本。目前,它因未定义的变量错误而失败。该变量在我包含在 main.yml 中的文件中定义,但它对我通过“import_tasks”函数包含的另一个 yml 文件不可见。
我的解决方法是在我引用变量的任何地方都包含相同的 var 文件。但我想知道是否有更简单的方法来做到这一点/如果我的方法是错误的。
YML 代码
我的剧本有以下文件夹结构:
tasks/main.yml
/initialize-postgresql.yml
/setup-db.yml
vars/debos.yml
debos.yml var 文件如下所示:
__postgresql_version: "12"
__postgresql_daemon: "postgresql"
__postgresql_data_dir: "/var/lib/postgresql/{{ __postgresql_version }}/data"
__postgresql_bin_path: "/usr/lib/postgresql/"
__postgresql_config_path: "/etc/postgresql/"
__postgresql_user: "postgres"
__postgresql_group: "postgres"
__postgresql_packages:
- postgresql
- postgresql-contrib
在 main.yml 中,我有以下代码(仅包括相关部分):
- delegate_to: "{{ inventory_hostname }}"
block:
# Variable configuration.
- include_vars: debos.yml
#Using import_ vs. include_ for the sake of tags.
- import_tasks: initialize-postgresql.yml
tags: init-db
initialize-postgresql.yml 部分看起来像这样:
---
# Variable configuration.
- include_vars: alpine.yml
- name: define local vars
set_fact:
postgresql_data_dir: "{{ __postgresql_data_dir }}"
postgresql_group: "{{ __postgresql_group }}"
postgresql_user: "{{ __postgresql_user }}"
postgresql_config_path: "{{ __postgresql_config_path }}"
postgresql_daemon: "{{ __postgresql_daemon }}"
为了能够运行可以用这样的标签玩书:
ansible-playbook -i inventory/dbs installpsqldb.yml --tags "init-db"
我必须在 initialize-postgresql.yml 文件中再次包含 debos.yml 文件。如果不是,我会收到有关未定义变量的错误。
最终在任务中,我将拥有更多的 yml 文件,它们都将使用相同的变量。所以我想确保我已经正确设置了正确的位置:
- 能够将任务拆分为单独的 yml 文件。
- 通过标记名调用 yml 文件。
谢谢。
【问题讨论】:
标签: ansible