ansible conditionals not stopping on matching first condition

ansible conditional variable
ansible if else condition
ansible when variable is not empty
ansible set variable if else
ansible when condition multiple lines
ansible "{% if"
ansible failed_when
ansible_facts

I am a starter in ansible. I have to automate a installation where two things needs to be checked

1) Is it a redhat system (exit if not) 2) If 1) is true then check whether RAM is more than 8GB(if no then print message and exit)

This is my playbook

 - name: getting RAM details
   shell: free -m | grep Mem | awk  '{ print $2 }'
   register: ram
   when: ansible_distribution == "RedHat"

 - name: Checking OS
   debug: msg="current OS is {{ ansible_distribution }} which is not 
   supported. Should be redhat"
   when: (ansible_distribution!="RedHat")

 - name: Checking RAM
   debug: msg="current RAM is {{ ram.stdout }} GB. Should be 8 GB"
   when: (ram.stdout | int < 7698 )

The issue is the play is not exiting when the first scenario is matched. Its still going for the second scenario . I am a newcomer to the ansible world.

Use assert module (or fail module) instead of debug:

- name: Checking OS
  assert:
    msg: "current OS is {{ ansible_distribution }} which is not supported. Should be redhat"
    that: "ansible_distribution == 'RedHat'"

- name: Checking RAM
  assert:
    msg: "current RAM is {{ ram.stdout }} GB. Should be 8 GB"
    that: "ram.stdout | int > 7698"

Conditionals, This could be something as simple as not installing a certain package if the This is especially useful in combination with the conditional import of vars files (​see in Ansible when using this approach on systems that don't match the criteria. On Debian, Ansible first looks for 'vars/Debian.yml' instead of 'vars/​RedHat.yml',  Find and use first match during a loop with when conditions in Ansible. Ask Question ansible conditionals not stopping on matching first condition. 1.

there are issues with the tasks structure, please see below how it should look like:

- name: getting RAM details
  shell: free -m | grep Mem | awk  '{ print $2 }'
  register: ram
  when: ansible_distribution == "RedHat"

- name: Checking OS
  debug: msg="current OS is {{ ansible_distribution }} which is not supported. Should be redhat"
  when: (ansible_distribution!="RedHat")

- name: Checking RAM
  debug: msg="current RAM is {{ ram.stdout }} GB. Should be 8 GB"
  when: (ram.stdout | int < 7698 )

Regarding exiting, the idea of ansible and the when condition is that it will process all tasks and will skip the ones that the when condition is not true. there is no "exit" way in the above code.

i understand what you were trying to achieve, but it doesnt work like that. Conditionally importing tasks would be probably of use to you with a when condition, example:

- include_tasks: "{{ hostvar }}.yml"
  when: hostvar is defined

hope it helps

Ansible 1.3 Conditional Execution -- Very complete example with , This could be something as simple as not installing a certain package if the operating This is especially useful in combination with the conditional import of vars files in Ansible when using this approach on systems that don't match the criteria. On Debian, it would instead first look towards 'vars/Debian.yml' instead of  Problem with when conditionals when trying to match a variable that makes part of a dictionary. Worked perfectly until Ansible version update. Since in new version there are the following warnings: [WARNING]: when statements should not include jinja2 templating delimiters such as {{ }} or {% %} , I tried to fix the syntax, but the condition is

You could use the meta: end_play to stop the play execution when either of the two condition is met.

- name: getting RAM details
  shell: free -m | grep Mem | awk  '{ print $2 }'
  register: ram
  when: ansible_distribution == "RedHat"

- name: abort if not Redhat or RAM less than 8G
  meta: end_play
  when: (ansible_distribution!="RedHat") or (ram.stdout | int < 7698 )

problem with conditionals and matching a variable in dictionary , Ansible 1.3 Conditional Execution -- Very complete example with comments -- I is that the type of the variable is very important, and it's not obvious what that is. as true and true is not equal to 'true' so only works for negative match name: $​is_live and (isFirstRun == 'true') # WRONG - Correct when true, but only first  Create a file using Ansible when the file does not exist. If your intention is to create a new file if it is not present on the remote servers, then you should use the file module. You can use the ‘state: touch’ to make sure the file is created.

Gracefully stop playbook when a condition is me. : ansible, ansible --version ansible 2.3.1.0 config file = /etc/ansible/ansible.cfg Problem with when conditionals when trying to match a variable that I tried to fix the syntax, but the condition is no longer matched. Normally it should not have worked even earlier because it must match a value in the following list: This could be something as simple as not installing a certain package if the operating system is a particular version, or it could be something like performing some cleanup steps if a filesystem is getting full. This is easy to do in Ansible with the when clause, which contains a raw Jinja2 expression without double curly braces (see Variables). It’s actually pretty simple:

Best practices to build great Ansible playbooks, If a required variable has not been set, you can skip or fail using Jinja2's defined test. This is especially useful in combination with the conditional import of vars in Ansible when using this approach on systems that don't match the criteria. On Debian, Ansible first looks for 'vars/Debian.yml' instead of 'vars/RedHat.yml',  Ansible - using with_items and when conditional to. you can loop over the registered results of the first task then: Ansible: when condition with_items. 0.

Match a condition, I am trying to make a playbook to gracefully stop completely when a condition is met, even on fail: msg="Stopping Playbook -- BASH FILE NOT FOUND UNDER /VAR/TMP" when: Ansible 101 with Jeff Geerling - Episode 4 - First Real-World Playbook! {"msg": "The conditional check ''False' in service_info.exists' failed. Working with Ansible variables in conditionals Posted on July 29, 2017 April 27, 2018 by Ansible admin Variables are necessary when you need to store the output of a task and need to access it in some other tasks.