WHEN CONDITION

83 views
Skip to first unread message

bablu

unread,
Jan 30, 2017, 5:24:04 AM1/30/17
to Ansible Project

I want to use the following condition in ansible playbook.

    Scenario is as follows.

        If  arg1 > 5
        follow some steps
        else
        endif;

IF ELSE is not possible in ansible. However, how can i achive with the WHEN condition.

Brian Coca

unread,
Jan 30, 2017, 9:29:33 AM1/30/17
to ansible...@googlegroups.com
Just add a when to the tasks you want in the 'if'
when: arg1 > 5

for the 'else' just use the opposite condition.

when: arg1 <=5

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

bablu

unread,
Jan 30, 2017, 1:02:08 PM1/30/17
to Ansible Project
I did create the small task. However, for defining variable (num, numami) in the same task is not working.

e.g

 tasks:
   - name: Condition statement  
     shell: date_string=`date +%d%b%G`; num=3; numami=5;
     when: $numami > $num
               echo Hi;

Brian Coca

unread,
Jan 30, 2017, 1:23:22 PM1/30/17
to ansible...@googlegroups.com
These are not valid variables for Ansible (not since 1.6).  Also, setting them in a shell will not make them available.

  when: $numami > $num


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

DHAVAL JAISWAL

unread,
Feb 1, 2017, 2:16:36 PM2/1/17
to ansible...@googlegroups.com
Where i can set this variable so that i can compare it in where condition.

Any small test scenario will help me to understand.

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/yNAVY48KdJ8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CACVha7fsDKmiP8DdnS6oqgbc3vp65gYXc-GFZmtiYFHmgWs8gw%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



--
Thanks & Regards
Dhaval Jaiswal

Rodrigo B Brasil

unread,
Feb 1, 2017, 2:46:53 PM2/1/17
to ansible...@googlegroups.com


[]s,
Rodrigo B Brasil

--
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-project+unsubscribe@googlegroups.com.

To post to this group, send email to ansible-project@googlegroups.com.

DHAVAL JAISWAL

unread,
Feb 6, 2017, 2:00:44 PM2/6/17
to ansible...@googlegroups.com
Tried below one. However, when condition is not correctly evaluating.

Even following condition true its executing other commands mentioned in other when condition.
current_image_state <= desire_image_state


  tasks:
   - name: Get the state of the Test version
     shell: alert1=`cat /tmp/test.log |  grep -e "pending" -e "failed" | wc -l`;
     register: image_state
     tags: image

   - name: Set the output of the Test state
     set_fact: current_image_state=image_state.stdout
     tags: Test1

   - name: Define our static value for the desired Test state
     set_fact: desire_image_state=0
     tags: Test2

   - name: Perform TASK1
     command: sh /tmp/test.sh
     when: current_image_state > desire_image_state
     tags: final

   - name: Message if couldnot able to perform TASK1
     debug: msg="Need to check the status of Pending image"
     when: current_image_state <= desire_image_state
     tags: message




For more options, visit https://groups.google.com/d/optout.

Johannes Kastl

unread,
Feb 7, 2017, 7:04:37 AM2/7/17
to ansible...@googlegroups.com
On 06.02.17 20:00 DHAVAL JAISWAL wrote:
> Tried below one. However, when condition is not correctly evaluating.

Please show the exact error message. Or the exact output, maybe with -v.

> tasks:
> - name: Get the state of the Test version
> shell: alert1=`cat /tmp/test.log | grep -e "pending" -e "failed" | wc
> -l`;
> register: image_state
> tags: image

Add a debug task here (with correct indentation):
- debug: var="image_state"

> set_fact: current_image_state=image_state.stdout

I guess here is your error. You set the variable current_image_state
to a text string called "image_state.stdout". Add double curly braces
and quotes:

set_fact: current_image_state="{{ image_state.stdout }}"

> - name: Define our static value for the desired Test state
> set_fact: desire_image_state=0

I am not sure if thus sets desire_image_state to an integer or a string.

Johannes

signature.asc

DHAVAL JAISWAL

unread,
Feb 7, 2017, 8:16:32 AM2/7/17
to ansible...@googlegroups.com
Thanks !!

It works, post adding set_fact: current_image_state="{{ image_state.stdout }}".

--
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/yNAVY48KdJ8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages