# How to add something to ansible\_facts?

**URL:** <https://forum.ansible.com/t/how-to-add-something-to-ansible-facts/23140>\
**Category:** Ansible Project\
**Created:** [November 30, 2016, 5:30pm UTC](https://forum.ansible.com/t/how-to-add-something-to-ansible-facts/23140 "2016-11-30T17:30:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![thokari81](https://avatars.discourse-cdn.com/v4/letter/t/e9c0ed/32.png) [@thokari81](https://forum.ansible.com/u/thokari81)\
**Post date:** [November 30, 2016, 5:30pm UTC](https://forum.ansible.com/t/how-to-add-something-to-ansible-facts/23140/1 "2016-11-30T17:30:08Z")

</div>

Hi,  
maybe I am being stupid here, but why doesn’t it work, that when I call the

set\_fact

module, and then run the

setup

module, my newly set fact is not returned?  
What else do I have to do?

---

<div class="post-metadata">

**Author:** ![Alexander\_H\_Laughlin](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@Alexander\_H\_Laughlin](https://forum.ansible.com/u/Alexander_H_Laughlin)\
**Post date:** [December 1, 2016, 12:40am UTC](https://forum.ansible.com/t/how-to-add-something-to-ansible-facts/23140/2 "2016-12-01T00:40:38Z")

</div>

You can start by providing an example playbook.

---

<div class="post-metadata">

**Author:** ![thokari81](https://avatars.discourse-cdn.com/v4/letter/t/e9c0ed/32.png) [@thokari81](https://forum.ansible.com/u/thokari81)\
**Post date:** [December 1, 2016, 9:17am UTC](https://forum.ansible.com/t/how-to-add-something-to-ansible-facts/23140/3 "2016-12-01T09:17:30Z")

</div>

For example:

`

- name: Get date  
command curl [http://some-url](http://some-url)  
register: my\_date

- name: Set fact  
set\_fact:  
my\_date\_as\_fact: my\_date

- name: Gather facts  
setup:  
register: {{ ansible\_facts }}

- name: Print facts  
debug: msg={{ ansible\_facts }}

`

“my\_date\_as\_fact” is not included in the output.

---

<div class="post-metadata">

**Author:** ![Ali\_Jabir](https://avatars.discourse-cdn.com/v4/letter/a/dfb087/32.png) [@Ali\_Jabir](https://forum.ansible.com/u/Ali_Jabir)\
**Post date:** [December 1, 2016, 11:37am UTC](https://forum.ansible.com/t/how-to-add-something-to-ansible-facts/23140/4 "2016-12-01T11:37:42Z")

</div>

Hi there,

Try doing,

-name: Set fact  
set\_fact:  
my\_date\_as\_fact: my\_date

---

<div class="post-metadata">

**Author:** ![thokari81](https://avatars.discourse-cdn.com/v4/letter/t/e9c0ed/32.png) [@thokari81](https://forum.ansible.com/u/thokari81)\
**Post date:** [December 1, 2016, 12:11pm UTC](https://forum.ansible.com/t/how-to-add-something-to-ansible-facts/23140/5 "2016-12-01T12:11:49Z")

</div>

That is what I have, it was a typo in my post.

---

<div class="post-metadata">

**Author:** ![thokari81](https://avatars.discourse-cdn.com/v4/letter/t/e9c0ed/32.png) [@thokari81](https://forum.ansible.com/u/thokari81)\
**Post date:** [December 1, 2016, 12:23pm UTC](https://forum.ansible.com/t/how-to-add-something-to-ansible-facts/23140/6 "2016-12-01T12:23:21Z")

</div>

This is what I have now, and it is still not working.

`

- name: Get app version  
command: curl -H application/json localhost:8088/rs  
register: get\_version  
ignore\_errors: true

- name: Set app version as fact  
set\_fact:  
fact\_app\_version:  
app\_version: “{{ ((get\_version | default({‘stdout’:‘N/A’})).stdout | from\_json).version }}”  
ignore\_errors: true

- name: Gather facts  
setup:  
register: ansible\_facts

- name: Send facts somewhere  
local\_action:  
module: uri  
url: “{{ some\_url }}”  
method: PUT  
body: “{{ ansible\_facts | combine(fact\_app\_version) | to\_json }}”  
status\_code: 204  
body\_format: json

`

The error is:

`  
FAILED! =\> {“failed”: true, “msg”: “the field ‘args’ has an invalid value (), and could not be converted to an dict. Error was: No JSON object could be decoded\n\nThe error appears to have been in ‘update-facts.yml’: line 18, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Set app version as fact\n ^ here\n”}

`

I am fairly certain what I am trying to do should be rather simply, however the more I dig into it, the uglier it gets.  
I want to add an optional fact to the dictionary of facts, and then send all facts somewhere.

What is the suggested approach do accomplish this?

---

<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:** [December 1, 2016, 4:02pm UTC](https://forum.ansible.com/t/how-to-add-something-to-ansible-facts/23140/7 "2016-12-01T16:02:41Z")

</div>

> This is what I have now, and it is still not working.  
> - name: Get app version  
> &nbsp;&nbsp;command: curl -H application/json localhost:8088/rs  
> &nbsp;&nbsp;register: get\_version  
> &nbsp;&nbsp;ignore\_errors: true
> 
> - name: Set app version as fact  
> &nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;fact\_app\_version:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;app\_version: "{{ ((get\_version |  
> default({'stdout':'N/A'})).stdout | from\_json).version }}"  
> &nbsp;&nbsp;ignore\_errors: true
> 
> - name: Gather facts  
> &nbsp;&nbsp;setup:  
> &nbsp;&nbsp;register: ansible\_facts

Remove the register:, you don't need it.  
If gather\_facts is set to true you can remove the entire "Gather facts" task.

> - name: Send facts somewhere  
> &nbsp;&nbsp;local\_action:  
> &nbsp;&nbsp;&nbsp;&nbsp;module: uri  
> &nbsp;&nbsp;&nbsp;&nbsp;url: "{{ some\_url }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;method: PUT  
> &nbsp;&nbsp;&nbsp;&nbsp;body: "{{ ansible\_facts | combine(fact\_app\_version) | to\_json }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;status\_code: 204  
> &nbsp;&nbsp;&nbsp;&nbsp;body\_format: json

Change body to  
body: "{{ hostvars[inventory\_hostname] }}"

> I am fairly certain what I am trying to do should be rather simply, however  
> the more I dig into it, the uglier it gets.  
> I want to add an optional fact to the dictionary of facts, and then send  
> all facts somewhere.  
> What is the suggested approach do accomplish this?

hostvars should contain all you need as it contains all your facts, variables and groups.

---

<div class="post-metadata">

**Author:** ![thokari81](https://avatars.discourse-cdn.com/v4/letter/t/e9c0ed/32.png) [@thokari81](https://forum.ansible.com/u/thokari81)\
**Post date:** [December 9, 2016, 2:31pm UTC](https://forum.ansible.com/t/how-to-add-something-to-ansible-facts/23140/8 "2016-12-09T14:31:37Z")

</div>

@Kai Stian Olstad

Thank you, the magic that is hostvars[inventory\_hostname] was exactly what I was looking for!

---

<div class="post-metadata">

**Author:** ![thokari81](https://avatars.discourse-cdn.com/v4/letter/t/e9c0ed/32.png) [@thokari81](https://forum.ansible.com/u/thokari81)\
**Post date:** [December 9, 2016, 3:32pm UTC](https://forum.ansible.com/t/how-to-add-something-to-ansible-facts/23140/9 "2016-12-09T15:32:17Z")

</div>

However I do have another question:

Is there a way to easily filter this list to at least exclude the temporary variables that got introduced during the playbook run, for example from “register” ?
