Skip to content

Instantly share code, notes, and snippets.

@manicminer
Last active August 29, 2015 14:07
Show Gist options
  • Save manicminer/e95580cc5e30ec616daf to your computer and use it in GitHub Desktop.
Save manicminer/e95580cc5e30ec616daf to your computer and use it in GitHub Desktop.
Conditional fail
---
- hosts: localhost
connection: local
tasks:
- set_fact:
is_test: "{{ env == 'development' or 'test' in env }}"
- set_fact:
security_group: blah
when: is_test
- set_fact:
security_group: blurh
when: not is_test
- debug: msg="is_test={{ is_test }}"
- debug: msg="security_group={{ security_group }}"
[root@localhost ansible]# ansible-playbook -i 'localhost,' test.yml -e 'env=prod'
PLAY [localhost] **************************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [set_fact ] *************************************************************
ok: [localhost]
TASK: [set_fact ] *************************************************************
skipping: [localhost]
TASK: [set_fact ] *************************************************************
skipping: [localhost]
TASK: [debug msg="is_test={{ is_test }}"] *************************************
ok: [localhost] => {
"msg": "is_test=False"
}
TASK: [debug msg="security_group={{ security_group }}"] ***********************
fatal: [localhost] => One or more undefined variables: 'security_group' is undefined
FATAL: all hosts have already failed -- aborting
PLAY RECAP ********************************************************************
to retry, use: --limit @/root/test.retry
localhost : ok=3 changed=0 unreachable=1 failed=0
[root@localhost ansible]# ansible-playbook -i 'localhost,' test.yml -e 'env=test'
PLAY [localhost] **************************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [set_fact ] *************************************************************
ok: [localhost]
TASK: [set_fact ] *************************************************************
ok: [localhost]
TASK: [set_fact ] *************************************************************
skipping: [localhost]
TASK: [debug msg="is_test={{ is_test }}"] *************************************
ok: [localhost] => {
"msg": "is_test=True"
}
TASK: [debug msg="security_group={{ security_group }}"] ***********************
ok: [localhost] => {
"msg": "security_group=blah"
}
PLAY RECAP ********************************************************************
localhost : ok=5 changed=0 unreachable=0 failed=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment