30

Is there exists some way to print on console gathered facts ?
I mean gatering facts using setup module. I would like to print gathered facts. Is it possible ? If it is possible can someone show example?

newbie
  • 597
  • 2
  • 6
  • 11
  • 1
    this command show all facts about a host: **ansible -i path_to_inventory -m setup hostname** – S.Bao Apr 16 '20 at 12:35

2 Answers2

42

Use setup module as ad-hoc command:

ansible myhost -m setup
Michael Altfield
  • 2,083
  • 23
  • 39
Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
  • 1
    ok, however. I have some playbook. And in this playbook I executed `setup` module. Can I check after this execution what facts are gathered? – newbie Apr 05 '18 at 18:21
  • run playbook with `-vv` switch? – Konstantin Suvorov Apr 05 '18 at 18:25
  • I always forget the possibility of using an ad-hoc command. – dgw Apr 05 '18 at 21:31
  • There are few issues with this approach: to make any use of this you need to run something like `ansible -v myhost -m setup | grep ansible_hostname` because facts are printed only when verbosity >=1 and grepping because there are too many. Ideally ansible should support chaining modules and doing something like: `ansible -m setup -m debug -a var=ansible_hostname` – sorin Feb 26 '19 at 08:25
  • 5
    `ansible -i somehost, all -m setup` if you are running this _truly_ ad-hoc and don't even have any inventory. (The trailing `,` is correct) – conny Aug 23 '19 at 12:34
  • I played with mobaxterm-based Ansible controller and I would like to expand @conny 's comment: `ansible -i somehost, all -m setup -u `. This allowed to connect using certain username without inventory file. – Rapekas Mar 02 '21 at 13:35
14

You can simply dump the hostvars:

dump.yml

---
 - name: Dump
   hosts: "{{ target|default('localhost') }}"
   tasks:
   - name: Facts
     setup:
   - name: Dump
     delegate_to: localhost
     run_once: true
     copy:
       content: "{{ hostvars[inventory_hostname] | to_nice_json }}"
       dest: /tmp/setup-dump.json

Call this playbook with ansible-playbook dump.yml -e target=hostname or simply without hostname.

dgw
  • 13,418
  • 11
  • 56
  • 54
  • Thanks, however it seems to don't work. `TASK [Dump] ******************************************************************************************************************************************************************************** changed: [localhost -> localhost] `I started with target=localhost – newbie Apr 05 '18 at 17:33
  • 2
    It works. Output is saved in /tmp/setup-dump.json – Alex Baranowski Aug 30 '18 at 11:16