Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force fact-gathering on all hosts

I'm sitting in front of a fairly complex Ansible project that we're using to set up our local development environments (multiple VMs) and there's one role that uses the facts gathered by Ansible to set up the /etc/hosts file on every VM. Unfortunately, when you want to run the playbook for one host only (using the -limit parameter) the facts from the other hosts are (obviously) missing.

Is there a way to force Ansible to gather facts on all hosts, even if you limit the playbook to one specific host?

We tried to add a play to the playbook to gather facts from all hosts, but of course that also gets limited to the one host given by the -limit parameter. If there'd be a way to force this play to run on all hosts before the other plays, that would be perfect.

I've googled a bit and found the solution with fact caching with redis, but since our playbook is used locally, I wanted to avoid the need for additional software. I know, it's not a big deal, but I was just looking for a "cleaner", Ansible-only solution and was wondering, if that would exist.

like image 678
tehK Avatar asked May 04 '15 07:05

tehK


People also ask

How do you gather ansible facts?

The gather_facts module from the Ansible playbook runs the setup module by default at the start of each playbook to gather the facts about remote hosts. Fetch the Ansible facts and display them using a playbook. Fetching the Ansible facts, filtering them, and displaying them using a playbook.

What are the custom facts defined on a host?

Custom facts (local facts) are the variables which are declared on ansible managed host. Custom facts are declared in ini or json file in the /etc/ansible/facts. d directory on managed host. File names of custom facts must have .

How do you stop ansible gathering facts?

You can use gather_facts: no keyword in your playbook. It will disable this task automatically.

What is Pre_tasks in ansible?

What is pre_tasks in Ansible? pre_tasks is a task which Ansible executes before executing any tasks mentioned in . yml file. Consider this scenario. You provisioned a new instance on Amazon EC2 cloud or Google Cloud .


2 Answers

Ansible version 2 introduced a clean, official way to do this using delegated facts (see: http://docs.ansible.com/ansible/latest/playbooks_delegation.html#delegated-facts).

when: hostvars[item]['ansible_default_ipv4'] is not defined is a check to ensure you don't check for facts in a host you already know the facts about

--- # This play will still work as intended if called with --limit "<host>" or --tags "some_tag"  - name: Hostfile generation   hosts: all   become: true    pre_tasks:     - name: Gather facts from ALL hosts (regardless of limit or tags)       setup:       delegate_to: "{{ item }}"       delegate_facts: True       when: hostvars[item]['ansible_default_ipv4'] is not defined       with_items: "{{ groups['all'] }}"    tasks:     - template:         src: "templates/hosts.j2"         dest: "/etc/hosts"       tags:         - hostfile       ... 
like image 153
corford Avatar answered Sep 19 '22 12:09

corford


In general the way to get facts for all hosts even when you don't want to run tasks on all hosts is to do something like this:

- hosts: all   tasks: [ ]   

But as you mentioned, the --limit parameter will limit what hosts this would be applied to.

I don't think there's a way to simply tell Ansible to ignore the --limit parameter on any plays. However there may be another way to do what you want entirely within Ansible.

I haven't used it personally, but as of Ansible 1.8 fact caching is available. In a nutshell, with fact caching enabled Ansible will use a redis server to cache all the facts about hosts it encounters and you'll be able to reference them in subsequent playbooks:

With fact caching enabled, it is possible for machine in one group to reference variables about machines in the other group, despite the fact that they have not been communicated with in the current execution of /usr/bin/ansible-playbook.

like image 39
Bruce P Avatar answered Sep 19 '22 12:09

Bruce P