'when condition fails in ansible
I have a simple playbook that loads data to different instances based on the DEPLOY_TO environment variable that is entered from jenkins which has the value dev and test. By comparing the {{ lookup('env','DEPLOY_TO') }} variable with dev or test using when condition
- name: load loadBase files into target mysql dev
shell: mysql -h {{ lookup('env','db_host_dev') }} -u {{ lookup('env','db_user_dev') }} --password={{ lookup('env','db_root_password_dev') }} {{ lookup('env','db_name') }} < /tmp/{{ item | basename }}
with_items: "{{ data.loadBase }}"
no_log: false
when: "{{ lookup('env','DEPLOY_TO') }} == dev"
- name: load loadBase files into target mysql test
shell: mysql -h {{ lookup('env','db_host_test') }} -u {{ lookup('env','db_user_test') }} --password={{ lookup('env','db_root_password_test') }} {{ lookup('env','db_name') }} < /tmp/{{ item | basename }}
with_items: "{{ data.loadBase }}"
no_log: false
when: "{{ lookup('env','DEPLOY_TO') }} == test"
But when running the pipeline to DEPLOY_TO either dev or test, it fails with the following error
18:34:15 [WARNING]: conditional statements should not include jinja2 templating
18:34:15 delimiters such as {{ }} or {% %}. Found: {{ lookup('env','DEPLOY_TO') }} ==
18:34:15 dev
18:34:15 fatal: [10.0.1.78]: FAILED! => {
18:34:15 "msg": "The conditional check '{{ lookup('env','DEPLOY_TO') }} == dev' failed. The error was: error while evaluating conditional ({{ lookup('env','DEPLOY_TO') }} == dev): 'dev' is undefined\n\nThe error appears to be in '/home/deploy/jenkins/workspace/infrastructure/deploy.yml': line 77, column 11, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: load loadBase files into target mysql dev\n ^ here\n"
May I please know what am I doing wrong?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
