2

I'm trying to collect servers inventory with ansible using the following command:

ansible all -m setup -a --tree facts/

but this is generating a lot os single files for each host under facts folder.

I would like all servers output in a unique file, I noticed the content is similar to json. I want to use it to read in python later and use the hostname as keys for the python dictionary and just read the the facts regarding each host collected.

Is there something like below??

ansible localhost -m setup -a --tree --single-file facts/
  • You can get the help from below stack-overflow link: https://stackoverflow.com/questions/36484980/use-ansible-facts-in-an-ansible-ad-hoc-command – Abhishek Verma Aug 06 '20 at 14:46

3 Answers3

2

This playbook will copy all the gather fact content in /tmp/ansible_facts_details.json file on the host machine. You can run it from location where inventory file is located in the control machine (Machine where Ansible is installed).

  1. Command to run playbook is ansible-playbook playbooks/gatherfacts_playbook.yaml
  2. Command to check playbook syntax is ansible-playbook playbooks/gatherfacts_playbook.yaml --syntax-check
  3. Command to do runtime debug ansible-playbook -vvv playbooks/gatherfacts_playbook.yaml
- name: Play to get the gathre facts content
  hosts: DEV1
  tasks:
  - name: print ansible_facts
    debug:
     var: ansible_facts["kernal"]
  - name: Copy ansible facts to a file.
    copy:
     content: "{{ ansible_facts }}"
     dest: /tmp/ansible_facts_details.json
Abhishek Verma
  • 396
  • 4
  • 14
1

According documentation, add to playbok next task

  tasks:
  - name: Generate report
    local_action:
      module: copy
      dest: ./hosts-OS-version.txt
      content: |
          {% for host in groups['all'] %}
          {% if hostvars[host]['ansible_facts']['distribution'] is defined %}
          {{ '%-50s' | format(host) }} {{ hostvars[host]['ansible_facts']['distribution'] }} {{ hostvars[host]['ansible_facts']['distribution_version'] }}
          {% else %}
          {{ '%-50s' | format(host) }} undefined
          {% endif %}
          {% endfor %}
qreodium
  • 33
  • 6
0

Try using --forks 1 option in first ansible command.

initanmol
  • 340
  • 1
  • 4