Search code examples
variablesansiblejinja2

Ansible parametrized variables in complex data structures on template


Is there any resolve problem parametrized variables in jinja without "if"?

I have var_file.yml:

fruit:
  yellow: banana
  green: apple

run ansible-playbook playbook.yml --extra-vars healthy=fruit

playbook.yml:

- name: include var
  include_vars:
    file: var_file.yml

- name: debug parametrized variables
  debug:
    var={{healthy.green}}
  

I want in my output fruit in parameter healthy.

is this possible? in debug vars i see:

ok: [localhost] => {
    "ansible_facts": {
       fruit{
         yellow: banana
         green: apple
         }
(...)

Solution

  • Q: "Output fruit in parameter healthy. Is this possible?"

    A: Yes. It's possible. Try this

    - name: debug parametrized variables
      debug:
        var: myfruits.green
      vars:
        myfruits: "{{ lookup('vars', healthy) }}"