When conditional not working in Ansible

0 votes

I am using the when conditional in ansible but it doens't seem to work.  If I run the playbook, it never checks with the condition rather it sets the last set_fact value of "shot"

---
- hosts: sandbox
  user: robo
  become: yes
  gather_facts: yes
  tasks:
  - debug: msg="{{ansible_memtotal_mb}}"

  - name: SHOT value for MEM less than 16G
    set_fact:
       shot: 3670016
       when: ansible_memtotal_mb|int <= 16384

  - name: SHOT value for MEM is between 16G and 32G
    set_fact:
       shot: 7340032
       when: ansible_memtotal_mb|int > 16384 and ansible_memtotal_mb|int <= 32768

  - debug: var=shot

================================================================================
SUDO password:

PLAY [sandbox] *****************************************************************

TASK [setup] *******************************************************************
ok: [uslv-sapp-lnx11]

TASK [debug] *******************************************************************
ok: [uslv-sapp-lnx11] => {
    "msg": 7872
}

TASK [SHOT value for MEM less than 16G] **************************************
ok: [uslv-sapp-lnx11]

TASK [SHOT value for MEM is between 16G and 32G] *****************************
ok: [uslv-sapp-lnx11]

TASK [debug] *******************************************************************
ok: [uslv-sapp-lnx11] => {
    "shot": 7340032
}

PLAY RECAP *********************************************************************
uslv-sapp-lnx11            : ok=5    changed=0    unreachable=0    failed=0

Apr 27, 2018 in Ansible by shubham
• 7,340 points

edited Apr 27, 2018 by shubham 5,623 views

1 answer to this question.

0 votes
Best answer

Your identation is wrong and when is not an argument for set_fact, it's an argument for a task:

- name: SHOT value for MEM less than 16G
  set_fact:
    shot: 3670016
  when: ansible_memtotal_mb|int <= 16384

- name: SHOT value for MEM is between 16G and 32G
  set_fact:
    shot: 7340032
  when: ansible_memtotal_mb|int > 16384 and ansible_memtotal_mb|int <= 32768
answered Apr 27, 2018 by ajs3033
• 7,300 points

selected Apr 27, 2018 by shubham

Related Questions In Ansible

0 votes
1 answer
0 votes
1 answer

stopping different hosts from reading the same value when usign ansible in loop

Try using this. Create a new directory ...READ MORE

answered Jul 27, 2018 in Ansible by DareDev
• 6,890 points
732 views
+1 vote
1 answer
0 votes
1 answer

Ansible playbook with jenkins not working

When you execute the playbook under Jenkins, ...READ MORE

answered Jan 17, 2019 in Ansible by Sameer
3,173 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,058 views
+2 votes
1 answer
0 votes
1 answer

Ansible: setting profile source in ansible.config

Try and add path in .bashrc or ...READ MORE

answered Jul 10, 2018 in Ansible by ajs3033
• 7,300 points
6,546 views
0 votes
1 answer

Ansible: List all installed Apps in RedHat

Try this to get a list of ...READ MORE

answered Jul 13, 2018 in Ansible by ajs3033
• 7,300 points

edited Jul 13, 2018 by ajs3033 2,563 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP