title: How to filter gathering facts inside a playbook?
body: <p>I'm working on a role that only needs to gather a single fact. </p>
  
  <p>Performance it's a concern and I know that gathering facts it's time-consuming.</p>
  
  <p>I'm looking for some way to filter <code>gather_facts</code> inside a playbook, this will allow me to gather only the required facts. </p>
  
  <p>This is possible using the setup core module:</p>
  
  <pre><code>ansible -m setup -a 'filter=ansible_hostname' my_host
  
  10.200.0.127 | success &gt;&gt; {
      "ansible_facts": {
          "ansible_hostname": "my_host"
      },
      "changed": false
  }
  </code></pre>
  
  <p>It's possible to use this feature inside the playbook? Something like this?</p>
  
  <pre><code>- hosts: all
    sudo: yes
    gather_facts: True
      filter: "filter=ansible_*"
  </code></pre>
  
  <p><strong><em>PS:</strong> The code above throws syntax exception.</em></p>
  
  <p><strong>EDIT 1</strong>: If someone needs to get hostname there's also another useful variable <a href="http://docs.ansible.com/ansible/playbooks_variables.html#magic-variables-and-how-to-access-information-about-other-hosts" rel="noreferrer">inventory_hostname</a>.</p>