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.
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 .
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.
Use this command to check the playbook for syntax errors: $ ansible-playbook <playbook. yml> --syntax-check.
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.
You do not need {{}}
in when conditions. What you are searching for is:
- fail: msg="unsupported version" when: version not in acceptable_versions
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With