【发布时间】:2020-06-01 11:03:51
【问题描述】:
我有一个 Ansible 角色,我想用它来执行 psql 脚本。角色的名字是“db_scripts”,下面我会列出各个目录/文件的内容。
db_scripts/tasks/mail.yml
-
name: "Run the SQLS"
command: "psql -f \"{{ script }}\"/files/my_sql_script.sql"
environment:
PGDATABASE: "{{ db_name }}"
PGUSER: "{{ db_user }}"
PGHOST: "{{ db_host }}"
PGPORT: "{{ db_port }}"
delegate_to: localhost
db_scripts/files/my_sql_script.yml
insert into employees(name, contact) values (Jack, 5555555);
db_scripts/default/main.yml
script: "{{ role_path }}"
/app/production/inventory
[dbservers]
db_host=192.168.1.100 db_name=mycompany db_user=abc db_port=3308
/app/playbooks/run_sqls.yml
- name: Run sql
hosts: dbservers
roles:
- db_scripts
我按如下方式运行剧本“run_sqls”。
ansible-playbook run_sqls.yml -i inventories/app/production/inventory
我想了解的是我应该如何指定以下环境变量的值,以便它们可以是动态的并且可以被其他环境的主机使用,例如:开发等。
PGDATABASE: "{{ db_name }}"
PGUSER: "{{ db_user }}"
PGHOST: "{{ db_host }}"
PGPORT: "{{ db_port }}"
在各自的库存文件中指定它们似乎不起作用,“未定义的变量”的剧本错误是有道理的。以下是我尝试过的方式。 /app/production/inventory
[dbservers]
192.168.1.100 db_name=mycompany db_user=abc db_port=3308
playbook 运行时的错误
TASK [db_scripts : Run the SQLS] ******************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The field 'environment' has an invalid value, which includes an undefined variable. The error was: 'db_host' is undefined\n\nThe error appears to be in '/home/db_scripts/tasks/mail.yml': line 37, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n-\n name: \"Run the SQLS\"\n ^ here\n"}
请注意,当上述环境变量的值在“db_scripts/default/main.yml”下指定时 ,playbook 完美运行。非常感谢一些投入。我刚刚开始使用 ansible,所以请尽可能简单地使用示例。在此先感谢:)
【问题讨论】:
-
你说:
"... playbook errors out for "undefined vars" which make sense."能解释一下到底什么是有意义的吗?
标签: ansible ansible-inventory ansible-role