ansistrano / deploy

Ansible role to deploy scripting applications like PHP, Python, Ruby, etc. in a capistrano style
https://ansistrano.com
MIT License
2.37k stars 343 forks source link

The task includes an option with an undefined variable. #390

Closed axelrindle closed 2 years ago

axelrindle commented 2 years ago

This is my playbook:

---
- name: Deploy API
  hosts: all
  vars:
    ansistrano_deploy_from: "{{ playbook_dir }}/../dist.tar.gz"
    ansistrano_deploy_to: "/var/www/api"
    ansistrano_keep_releases: 5
    ansistrano_deploy_via: copy_unarchive
    ansistrano_before_update_code_tasks_file: "{{ playbook_dir }}/hooks/before-code-update.yml"
    ansistrano_after_update_code_tasks_file: "{{ playbook_dir }}/hooks/after-code-update.yml"
  roles:
    - { role: ansistrano.deploy }

before-code-update.yml:

- name: Create database backup
  ansible.builtin.command:
    cmd: "./artisan backup:run --only-db"
    chdir: "{{ ansistrano_release_path.stdout }}"

And here is the output:

``` $ ansible-playbook -i .ci/hosts.yml -e "deploy_environment=stage" .ci/deploy.yml PLAY [Deploy API] ************************************************************************************************************************************************************************ TASK [Gathering Facts] ******************************************************************************************************************************************************************* ok: [my.web.server] TASK [ansistrano.deploy : include_tasks] ************************************************************************************************************************************************* TASK [ansistrano.deploy : include_tasks] ************************************************************************************************************************************************* included: /home/axel/.ansible/roles/ansistrano.deploy/tasks/setup.yml for my.web.server TASK [ansistrano.deploy : ANSISTRANO | Ensure deployment base path exists] *************************************************************************************************************** ok: [my.web.server] TASK [ansistrano.deploy : ANSISTRANO | Ensure releases folder exists] ******************************************************************************************************************** ok: [my.web.server] TASK [ansistrano.deploy : ANSISTRANO | Ensure shared elements folder exists] ************************************************************************************************************* ok: [my.web.server] TASK [ansistrano.deploy : ANSISTRANO | Ensure shared paths exists] *********************************************************************************************************************** TASK [ansistrano.deploy : ANSISTRANO | Ensure basedir shared files exists] *************************************************************************************************************** TASK [ansistrano.deploy : include_tasks] ************************************************************************************************************************************************* TASK [ansistrano.deploy : include_tasks] ************************************************************************************************************************************************* included: .../hooks/before-code-update.yml for my.web.server TASK [ansistrano.deploy : Create database backup] **************************************************************************************************************************************** fatal: [my.web.server]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansistrano_release_path' is undefined\n\nThe error appears to be in '.../hooks/before-code-update.yml': line 1, 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: Create database backup\n ^ here\n"} PLAY RECAP ******************************************************************************************************************************************************************************* my.web.server : ok=6 changed=0 unreachable=0 failed=1 skipped=2 rescued=0 ignored=0 ```

The pretty-formatted error message:

The task includes an option with an undefined variable. The error was: 'ansistrano_release_path' is undefined

The error appears to be in '.../hooks/before-code-update.yml': line 1, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- name: Create database backup
  ^ here

It works without the custom hooks, but then the app does not function. What am I doing wrong?

axelrindle commented 2 years ago

I got it working.

I found out that the variable {{ ansistrano_release_path }} is set in the update-code stage. So it's not available until after-code-update.

You should definitely update your documentation accordingly.