multiple when condition not working in playbook

59 views
Skip to first unread message

bablu

unread,
Jan 16, 2016, 3:55:53 PM1/16/16
to Ansible Project, dhav...@hotmail.com
Hi, 

Trying to write playbook with when conditional statement. 

However, it always choose last command (/home/user/<PATH2>/bin/) to execute even though condition matches with another domainname. 

playbook.yml

# command execution Ansible Playbooks
- hosts: "{{domainname}}"
  remote_user: <user>
  vars_prompt:
   - name: "domainname"
     prompt: "Enter domain name"
     private: no
  tasks:
   - name: Copy file to client
     copy: src=/tmp/{{file_name}} dest=/tmp/ owner=<user> group=user mode=0644
     become: yes
     become_method: su
  
  tasks:
   - name: installing something
     command: /home/user/<PATH1>/bin/
     become: yes
     become_method: su
     when:  ('"{{domainname}}" == "test.com"') or ('"{{domainname}}" == "test1.com"')
   - command: /home/user/<PATH2>/bin/
     become: yes
     become_method: su
     when:  ('"{{domainname}}" == "test5.com"') or ('"{{domainname}}" == "test5.com"')


While running above if i will enter test.com as a domain name still it will execute command (/home/user/<PATH2>/bin/) instead of (/home/user/<PATH1>/bin/)

Please correct me where i am making mistake.  


bablu

unread,
Jan 18, 2016, 5:42:44 AM1/18/16
to Ansible Project, dhav...@hotmail.com
 solved. 

Brian Coca

unread,
Jan 18, 2016, 8:25:15 AM1/18/16
to Ansible Project, dhav...@hotmail.com
don't use moustaches in the when:

when: (domainname == "test.com"') or (domainname == "test1.com)

though i suggest using:

when: domainname in ["test.com", "tes1.com"]
> --
> 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/36e2a39a-a977-4d07-9610-18fa98416a5b%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Brian Coca
Reply all
Reply to author
Forward
0 new messages