Repository: ansible/ansible

Issue:
[REQUEST] Configuration option to suppress warnings of empty inventory
##### ISSUE TYPE

Feature Idea
##### COMPONENT NAME

?
##### ANSIBLE VERSION

```
ansible 2.1.1.0
  config file =
  configured module search path = Default w/o overrides
```
##### CONFIGURATION

None
##### OS / ENVIRONMENT

N/A
##### SUMMARY

I am working on a product that can use Ansible (not only it though) to perform some tasks on localhost. In this case it is always ran against localhost. Thus there is never ever inventory or hosts file.

As the result I always see in output:

```
 [WARNING]: Host file not found: /etc/ansible/hosts
 [WARNING]: provided hosts list is empty, only localhost is available
```
##### STEPS TO REPRODUCE

Create playbook `example.yml`:

```
- hosts: localhost
  connection: local
  tasks:
    - name: Ensure package is latest is latest
      yum: name=nano state=latest
```

Execute it with:

```
$ ansible-playbook example.yml
```
##### EXPECTED RESULTS

Don't see warning, because there is no hosts files and no inventory by intention (should be rather obvious from `hosts: localhost`)
##### ACTUAL RESULTS

See two warning as in above.



Discussion:
I would really like this as well, for a different use case - creating a really simple "hello world" playbook as first step of an Ansible presentation/demo, like this:

```
- hosts: localhost
  connection: local

  tasks:
  - name: Say hello
    debug: msg="hello world"
```
The idea is to show some Ansible basics without getting into the complexity of inventory right away.

To achieve a simple looking `ansible-playbook hello.yml` without warnings, I have to:

1. Create an Ansible inventory file `local`, containing `localhost` only.
2. Create `ansible.cfg` with `inventory = local` setting.

I can also suppress the warnings by  using `-i local` on command line, but that requires the inventory that repeats the 'localhost'.

Since `hosts: localhost` is specified, maybe this could be used to suppress the  need for a redundant 'localhost only' inventory, and the warning?

Alternatively, how about a `--no-warnings` option that suppresses all Ansible warnings?

This would be especially useful for me, so I could do something like this:
`ANSIBLE_CALLBACK_WHITELIST=json ANSIBLE_STDOUT_CALLBACK=json ansible-playbook play.yml` and parse output as json, with no need to catch json with regexp
+1, particularly for use with ansible-pull (where the whole point is to operate on localhost).

Of course I can manually create an `/etc/ansible/hosts` consisting solely of 

    localhost ansible_connection=local

but that defeats the purpose of offering implicit localhost (and adds to the list of bootstrapping tasks that must be performed before launching ansible-pull)
+1 it would be nice if the warning didn't show any time you used `--limit localhost` since you clearly are trying to run on localhost.

We have a dev playbook to provision some stuff on mac laptops, but it's a common source of confusion for people on the team who don't know much about ansible that the output starts off with a big WARNING message even though it ran successfully.
Is there any update regarding this issue? 
I completely agree that this would be a useful change. If the playbook is targeted toward localhost and you're using ansible-pull, the inventory file warnings are a useless eyesore.

I would go as far as to say that the ansible-pull binary should never warn if there is no inventory file at all, since I can't imagine a use-case where an inventory file would be useful with ansible-pull.

Resolving patch:
diff --git a/lib/ansible/cli/__init__.py b/lib/ansible/cli/__init__.py
--- a/lib/ansible/cli/__init__.py
+++ b/lib/ansible/cli/__init__.py
@@ -819,7 +819,8 @@ def get_host_list(inventory, subset, pattern='all'):
         no_hosts = False
         if len(inventory.list_hosts()) == 0:
             # Empty inventory
-            display.warning("provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'")
+            if C.LOCALHOST_WARNING:
+                display.warning("provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'")
             no_hosts = True
 
         inventory.subset(subset)


Test patch: