When conditional can be used with variables??

43 views
Skip to first unread message

NEW HOMIE

unread,
Mar 17, 2016, 8:10:20 AM3/17/16
to Ansible Project
Hello there~
Is this possible using when conditional with variables?

For instance, I use two kind of was (Jboss, Weblogic)

I create for checking process id some yml files like under line

check.yml
- shell: ps -ef | grep '{{ nodename }}' | grep XX | awk {'print $2'}
  register: pid
  ignore_errors: True
  when: {{ was }} == jboss

- shell: /usr/ucb/ps -auxwww | grep '{{ nodename }}' | grep XX | awk {'print $2'}
  register: pid
  ignore_errors: True
  when: {{ was }} == weblogic


and then, put this yml file in main.yml

main.yml
- include: check.yml
  tags: check

now I would execute this command

# ansible-playbook playbook -i inventory -t check -e component=somecomponent -e nodename=somenodename -e was=jboss


however It gives me under error message?
{"failed": true, "reason": "ERROR! Syntax Error while loading YAML.
The error appears to have been in 'check.yml': line 5, column 19, but maybe elsewhere in the file depending on the exact syntax problem.
The offending line appears to be: ignore_errors: True\n  when: {{ was }} == jboss^ here
We could be wrong, but this one looks like it might be an issue with missing quotes. 
Always quote template expression brackets when they start a value. For instance: with_items: - {{ foo }} Should be written as: with_items: - \"{{ foo }}\""}


What's problem in my check.yml??

Brian Coca

unread,
Mar 17, 2016, 6:08:52 PM3/17/16
to ansible...@googlegroups.com
you are writing your conditionals wrong, vars don't need  {{}} and strings need quoting:

  when: was == 'jboss'

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/c6f4ab7d-40bf-4eb8-850a-19a00ee6fd71%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
----------
Brian Coca

Arthur Reyes

unread,
Mar 17, 2016, 7:53:16 PM3/17/16
to Ansible Project
I might be misinformed but I believe that syntax will be deprecated soon and the best way to write that conditional is:
when: '{{ was }}' == 'jboss'

Brian Coca

unread,
Mar 17, 2016, 8:15:29 PM3/17/16
to ansible...@googlegroups.com
you are misinformed


----------
Brian Coca

Arthur Reyes

unread,
Mar 17, 2016, 8:21:59 PM3/17/16
to Ansible Project
Rereading the porting to 2.0 guide, there are a number of instances where bare variables do produce warnings of future deprecation, but when conditions are not mentioned there.

Apologies for any confusion I may have caused.

Reply all
Reply to author
Forward
0 new messages