# Using facts with Ansible Tower does not appear to work

**URL:** <https://forum.ansible.com/t/using-facts-with-ansible-tower-does-not-appear-to-work/23042>\
**Category:** Ansible Project\
**Created:** [November 16, 2016, 4:25pm UTC](https://forum.ansible.com/t/using-facts-with-ansible-tower-does-not-appear-to-work/23042 "2016-11-16T16:25:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jon\_Kent](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@Jon\_Kent](https://forum.ansible.com/u/Jon_Kent)\
**Post date:** [November 16, 2016, 4:25pm UTC](https://forum.ansible.com/t/using-facts-with-ansible-tower-does-not-appear-to-work/23042/1 "2016-11-16T16:25:13Z")

</div>

Hi,

I’ve been trying to get Ansible Tower to run a playbook/role which should update the /etc/hosts file across all hosts in a group.

I have the following playbook:

- name: Update hosts file if required  
hosts: all  
gather\_facts: yes

roles:

- { role: hostfile }

and the following role:

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [November 16, 2016, 5:43pm UTC](https://forum.ansible.com/t/using-facts-with-ansible-tower-does-not-appear-to-work/23042/2 "2016-11-16T17:43:47Z")

</div>

> I've been trying to get Ansible Tower to run a playbook/role which should  
> update the /etc/hosts file across all hosts in a group.
> 
> I have the following playbook:
> 
> - name: Update hosts file if required  
> &nbsp;&nbsp;hosts: all  
> &nbsp;&nbsp;gather\_facts: yes
> 
> &nbsp;&nbsp;roles:  
> &nbsp;&nbsp;&nbsp;&nbsp;- { role: hostfile }
> 
> and the following role:
> 
> ---
> 
> - debug: var=ansible\_default\_ipv4.address
> 
> - name: node to ip mapping  
> &nbsp;&nbsp;debug: var=hostvars[item].ansible\_default\_ipv4.address  
> &nbsp;&nbsp;with\_items: groups['all']

It you check your output you will see that your item i literally the string groups['all']. But you need to write this as a variable  
&nbsp;&nbsp;&nbsp;with\_items: "{{ groups['all'] }}"

> - name: Build Host file  
> &nbsp;&nbsp;lineinfile: dest=/etc/hosts  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;regex='.\*{{ item }}$ }}'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;line="{{ hostvars[item].ansible\_default\_ipv4.address }}  
> {{item}}"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state=present  
> &nbsp;&nbsp;when: hostvars[item].ansible\_default\_ipv4.address is defined  
> &nbsp;&nbsp;with\_items: groups['all']  
> &nbsp;&nbsp;become: true
> 
> However, when Tower run this its skips the 2 hosts in the group. From  
> looking at the debug output it would appear that Tower is not caching the  
> facts under hostvars as you can see from the output below:
> 
> TASK [setup]  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ok:  
> [172.31.9.43] ok: [52.50.60.2] TASK [hostfile : debug]  
> \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ok: [172.31.9.43]  
> =\> { "ansible\_default\_ipv4.address": "172.31.9.43" } ok: [52.50.60.2] =\> {  
> "ansible\_default\_ipv4.address": "172.31.33.206" } TASK [hostfile : node to  
> ip mapping] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* ok: [172.31.9.43]  
> =\> (item=groups['all']) =\> { "hostvars[item].ansible\_default\_ipv4.address":

Here you see your item is "groups['all']" and not a hostname.

> I have a feeling that I'm missing something very obvious here. Any ideas  
> where I should look?

Bare variables in with\_items was deprecated before 2.2, in 2.2 it will take a bare variable as a string.

---

<div class="post-metadata">

**Author:** ![Jon\_Kent](https://avatars.discourse-cdn.com/v4/letter/j/3d9bf3/32.png) [@Jon\_Kent](https://forum.ansible.com/u/Jon_Kent)\
**Post date:** [November 17, 2016, 9:52am UTC](https://forum.ansible.com/t/using-facts-with-ansible-tower-does-not-appear-to-work/23042/3 "2016-11-17T09:52:02Z")

</div>

Hi,

Many thanks for that, hoped it was straight forward.

All working now.

Many thanks,  
Jon
