Guest User

Untitled

a guest
Jan 20th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. ---
  2. # This has been tested with ansible 1.3 with these commands:
  3. # ansible-playbook -i hosts ansible_conditionals_examples.yaml --extra-vars="hosts=myhosts isFirstRun=false"
  4. # ansible-playbook -i hosts ansible_conditionals_examples.yaml --extra-vars="hosts=myhosts isFirstRun=true"
  5. # ansible-playbook -i hosts ansible_conditionals_examples.yaml --extra-vars="hosts=myhosts"
  6. # NB: The type of the variable is crucial!
  7.  
  8. - name: Ansible Conditionals Examples
  9. hosts: $hosts
  10. vars_files:
  11. - vars.yml
  12. tasks:
  13. ########### Correct COMPLEX expressions ###################
  14. # Need parentheses around $is_live, because it (may) contain an _or_, and _and_ has higher prio than _or_;
  15. - name: ($is_live) and (isFirstRun == 'true') # OK only if isFirstRun is defined.
  16. command: echo hello
  17. when: ($is_live) and (isFirstRun == 'true')
  18.  
  19. # isFirstRun is a string var, whereas is_mgmt is an expression evaluating to a string, so it seems we need the $ to dereference it
  20. - name: $is_mgmt and (isFirstRun == 'true') # OK only if isFirstRun is defined
  21. command: echo hello
  22. when: $is_mgmt and (isFirstRun == 'true')
  23.  
  24. - name: $is_mgmt and (isFirstRun != 'false') # OK only if isFirstRun is defined
  25. command: echo hello
  26. when: $is_mgmt and (isFirstRun != 'false')
  27.  
  28. - name: $is_mgmt and (not isFirstRun == 'false') # OK only if isFirstRun is defined
  29. command: echo hello
  30. when: $is_mgmt and (not isFirstRun == 'false')
  31. ########### SIMPLE expressions ############################
  32. - name: is_mgmt # OK
  33. command: echo hello
  34. when: is_mgmt
  35.  
  36. - name: $is_mgmt # OK
  37. command: echo hello
  38. when: $is_mgmt
  39.  
  40. - name: ($is_mgmt) # OK
  41. command: echo hello
  42. when: ($is_mgmt)
  43.  
  44. - name: (is_mgmt) # WRONG - Always runs
  45. command: echo hello
  46. when: (is_mgmt)
  47. ######
  48. - name: isFirstRun == 'true' # OK
  49. command: echo hello
  50. when: isFirstRun == 'true'
  51.  
  52. - name: not isFirstRun != 'true' # OK
  53. command: echo hello
  54. when: not isFirstRun != 'true'
  55.  
  56. - name: not isFirstRun == 'false' # OK
  57. command: echo hello
  58. when: not isFirstRun == 'false'
  59.  
  60. - name: isFirstRun != 'false' # OK
  61. command: echo hello
  62. when: isFirstRun != 'false'
  63.  
  64. - name: $isFirstRun == 'true' # WRONG - $isFirstRun is interpreted as true and true is not equal to 'true' so only works for negative match
  65. command: echo hello
  66. when: $isFirstRun == 'true'
  67. ########### All wrong #####################################
  68. - name: ($is_live == True) and (isFirstRun == 'true') # WRONG - Only first live node on true (none of false (correct))
  69. command: echo hello
  70. when: ($is_live == True) and (isFirstRun == 'true')
  71.  
  72. - name: $is_live and (isFirstRun == 'true') # WRONG - Correct when true, but only first live when false
  73. # ['$inventory_hostname' == 'tsrvuweblive71' or '$inventory_hostname' == 'tsrvuweblive72' and (isFirstRun == 'true')]
  74. command: echo hello
  75. when: $is_live and (isFirstRun == 'true')
  76.  
  77. - name: is_live and (isFirstRun == 'true') # WRONG - All nodes run on true (none of false (correct))
  78. command: echo hello
  79. when: is_live and (isFirstRun == 'true')
  80.  
  81. - name: ($is_live == 'True') and (isFirstRun == 'true') # WRONG - Only first live node on true (none on false (correct))
  82. command: echo hello
  83. when: ($is_live == 'True') and (isFirstRun == 'true')
  84.  
  85. - name: (is_live == 'True') and (isFirstRun == 'true') # WRONG - No nodes run on niether true nor false - is_live not dereferenced
  86. command: echo hello
  87. when: (is_live == 'True') and (isFirstRun == 'true')
  88. ######
  89. - name: is_mgmt and (isFirstRun == 'true')
  90. # WRONG - All run on true
  91. command: echo hello
  92. when: is_mgmt and (isFirstRun == 'true')
  93.  
  94. - name: is_mgmt and (isFirstRun != 'false')
  95. # WRONG - All run on true
  96. command: echo hello
  97. when: is_mgmt and (isFirstRun != 'false')
  98.  
  99. - name: is_mgmt and (not isFirstRun == 'false')
  100. # WRONG - All run on true
  101. command: echo hello
  102. when: is_mgmt and (not isFirstRun == 'false')
  103.  
  104. - name: mgmt and isFirstRun equal true with ()
  105. # WRONG - Does not run when isFirstRun is 'false' (which is correct), but runs all nodes when 'true'
  106. command: echo hello
  107. when: ((is_mgmt) and (isFirstRun == 'true'))
  108.  
  109. - name: mgmt and isFirstRun not_equal true with $
  110. # WRONG - Always runs mgmt irrespective of value of isFirstRun
  111. command: echo hello
  112. when: $is_mgmt and $isFirstRun != 'true'
  113.  
  114. - name: mgmt and not isFirstRun equal true with $
  115. # # WRONG - Always runs mgmt irrespective of value of isFirstRun
  116. command: echo hello
  117. when: $is_mgmt and not $isFirstRun == 'true'
  118.  
  119. - name: mgmt and isFirstRun equal true with $
  120. # WRONG - Never runs any node
  121. command: echo hello
  122. when: $is_mgmt and $isFirstRun == 'true'
  123.  
  124. - name: mgmt and not isFirstRun equal true with only_if and $
  125. # WRONG - All nodes run
  126. command: echo hello
  127. only_if: "'$is_mgmt' and not '$isFirstRun' == 'true'"
  128.  
  129. - name: mgmt and isFirstRun not_equal true with only_if and $
  130. # WRONG - All nodes run
  131. command: echo hello
  132. only_if: "'$is_mgmt' and '$isFirstRun' != 'true'"
  133.  
  134. - name: mgmt and isFirstRun not_equal true with ()
  135. # WRONG - All nodes run when isFirstRun is false and no nodes run when isFirstRun is true
  136. command: echo hello
  137. when: ((is_mgmt) and (isFirstRun != 'true'))
  138.  
  139. - name: mgmt and isFirstRun not_equal true
  140. # WRONG - All nodes run when isFirstRun is false and no nodes run when isFirstRun is true
  141. command: echo hello
  142. when: is_mgmt and isFirstRun != 'true'
  143.  
  144. - name: mgmt and not isFirstRun equal true
  145. # WRONG - All nodes run when isFirstRun is false and no nodes run when isFirstRun is true
  146. command: echo hello
  147. when: is_mgmt and not isFirstRun == 'true'
  148.  
  149. - name: mgmt and not isFirstRun equal true with ()
  150. # WRONG - All nodes run when isFirstRun is false and no nodes run when isFirstRun is true
  151. command: echo hello
  152. when: ((is_mgmt) and (not isFirstRun == 'true'))
  153.  
  154. - name: mgmt and not isFirstRun equal true with only_if, $ and ()
  155. # ERROR Conditional expression must evaluate to True or False: ($is_mgmt) and (not '$isFirstRun' == 'true')
  156. command: echo hello
  157. only_if: "($is_mgmt) and (not '$isFirstRun' == 'true')"
Add Comment
Please, Sign In to add comment