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'] }}" 
>
> On Thu, Jun 25, 2020 at 1:47 PM Willem Bos <[email protected] 
> <javascript:>> wrote: 
> > 
> > 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 
> >> 
> >> On Wed, Jun 24, 2020 at 12:33 PM Willem Bos <[email protected]> 
> wrote: 
> >> > 
> >> > 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. 
> >> > 
> >> > -- 
> >> > 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 [email protected]. 
> >> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/ansible-project/76b24a20-1c5b-46ea-a197-f1c390fcd0f7o%40googlegroups.com.
>  
>
> >> 
> > -- 
> > 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 [email protected] <javascript:>. 
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/ansible-project/97e0166f-d015-4097-a870-1a207b14c7a5o%40googlegroups.com.
>  
>
>
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/d1137be1-6bea-4320-83db-bf53145616bfo%40googlegroups.com.

Reply via email to