Reputation: 369
Have a role with the following task that is supposed to create a cron job if my_environment var is "prod".
I would like the playbook to skip this task rather than failing, when the condition is not satisfied:
---
- name: Configure cron job to export patch logs
cron:
name: export patch logs daily
minute: 0
hour: 0
user: root
cron_file: patch_logs
job: "/usr/local/bin/aws s3 cp /var/log/dpkg.log s3://{{ patch_logs_bucket }}/dpkg.log.$(hostname).$(date +\\%F)"
when: my_environment == "prod"
Failure message:
TASK [ansible-contxt-base : Configure cron job to export patch logs] ***********
fatal: [ubuntu-1604]: FAILED! => {"msg": "The conditional check 'scx_environment == \"prod\"' failed. The error was: error while evaluating conditional (scx_environment == \"prod\"): 'my_environment' is undefined\n\nThe error appears to have been in '/**<path>**/tasks/base.yml': line 125, 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: Configure cron job to export patch logs\n ^ here\n"}
Upvotes: 1
Views: 2448
Reputation: 1920
found here https://ansible-docs.readthedocs.io/zh/stable-2.0/rst/playbooks_conditionals.html#the-when-statement
- shell: echo "This certainly isn't epic!"
when: not epic
or
- fail: msg="Bailing out. this play requires 'bar'"
when: bar is undefined
Upvotes: 0
Reputation: 369
We can use the jinja2 shortcut for 'default(False) ’: 'd(False)’.
when: my_environment|d(False) == "prod"
Upvotes: 2