0

I want to collect remote facts from hosts that are not in my inventory.

"{{ record[1] }}" is the host I want the facts from.

I'm using:

- name: Gather facts
  ansible.builtin.gather_facts:
  delegate_to: "{{ record[1] }}"
  delegate_facts: true
  register: facts_polled

Unfortunately the facts registered are the one from my localhost and not the remote one. I tried with the setup module, with delegate_facts to true or false same issues :(

Any idea?

Thanks, Nicolas

Zeitounator
  • 38,476
  • 7
  • 53
  • 66
  • 2
    You could use `add_host` to add the host to the runtime inventory, and then target it with a separate play. – larsks Jan 18 '22 at 12:39
  • I'm looping across 1k hosts to extract their facts to dump some of them for an hardware inventory. add_host will add all of them when in my current inventory. The idea is: During my play, I loop a csv, set facts, enrich the initial csv with the output. – Nicolas Stojanovic Jan 18 '22 at 13:47
  • @NicolasStojanovic then add them to a group also, an target the group https://docs.ansible.com/ansible/latest/collections/ansible/builtin/add_host_module.html#parameter-groups. Adding hosts dynamically won't change your inventory, they will only be there for the time of the play. – β.εηοιτ.βε Jan 18 '22 at 14:04
  • 1
    Looping 1k hosts, consider [caching facts](https://docs.ansible.com/ansible/latest/user_guide/playbooks_vars_facts.html#caching-facts). See [Fastest way to gather facts to fact cache](https://stackoverflow.com/questions/32703874/fastest-way-to-gather-facts-to-fact-cache). Run ``ansible-doc -t cache -l`` to see what plugins are available. – Vladimir Botka Jan 18 '22 at 15:13
  • Looping 1k hosts: consider (in addition to cache) adding them in their specific inventory rather than looping add_host. You can use several inventory files at once so load those hosts only when needed. For an example: https://stackoverflow.com/questions/58651943/can-an-ansible-inventory-include-another/58657703#58657703 – Zeitounator Jan 18 '22 at 17:26

1 Answers1

1

Preliminary note: There might be other issues since you did not provide a full playbook example. More specifically, I strongly suspect you have used connection: local on your play/inventory which will make any host use the local connection to localhost. You should check this and fix if it is the case.


Quoting the documentation

Warning
Although you can delegate_to a host that does not exist in inventory (by adding IP address, DNS name or whatever requirement the connection plugin has), doing so does not add the host to your inventory and might cause issues. Hosts delegated to in this way do not inherit variables from the “all” group’, so variables like connection user and key are missing. If you must delegate_to a non-inventory host, use the add host module.

This is a pseudo-example of how to do this respecting the above rules:

- name: Your playbook
  hosts: my_group
  
  tasks:
    # A few tasks here where you get at some point
    # the `record` variable holding your host and to the point
    # where you need to gather facts on it

    - name: Add external host to in memory inventory
      add_host:
        name: "{{ record[1] }}"

    - name: Gather facts from added host
      setup:
      delegate_to: "{{ record[1] }}"
      delegate_facts: true
      run_once: true

    - name: Use gathered facts (show all for server)
      debug:
        msg: "{{ hostvars[record[1]] }}"

     # Rest of your play...


Zeitounator
  • 38,476
  • 7
  • 53
  • 66