I'm trying to display some variables about the hosts using ansible gather facts. Here's the yml file

- name: setup users
  gather_facts: true
  hosts: hosts
    name: display stats after login
      src: /stats.j2
      dest: /etc/motd
    become: true

Here's the jinja2 file

Arch: {{ hostvars[inventory_hostname]['ansible_architecture'] }}

Accoring to anisble -m setup the variable exits but I'm always getting an error that the variable is undefined.

1 Answer 1


You would use {{hostvars['inventory_hostname']['...']}}. But if you do not want to access informations from other hosts you could change your template (stats.j2) to

Arch: {{ansible_architecture}}
  • I tried both versions and still get the same error that the variable is undefined.
    – tactical77
    Commented Jan 25, 2021 at 17:46
  • It actually works now don't really know what the problem was but thanks.
    – tactical77
    Commented Jan 30, 2021 at 14:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .