Skip to content

Instantly share code, notes, and snippets.

@base10
Created July 19, 2013 02:25
Show Gist options
  • Save base10/6034641 to your computer and use it in GitHub Desktop.
Save base10/6034641 to your computer and use it in GitHub Desktop.
Need help with testing definedness using 'when_unset' The key question. Why does when_unset not catch a variable as defined if it begins with a '$'?
---
- name: webserver plays
hosts: all
user: nathan
sudo: no
gather_facts: false
vars:
foo: onetwothree
bar: fourfivesix
err_variable_not_present: |
Required variables not present. Make sure you have ~/.wa-dev-ansible-vars defined
Use playbooks/group_vars/default as a starting point
pre_tasks:
- name: "ensure variables"
fail: >
msg="{{ err_variable_not_present }}"
when_unset: $item
with_items:
- $foo
- $bar
---
- name: webserver plays
hosts: all
user: nathan
sudo: no
gather_facts: false
vars:
foo: $onetwothree
bar: fourfivesix
err_variable_not_present: |
Required variables not present. Make sure you have ~/.wa-dev-ansible-vars defined
Use playbooks/group_vars/default as a starting point
pre_tasks:
- name: "ensure variables"
fail: >
msg="{{ err_variable_not_present }}"
when_unset: $item
with_items:
- $foo
- $bar
[auto-1115][~/code/infrastructure/wa-dev-vagrants] ansible-playbook -i fake-inventory playbooks/tmp.yml
PLAY [webserver plays] ********************************************************
TASK: [ensure variables] ******************************************************
skipping: [localhost] => (item=onetwothree)
skipping: [localhost] => (item=fourfivesix)
PLAY RECAP ********************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
[auto-1115][~/code/infrastructure/wa-dev-vagrants] ansible-playbook -i fake-inventory playbooks/tmp.yml
PLAY [webserver plays] ********************************************************
TASK: [ensure variables] ******************************************************
failed: [localhost] => (item=$onetwothree) => {"failed": true, "item": "$onetwothree"}
msg: Required variables not present. Make sure you have ~/.wa-dev-ansible-vars defined
Use playbooks/group_vars/default as a starting point
skipping: [localhost] => (item=fourfivesix)
FATAL: all hosts have already failed -- aborting
PLAY RECAP ********************************************************************
to retry, use: --limit @/var/tmp/ansible/tmp.retry
localhost : ok=0 changed=0 unreachable=0 failed=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment