jcalazan / ansible-django-stack

Ansible Playbook for setting up a Django app with Nginx, Gunicorn, PostgreSQL, Celery, RabbitMQ, Supervisor, Virtualenv, and Memcached. A Vagrantfile for provisioning a VirtualBox virtual machine is included as well.
MIT License
1.37k stars 392 forks source link

'db_name' is undefined\ #186

Closed vrr-data closed 1 year ago

vrr-data commented 2 years ago

Hi, Was trying this out pretty much unchanged on Windows 10 Pro Host with Ubuntu 20.04 Guest. Only change I made was renaming the VM and using ansible_local in the vagrant file. I am using VirtualBox provider not docker.

I get the following error during PostgeSQL database creation. The group_vars/vagrant/vars.yaml has the application name set. The db/tasks/main doesn't seem to reference the group_vars. Not sure if I am doing something wrong or if this is a bug. Please check and let me know.

TASK [db : Install PostgreSQL] *************************************************
task path: /vagrant/roles/db/tasks/main.yml:15
ok: [pavillion] => {"cache_update_time": 1646011715, "cache_updated": true, "changed": false}

TASK [db : Ensure the PostgreSQL service is running] ***************************
task path: /vagrant/roles/db/tasks/main.yml:25
ok: [pavillion] => {"changed": false, "name": "postgresql", "status": {"enabled": {"changed": false, "rc": null, "stderr": null, "stdout": null}, "started": {"changed": false, "rc": null, "stderr": null, "stdout": null}}}

TASK [db : Ensure database is created] *****************************************
task path: /vagrant/roles/db/tasks/main.yml:35
fatal: [pavillion]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'db_name' is undefined\n\nThe error appears to be in '/vagrant/roles/db/tasks/main.yml': line 35, 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: Ensure database is created\n  ^ here\n"}

PLAY RECAP *********************************************************************
pavillion                  : ok=11   changed=0    unreachable=0    failed=1    skipped=6    rescued=0    ignored=0

Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.
YPCrumble commented 2 years ago

What is the structure of your group_vars directory? Note that you need to name the subdirectory environment_name, so in your case I believe it should be group_vars/pavillion/vars.yml. See https://github.com/jcalazan/ansible-django-stack#configuring-your-application

Next thing to do would be to run ansible in debug mode and show the output of that here and I'd be happy to help if I can!

vrr-data commented 2 years ago

Thanks for your response and sorry for the long delay. I fell ill for a while but I better now. I changed the group_vars to be group_vars/pavillion/vars.yaml I still see the same error. Other than project_name and app_name I have not changed anything in vars.yml


TASK [db : Ensure the PostgreSQL service is running] *** task path: /vagrant/roles/db/tasks/main.yml:25 ok: [pavillion] => {"changed": false, "name": "postgresql", "status": {"enabled": {"changed": false, "rc": null, "stderr": null, "stdout": null}, "started": {"changed": false, "rc": null, "stderr": null, "stdout": null}}}

TASK [db : Ensure database is created] ***** task path: /vagrant/roles/db/tasks/main.yml:35 fatal: [pavillion]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'db_name' is undefined\n\nThe error appears to be in '/vagrant/roles/db/tasks/main.yml': line 35, 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: Ensure database is created\n ^ here\n"}

PLAY RECAP ***** pavillion : ok=11 changed=7 unreachable=0 failed=1 skipped=6 rescued=0 ignored=0

Ansible failed to complete successfully. Any error output should be visible above. Please fix these errors and try again.


Line 35 in main.yml is related to the check that fails above.

Now figuring out how to run ansible_local in debug mode.

vrr-data commented 2 years ago

I added the line debugger: on_failed under line 35 above in main.yml and I get this... I just ran vagrant provision since the VM was already up.

TASK [db : Add the PostgreSQL repository key to the apt trusted keys] ** task path: /vagrant/roles/db/tasks/main.yml:3 ok: [pavillion] => {"before": ["93C4A3FD7BB9C367", "7FCC7D46ACCC4CF8", "3B4FE6ACC0B21F32", "D94AA3F0EFE21092", "871920D1991BC93C"], "changed": false, "fp": "7FCC7D46ACCC4CF8", "id": "7FCC7D46ACCC4CF8", "key_id": "7FCC7D46ACCC4CF8", "short_id": "ACCC4CF8"}

TASK [db : Add the PostgreSQL repository to the apt sources list] ** task path: /vagrant/roles/db/tasks/main.yml:7 ok: [pavillion] => {"changed": false, "repo": "deb http://apt.postgresql.org/pub/repos/apt/ focal-pgdg main", "state": "present"}

TASK [db : Ensure locale is available] ***** task path: /vagrant/roles/db/tasks/main.yml:12 redirecting (type: modules) ansible.builtin.locale_gen to community.general.locale_gen ok: [pavillion] => {"changed": false, "msg": "OK", "name": "en_US.UTF-8"}

TASK [db : Install PostgreSQL] ***** task path: /vagrant/roles/db/tasks/main.yml:15 ok: [pavillion] => {"cache_update_time": 1647717386, "cache_updated": true, "changed": false}

TASK [db : Ensure the PostgreSQL service is running] *** task path: /vagrant/roles/db/tasks/main.yml:25 ok: [pavillion] => {"changed": false, "name": "postgresql", "status": {"enabled": {"changed": false, "rc": null, "stderr": null, "stdout": null}, "started": {"changed": false, "rc": null, "stderr": null, "stdout": null}}}

TASK [db : Ensure database is created] ***** task path: /vagrant/roles/db/tasks/main.yml:35 fatal: [pavillion]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'db_name' is undefined\n\nThe error appears to be in '/vagrant/roles/db/tasks/main.yml': line 35, 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: Ensure database is created\n ^ here\n"} [pavillion] TASK: db : Ensure database is created (debug)> Use exit() or Ctrl-D (i.e. EOF) to exit [pavillion] TASK: db : Ensure database is created (debug)> User interrupted execution Ansible failed to complete successfully. Any error output should be visible above. Please fix these errors and try again.

YPCrumble commented 2 years ago

What does your vars.yml file look like?

vrr-data commented 1 year ago

No change from original except to rename django_default_project to my project name on Line #5 and #6 and commenting out Lines #13 and #14 to use offical ppa and default python.

YPCrumble commented 1 year ago

@vrr-data are you using the latest version of this playbook? We changed the way variables are handled such that we now use the group_vars directory for variables. If you are using this version I'd be happy to try to help you debug!

prince-tanapong commented 1 year ago

@YPCrumble

I have the same problem, I just clone and try to test provision the server This is a development inventory file image

This is group vars file structure look like image

This is common I ran ansible-playbook -i development webservers.yml and this is an error image

I dont think ansible pick up the group var from group_vars/development/vars.yml, I explored to webserver.yml file the host is set to webservers image So I try to change file name to group_vars/webservers/var.yml it worked fine So I still confuse about the project structure, Or do I do something wrong? what is your expected how env and group_vars work together?

I expect that ansible should pick up group_vars from group_vars/{env}/vars and if I have multiple env I just should create inventory per environment and it should pick up the group_vars correctly? Do you have an example project that config this and work collectively so I can try to follow your this repo practice

YPCrumble commented 1 year ago

@prince-tanapong thank you for the detailed report! I just added #188 - would you mind trying that to see if it doesn't solve your issue? I'm surprised that the test suite passed when we first merged this in if the db_name variable doesn't get defined, but it could be some discrepancy between the development inventory and whatever inventory we're using for Molecule tests.