Skip to content

Instantly share code, notes, and snippets.

@michaeldiamant
Created December 10, 2015 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaeldiamant/0cbab8984b6c84daa9c7 to your computer and use it in GitHub Desktop.
Save michaeldiamant/0cbab8984b6c84daa9c7 to your computer and use it in GitHub Desktop.
Ansible unable to check for undefined vars using with_items
[michael@dca-infra01 ansible]$ cat /home/michael/ntoggle/ansible/roles/ntoggle.helix-service/tasks/required_vars.yml - name: check required variables are defined
fail: msg="Variable '{{ item }}' is undefined"
when: item is not defined
with_items:
- foo
- bar
- fail: msg="foo"
when: foo is not defined
[michael@dca-infra01 ansible]$ cat /home/michael/ntoggle/ansible/roles/ntoggle.helix-service/tasks/main.yml
---
# All of these MUST be set by each individual DSP environment.
- command: echo 'hello'
#- debug: msg="{{bar}}"
- include: required_vars.yml
- command: echo 'bye'
[michael@dca-infra01 ansible]$ cat inventory/ntoggle/group_vars/loadtest.yml
#foo: "test"
#bar: "2"
[michael@dca-infra01 ansible]$ ansible-playbook -i inventory/ntoggle playbooks/helix/engine.yml --extra-vars="target=loadtest" --module-path ./modules
[DEPRECATION WARNING]: Instead of sudo/sudo_user, use become/become_user and make sure become_method is 'sudo' (default). This feature will be
removed in a future release. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
[WARNING]: Slack Webhook URL was not provided. The Slack Webhook URL can be provided using the `SLACK_WEBHOOK_URL` environment variable.
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [loadtest2.internal.ntoggle.com]
TASK [datadog_event priority=low text=BEGIN - Nroute deployment api_key={{datadog_api_key}} title=Deployment of helix] ***
changed: [loadtest2.internal.ntoggle.com]
TASK [ntoggle.helix-service : command] *****************************************
changed: [loadtest2.internal.ntoggle.com]
TASK [ntoggle.helix-service : include] *****************************************
included: /home/michael/ntoggle/ansible/roles/ntoggle.helix-service/tasks/required_vars.yml for loadtest2.internal.ntoggle.com
TASK [ntoggle.helix-service : check required variables are defined] ************
skipping: [loadtest2.internal.ntoggle.com] => (item=foo)
skipping: [loadtest2.internal.ntoggle.com] => (item=bar)
TASK [ntoggle.helix-service : fail msg=foo] ************************************
fatal: [loadtest2.internal.ntoggle.com]: FAILED! => {"changed": false, "failed": true, "msg": "foo"}
PLAY RECAP *********************************************************************
loadtest2.internal.ntoggle.com : ok=4 changed=2 unreachable=0 failed=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment