How to add something to ansible_facts?

706 views
Skip to first unread message

thok...@gmail.com

unread,
Nov 30, 2016, 12:30:08 PM11/30/16
to Ansible Project
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?


Alexander H. Laughlin

unread,
Nov 30, 2016, 7:40:38 PM11/30/16
to Ansible Project
You can start by providing an example playbook.

thok...@gmail.com

unread,
Dec 1, 2016, 4:17:30 AM12/1/16
to Ansible Project
For example:

- name: Get date
  command curl 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.

Ali Jabir

unread,
Dec 1, 2016, 6:38:33 AM12/1/16
to ansible...@googlegroups.com
Hi there,

Try doing,

-
 name: Set fact
  set_fact
:
    my_date_as_fact
: my_date

Thanks
-AliJ

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/492f1ad9-64f5-4e3e-b63b-ebca21fad90f%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

thok...@gmail.com

unread,
Dec 1, 2016, 7:11:49 AM12/1/16
to Ansible Project


Am Donnerstag, 1. Dezember 2016 12:38:33 UTC+1 schrieb Ali Jabir:
Hi there,

Try doing,

-
 name: Set fact
  set_fact
:
    my_date_as_fact
: my_date

Thanks
-AliJ

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

thok...@gmail.com

unread,
Dec 1, 2016, 7:23:21 AM12/1/16
to Ansible Project
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?

Kai Stian Olstad

unread,
Dec 1, 2016, 11:02:55 AM12/1/16
to ansible...@googlegroups.com
On 01. des. 2016 13:23, thok...@gmail.com wrote:
> 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

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
> local_action:
> module: uri
> url: "{{ some_url }}"
> method: PUT
> body: "{{ ansible_facts | combine(fact_app_version) | to_json }}"
> status_code: 204
> 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.


--
Kai Stian Olstad

thok...@gmail.com

unread,
Dec 9, 2016, 9:31:37 AM12/9/16
to Ansible Project, ansible-pr...@olstad.com
@Kai Stian Olstad

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

thok...@gmail.com

unread,
Dec 9, 2016, 10:32:17 AM12/9/16
to Ansible Project, ansible-pr...@olstad.com
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" ?
Reply all
Reply to author
Forward
0 new messages