Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a list contains an item in Ansible

Tags:

yaml

ansible

I'm trying to check if the version supplied is a valid supported version. I've set the list of acceptable versions in a variable, and I want to fail out of the task if the supplied version is not in the list. However, I'm unsure of how to do that.

#/role/vars/main.yml ---   acceptable_versions: [2, 3, 4] 

and

#/role/tasks/main.yml ---   - fail:        msg: "unsupported version"       with_items: "{{acceptable_versions}}"       when: "{{item}} != {{version}}"    - name: continue with rest of tasks... 

Above is sort of what I want to do, but I haven't been able to figure out if there's a one line way to construct a "list contains" call for the fail module.

like image 434
Shark Avatar asked Jan 22 '15 02:01

Shark


People also ask

What is {{ item }} Ansible?

item is not a command, but a variable automatically created and populated by Ansible in tasks which use loops. In the following example: - debug: msg: "{{ item }}" with_items: - first - second. the task will be run twice: first time with the variable item set to first , the second time with second .

How is it possible to check if a variable is defined in Ansible?

As per latest Ansible Version 2.5, to check if a variable is defined and depending upon this if you want to run any task, use undefined keyword. Save this answer. Show activity on this post. Strictly stated you must check all of the following: defined, not empty AND not None.

How do you test Ansible syntax?

Use this command to check the playbook for syntax errors: $ ansible-playbook <playbook. yml> --syntax-check.

What is Ansible list?

In this article, we are going to learn about Ansible List. and How to create Ansible List and append items to it. The LIST is a type in data structure and is widely used in all programming languages. It is to store a group of heterogeneous elements. We are NOT going to talk about data structures don't worry.


1 Answers

You do not need {{}} in when conditions. What you are searching for is:

- fail: msg="unsupported version"   when: version not in acceptable_versions 
like image 128
ProfHase85 Avatar answered Oct 12 '22 23:10

ProfHase85