# How To Get Hostname, OS and Version From Ansible Facts In A List Format?

**URL:** <https://forum.ansible.com/t/how-to-get-hostname-os-and-version-from-ansible-facts-in-a-list-format/35260>\
**Category:** Ansible Project\
**Created:** [May 31, 2021, 1:33pm UTC](https://forum.ansible.com/t/how-to-get-hostname-os-and-version-from-ansible-facts-in-a-list-format/35260 "2021-05-31T13:33:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcus\_Hayhurst](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@Marcus\_Hayhurst](https://forum.ansible.com/u/Marcus_Hayhurst)\
**Post date:** [May 31, 2021, 1:33pm UTC](https://forum.ansible.com/t/how-to-get-hostname-os-and-version-from-ansible-facts-in-a-list-format/35260/1 "2021-05-31T13:33:00Z")

</div>

Hello everyone,

I’m attempting to get a list of information of all my machines to send in an email:

**Hostname - OS (and) Version**

so essentially:

**{{ ansible\_hostname }} - {{ ansible\_distribution }} {{ ansible\_distribution\_version }}**

I tried something like this but it does not work:

- name: SET FACT FOR ANSIBLE FACTS  
set\_fact:  
machine\_os: “{{ ansible\_facts }}”

---

<div class="post-metadata">

**Author:** ![Pushparaj\_Gothandam](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/pushparaj_gothandam/32/5538_2.png) [@Pushparaj\_Gothandam](https://forum.ansible.com/u/Pushparaj_Gothandam)\
**Post date:** [May 31, 2021, 2:46pm UTC](https://forum.ansible.com/t/how-to-get-hostname-os-and-version-from-ansible-facts-in-a-list-format/35260/2 "2021-05-31T14:46:12Z")

</div>

Can you show me the error message?

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [May 31, 2021, 3:55pm UTC](https://forum.ansible.com/t/how-to-get-hostname-os-and-version-from-ansible-facts-in-a-list-format/35260/3 "2021-05-31T15:55:49Z")

</div>

> Hello everyone,
> 
> I'm attempting to get a list of information of all my machines to send  
> in an email:
> 
> \*Hostname - OS (and) Version \*
> 
> so essentially:
> 
> \*{{ ansible\_hostname }} - {{ ansible\_distribution }} {{  
> ansible\_distribution\_version }}\*
> 
> I tried something like this but it does not work:
> 
> - name: SET FACT FOR ANSIBLE FACTS  
> set\_fact:  
> machine\_os: "{{ ansible\_facts }}"  
> #  
> #  
> - name: DISTRIBUTION AND MAJOR VERSION  
> debug:  
> msg: "{% for os in machine\_os %}{{ os.ansible\_hostname }} - {{  
> os.ansible\_distribution }} {{ os.ansible\_distribution\_version }}{%  
> endfor %}"
> 
> Would anyone know how I could generate this list of information for all  
> my machines to send via email?

Please give this a try:

&nbsp;&nbsp;&nbsp;&nbsp;- name: Collect information on all machines  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;machine\_info: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for host in ansible\_play\_hosts %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ hostvars[host]['ansible\_hostname'] }} - {{ hostvars[host]['ansible\_distribution'] }} {{ hostvars[host]['ansible\_distribution\_version'] }}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;run\_once: yes

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke
