freephile

ansible failed_when

Sep 10th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 1.90 KB | None | 0 0
  1. #### Running the task without condition shows us the 'stdout' msg
  2.  
  3. TASK [qualitybox : Pear | install pear mail module] ****************************
  4. ok: [wiki.example.com] => {
  5.     "changed": false,
  6.     "invocation": {
  7.         "module_args": {
  8.             "name": "Mail",
  9.             "state": "latest"
  10.         },
  11.         "module_name": "pear"
  12.     },
  13.     "msg": "package(s) already installed"
  14. }
  15.  
  16. TASK [qualitybox : debug] ******************************************************
  17. task path: /home/greg/src/ansible-digitalocean/roles/qualitybox/tasks/install_pear.yml:71
  18. ok: [wiki.example.com] => {
  19.     "msg": {
  20.         "changed": false,
  21.         "msg": "package(s) already installed"
  22.     }
  23. }
  24.  
  25. #### So we try to incorporate a 'changed_when' and 'failed_when' condition
  26.  
  27. - name: Pear | install pear mail module
  28.   pear: name=Mail state=latest
  29.   register: pear_result
  30.   changed_when: "'already installed' not in pear_result.stdout"
  31.   failed_when: "'already installed' not in pear_result.stdout and pear_result.stderr"
  32.  
  33. ###### But we get an error
  34.  
  35. fatal: [wiki.example.com]: FAILED! => {
  36.     "failed": true,
  37.     "msg": "The conditional check ''already installed' not in pear_result.stdout' failed. The error was: error while evaluating conditional ('already installed' not in pear_result.stdout): Unable to look up a name or access an attribute in template string ({% if 'already installed' not in pear_result.stdout %} True {% else %} False {% endif %}).\nMake sure your variable name does not contain invalid characters like '-': argument of type 'StrictUndefined' is not iterable"
  38. }
  39.  
  40.  
  41. ###### SOLVED: The reason this fails is because the ansible extras PEAR module does NOT return 'stdout'.  Instead there is the 'msg' attribute which we can test:
  42. ###### SEE https://github.com/ansible/ansible-modules-extras/blob/devel/packaging/language/pear.py
  43.  
  44.   changed_when: "'already installed' not in pear_result.msg"
Add Comment
Please, Sign In to add comment