I have following code where in I want to implement conditional logic like if-else construct:
- name: Download katello agent package
get_url:
url: "{{ abc_url }}/pub/yyy.rpm"
dest: "{{ tmp_dir }}"
when: 'rt' in group_names url = abc_url,
when: 'rt' not in group_names url = def_url, based on group_names, my URL changes.
It depends on the circumstances. But in the Use Case shown, no, one won't to do so in Ansible. It is not recommended to implement a conditional logic in such a case. Instead one should use the inventory and
group_vars.For an inventory file
example.comand
group_varsfileswith content of
cat group_vars/rt/katellocat group_vars/def/katelloa minimal example playbook
called via
will result into an output of
As one can see from the output, no IF THEN ELSE, no Conditionals or
when, noset_factor tasks, no code, no programming at all. Just and only How to build your inventory.Further Reading and Documentation
Things to know about programming using Ansible
Ansible Best Practices: The Essentials
What is the XY problem?