Use lookup/dynamic variable names in jinja2 template

63 views
Skip to first unread message

Willem Bos

unread,
Jun 24, 2020, 6:33:03 AM6/24/20
to Ansible Project
Hi,

The playbook below should generate a file with the content:
a,b
ssh_host_key
ssh_rsa_host_key

However, the way I construct the variable names results in either syntax/templating errors or 'variable name does not exists'.

---
- hosts: localhost
  connection: local

  vars:
    CentOS:
      ciphers: "a,b"
      hostkeys:
        - "ssh_host_key"
        - "ssh_rsa_host_key"
  tasks:
  - copy:
      dest: "{{ playbook_dir }}/test.out"
      content: |

        # This works:
        {{ CentOS.ciphers }}

        # This results in 'No variable found with this name':
        {# Ciphers {{ lookup('vars', ansible_distribution + '.ciphers') }}

        # Templating errors:
        {% for hostkey in {{ lookup('vars', ansible_distribution + '.hostkeys') }} %}
        {{ hostkey }}
        {% endfor %}

        # Templating errors:
        {% for hostkey in {{ hostvars[inventory_hostname][ansible_distribution + '.hostkeys'] }} %}
        {{ hostkey }}
        {% endfor %}


What is the proper way to 'assemble' the variable names? Or is there a better way of doing this?

Regards,
Willem.

Martin Krizek

unread,
Jun 24, 2020, 8:39:52 AM6/24/20
to ansible...@googlegroups.com
You can use the varnames lookup to get variable names that match given
pattern: https://docs.ansible.com/ansible/latest/plugins/lookup/varnames.html
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/76b24a20-1c5b-46ea-a197-f1c390fcd0f7o%40googlegroups.com.

Willem Bos

unread,
Jun 25, 2020, 7:47:07 AM6/25/20
to Ansible Project
Hi Martin,

Thanks, but I'm not sure varnames is useful to me.

I know what the variable is called, but don't know how to construct it programmatically (as the Ansible manual calls it). Taking the example code from the documentation results in an empty list:

...
- debug:
    msg: "{{ lookup('varnames', ansible_distribution + '.hostkeys') }}"
...
TASK [debug] *******
ok: [localhost] => {
    "msg": []
}
...

Regards,
Willem.


On Wednesday, June 24, 2020 at 2:39:52 PM UTC+2, Martin Krizek wrote:
You can use the varnames lookup to get variable names that match given
pattern: https://docs.ansible.com/ansible/latest/plugins/lookup/varnames.html

> To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Martin Krizek

unread,
Jun 25, 2020, 8:19:03 AM6/25/20
to ansible...@googlegroups.com
Sorry, I misread your example and missed that the variable you want to
look up is a dictionary and that you want to access a key within that
dictionary. In that case you need to look up just the var and access
the key on the result of the lookup:

- hosts: localhost
gather_facts: yes
vars:
Fedora:
key: value
tasks:
- debug:
msg: "{{ lookup('vars', ansible_distribution)['key'] }}"
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/97e0166f-d015-4097-a870-1a207b14c7a5o%40googlegroups.com.

Willem Bos

unread,
Jun 25, 2020, 9:21:41 AM6/25/20
to Ansible Project
Hi Martin,

Thanks for your quick response, really appreciate it. I hope you don't mind an additional question: the vars section actually is one level deeper:

vars:
  sshd:

    CentOS:
      ciphers: "a,b"
      hostkeys:
        - "ssh_host_key"
        - "ssh_rsa_host_key"

How do I add this to:
{% for hostkey in lookup('vars', ansible_distribution)['hostkeys'] %}
{{ hostkey }}
{% endfor %}

I tried ['sshd']lookup('vars', ansible_distribution)['hostkeys'] and a few variants, but they all fail.

Sorry for being such a noob, but accessing vars/lists/dicts/etc. always gives me major headaches :-)

Regards,
Willem.





On Thursday, June 25, 2020 at 2:19:03 PM UTC+2, Martin Krizek wrote:
Sorry, I misread your example and missed that the variable you want to
look up is a dictionary and that you want to access a key within that
dictionary. In that case you need to look up just the var and access
the key on the result of the lookup:

- hosts: localhost
  gather_facts: yes
  vars:
    Fedora:
      key: value
  tasks:
    - debug:
        msg: "{{ lookup('vars', ansible_distribution)['key'] }}"

Martin Krizek

unread,
Jun 25, 2020, 9:53:56 AM6/25/20
to ansible...@googlegroups.com
- hosts: localhost
gather_facts: yes
vars:
top_level:
Fedora:
key: value
tasks:
- debug:
msg: "{{ top_level[ansible_distribution]['key'] }}"
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d1137be1-6bea-4320-83db-bf53145616bfo%40googlegroups.com.

Willem Bos

unread,
Jun 26, 2020, 4:39:58 AM6/26/20
to Ansible Project
Hi Martin,

Thank you for your effort. And your patience.

Regards,
Willem.
Reply all
Reply to author
Forward
0 new messages