2

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':
        {{ 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?

Willem
  • 157
  • 4
  • 13

1 Answers1

3

My question was answered by Martin Krizek, in the Ansible newsgroup. The correct syntax is:

---
- hosts: localhost
  connection: local

  vars:
    CentOS:
      hostkeys:
        - "ssh_host_key"
        - "ssh_rsa_host_key"
  tasks:
  - copy:
      dest: "{{ playbook_dir }}/test.out"
      content: |
        {% for hostkey in lookup('vars', ansible_distribution)['hostkeys'] %}
        {{ hostkey }}
        {% endfor %}
Willem
  • 157
  • 4
  • 13