<p>use the condition</p>

<pre><code>- when: variable is defined
  run_module_when_defined:
</code></pre>

<p>or </p>

<pre><code>- name:
  block:
    - task1
    - task2
  when: variable is defined
</code></pre>

CC BY-SA 4.0
2019-03-01T16:04:15.567Z
2019-03-01T16:04:15.567Z
Zohaib Amanzai
<p>This question may look strange as something like this is not implemented in real life. I have tried to make it very general. </p>

<p>Here is my ansible-playbook:</p>

<p><strong>tasks/main.yml</strong></p>

<pre><code>- name: "Setting place name of {{ country }}" 
  set_fact:
     place: "get_place_name"

- name: "Setting places name of {{ country }}" 
  set_fact:
     places: "get_places_name"

- name: Details of "{{ country }}"
  debug:
    msg: "{{ item }}" 
  when:
    -  place in "{{ item.Other_Places }}" or places in "{{ item.Other_Places }}" 
  with_items: "{{ Places }}"
</code></pre>

<p><strong>vars/main.yml</strong></p>

<pre><code>---
Places:
- Country: "United States of America"
  Capital: "{{ capital }}"
  Other_Places: ['place1', 'place2']

- Country: "China"
  Capital: "{{ capital }}"
  Other_Places: ['places1', 'places2', 'places3', 'places4']
</code></pre>

<p>Here if "place" variable is defined as "place1" then it will run and output will be something as below:</p>

<pre><code>"msg": {
        "Country": "United States of America", 
        "Capital": "place1", 
        "Other_Places": "['place1', 'place2']"
    }
</code></pre>

<p>The problem is I am confused on how to handle something that is not listed in "Other_Places"? Suppose I input "place10" as variable name "Capital" then it is not defined then it should simply output "Sorry, no matching place found."</p>

<p>If we do something like below :</p>

<pre><code>- name: Exceptional case
  debug:
    msg: "{{ capital }} not in {{ item.Capital }} " 
  when:
    -  place not in "{{ item.Other_Places }}" and places not in "{{ item.Other_Places }}" 
  with_items: "{{ Places }}"
</code></pre>

<p>This will output all at once because "place" is not defined in "capital".</p>

CC BY-SA 3.0
2018-04-25T04:00:45.317Z
In its current form, I found your question unclear. Can you make your code [short and self-contained](http://sscce.org/) - so that it should run simply by copying and pasting it into a playbook. This way the community will be able to help you better.
Vish
CC BY-SA 3.0
2018-04-25T04:01:38.393Z
BTW you have an extra `}` in the line `Capital: "{{ capital}}}"`
Vish
CC BY-SA 3.0
2018-04-25T08:13:20.817Z
well this is just a common example I gave. I want to show msg like "Place not found" when there is no place(s) defined in "Other_Places" variable.
Prakash
<ansible>
How to handle item not in list in ansible variable?
Prakash
# How to handle item not in list in ansible variable?

(Asked by: username_0 on 2018-04-24)

This question may look strange as something like this is not implemented in real life. I have tried to make it very general. 
Here is my ansible-playbook:
tasks/main.yml
- name: "Setting place name of {{ country }}" 
  set_fact:
     place: "get_place_name"

- name: "Setting places name of {{ country }}" 
  set_fact:
     places: "get_places_name"

- name: Details of "{{ country }}"
  debug:
    msg: "{{ item }}" 
  when:
    -  place in "{{ item.Other_Places }}" or places in "{{ item.Other_Places }}" 
  with_items: "{{ Places }}"

vars/main.yml
---
Places:
- Country: "United States of America"
  Capital: "{{ capital }}"
  Other_Places: ['place1', 'place2']

- Country: "China"
  Capital: "{{ capital }}"
  Other_Places: ['places1', 'places2', 'places3', 'places4']

Here if "place" variable is defined as "place1" then it will run and output will be something as below:
"msg": {
        "Country": "United States of America", 
        "Capital": "place1", 
        "Other_Places": "['place1', 'place2']"
    }

The problem is I am confused on how to handle something that is not listed in "Other_Places"? Suppose I input "place10" as variable name "Capital" then it is not defined then it should simply output "Sorry, no matching place found."
If we do something like below :
- name: Exceptional case
  debug:
    msg: "{{ capital }} not in {{ item.Capital }} " 
  when:
    -  place not in "{{ item.Other_Places }}" and places not in "{{ item.Other_Places }}" 
  with_items: "{{ Places }}"

This will output all at once because "place" is not defined in "capital".

**Tags:** ansible

> **Comments:**
> **username_1**: In its current form, I found your question unclear. Can you make your code [short and self-contained](http://sscce.org/) - so that it should run simply by copying and pasting it into a playbook. This way the community will be able to help you better.  
> **username_1**: BTW you have an extra `}` in the line `Capital: "{{ capital}}}"`  
> **username_0**: well this is just a common example I gave. I want to show msg like "Place not found" when there is no place(s) defined in "Other_Places" variable.  

---

## 1 answers

### Answer by username_2 on 2019-03-01. Score: 3

use the condition
- when: variable is defined
  run_module_when_defined:

or 
- name:
  block:
    - task1
    - task2
  when: variable is defined


---

