Skip to Content

how to check if an item is in the list in Ansible

In Ansible, you can check if an element is present in a list using the in keyword in a playbook or in a Jinja2 template.

Here’s an example of how you can use the in keyword to check if an element is in a list in a playbook:

- name: Check if an element is in list
  hosts: localhost
  gather_facts: no
  vars:
    my_list:
      - item1
      - item2
      - item3
  tasks:
    - name: Debug if element is in list
      debug:
        var: "'item2' in my_list"

In this example, the my_list variable is a list containing three items. The debug task uses the in keyword to check if ‘item2’ is present in my_list. The result will be displayed in the debug output, and it will be True if the element is found in the list.

You can also use the in keyword in Jinja2 templates to perform the same check. Here’s an example:

Get Your Free Linux training!

Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!
{% set my_list = ['item1', 'item2', 'item3'] %}
{% if 'item2' in my_list %}
  Element found in list.
{% else %}
  Element not found in list.
{% endif %}

In this example, the my_list variable is a list containing three items. The Jinja2 template uses the in keyword inside an if statement to check if ‘item2’ is present in my_list and displays the appropriate message based on the result.

The when keyword allows you to conditionally execute tasks based on a certain condition, such as checking if an item is in a list.

Here’s an example of how you can use the when keyword to check if an item is in a list in a playbook:

- name: Check if item is in list
  hosts: localhost
  gather_facts: no
  vars:
    my_list:
      - item1
      - item2
      - item3
  tasks:
    - name: Task to execute if item is in list
      debug:
        var: my_list
      when: "'item2' in my_list"

In this example, the my_list variable is a list containing three items. The debug task is only executed when the condition ‘item2’ in my_list is True, which means ‘item2’ is present in the list.

You can also use the not in keyword to check if an item is not present in a list. Here’s an example:

- name: Check if item is not in list
  hosts: localhost
  gather_facts: no
  vars:
    my_list:
      - item1
      - item2
      - item3
  tasks:
    - name: Task to execute if item is not in list
      debug:
        var: my_list
      when: "'item4' not in my_list"

In this example, the debug task is only executed when the condition ‘item4’ not in my_list is True, which means ‘item4’ is not present in the list.

 

Here’s an example of using a complex condition to check if an item is present in an Ansible list:

- name: Example playbook with complex condition on list
  hosts: localhost
  gather_facts: no
  vars:
    my_list:
      - item1
      - item2
      - item3
    my_dict:
      key1: value1
      key2: value2
  tasks:
    - name: Task to execute if item is present in list and dictionary value matches
      debug:
        var: my_list
      when: "'item2' in my_list and my_dict.key2 == 'value2'"

    - name: Task to execute if item is present in list or dictionary key matches
      debug:
        var: my_dict
      when: "'item4' in my_list or my_dict.key1 == 'value1'"

In this example, the first task will be executed if ‘item2’ is present in the my_list variable and the value of my_dict.key2 is ‘value2’. The second task will be executed if ‘item4’ is present in the my_list variable or the value of my_dict.key1 is ‘value1’.

Note that you can use any valid Jinja2 expression in the when condition to check for complex conditions involving lists, dictionaries, and other variables in Ansible.