Reputation: 87
I got an Ansible role that Im wanting to execute a psql script with. The name of the role is "db_scripts" and I will list the contents of respective directories/files below.
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
I run the playbook "run_sqls" as follows.
ansible-playbook run_sqls.yml -i inventories/app/production/inventory
What I want to understand is that how I should specify the values for following environment vars so that they can be dynamic and can be used by the hosts of other environments i.e: development etc..
PGDATABASE: "{{ db_name }}"
PGUSER: "{{ db_user }}"
PGHOST: "{{ db_host }}"
PGPORT: "{{ db_port }}"
Specifying them in respective inventory file doesn't seem to work,playbook errors out for "undefined vars" which make sense. Following is how I've attempted. /app/production/inventory
[dbservers]
192.168.1.100 db_name=mycompany db_user=abc db_port=3308
The error when the playbook is run
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"}
Please note when the values for the said env vars are specified under "db_scripts/default/main.yml" ,playbook Runs perfectly. Really appreciate some inputs. I've just started with ansible so please be as simple as you can be with examples. Thanks in advance :)
Upvotes: 0
Views: 1475
Reputation: 68024
[dbservers]
192.168.1.100 db_name=mycompany db_user=abc db_port=3308
Q: "Specifying them (vars) in respective inventory file doesn't seem to work, playbook errors out for "undefined vars" which make sense."
A: The variables from this inventory should work as expected. Simply test it
- hosts: dbservers
tasks:
- debug:
msg:
- '{{ db_name }}'
- '{{ db_user }}'
- '{{ db_host }}'
- '{{ db_port }}'
There are 2 problems
1) db_host
is missing in the inventory
2) The path in command inventories/app/production/inventory
is different from the path described /app/production/inventory
ansible-playbook run_sqls.yml -i inventories/app/production/inventory
Upvotes: 0