Force fact-gathering on all hosts

前端 未结 4 1844
夕颜
夕颜 2020-12-15 17:04

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 f

相关标签:
4条回答
  • 2020-12-15 17:46

    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.

    0 讨论(0)
  • 2020-12-15 17:48

    You could modify your playbook to:

    ...
    - hosts: "{{ limit_hosts|default('default_group') }}"
      tasks:
        ...
    ...
    

    And when you run it, if some_var is not defined (normal state) then it will run on the default_group inventory group, BUT if you run it as:

    ansible-playbook --extra-vars "limit_hosts=myHost" myplaybook.yml
    

    Then it will only run on your myHost, but you could still have other sections with different hosts: .. declarations, for fact gathering, or anything else actually.

    0 讨论(0)
  • 2020-12-15 17:58

    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
    
         ...
    
    0 讨论(0)
  • 2020-12-15 17:58

    This still seems to be an issue without a clean solution here in 2016, but newer versions of Ansible offer a "jsonfile" fact caching backend, which seems to be a decent compromise to installing Redis locally just to address this need. Now I just fire off an ansible all -m setup before running a playbook with the --limit option. Good enough for jazz!

    http://docs.ansible.com/ansible/playbooks_variables.html#fact-caching

    0 讨论(0)
提交回复
热议问题