Repository: ansible/ansible

Issue:
--limit does not work with ansible-inventory CLI
##### ISSUE TYPE
 - Bug Report
 - Feature Idea

##### COMPONENT NAME
ansible-inventory CLI

##### ANSIBLE VERSION
```
ansible --version
ansible 2.5.0
  config file = None
  configured module search path = [u'/Users/alancoding/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/alancoding/.virtualenvs/ansible/lib/python2.7/site-packages/ansible-2.5.0-py2.7.egg/ansible
  executable location = /Users/alancoding/.virtualenvs/ansible/bin/ansible
  python version = 2.7.10 (default, Jul 13 2015, 12:05:58) [GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]
```

##### CONFIGURATION
```
$ ansible-config dump --only-changed
ANSIBLE_NOCOWS(env: ANSIBLE_NOCOWS) = True
```

##### OS / ENVIRONMENT
Mac

##### SUMMARY
`ansible-inventory` command does not respect `--limit` option.

##### STEPS TO REPRODUCE
I'm using a basic example inventory file example with 2 groups and 4 hosts.

https://github.com/AlanCoding/Ansible-inventory-file-examples/blob/master/top_level_file.ini

```
$ cat top_level_file.ini 
[fooland]
foo1
foo2

[barland]
bar1
bar2
```

To reproduce:

```
ansible-inventory -l foo1 -i top_level_file.ini --list
```

or

```
ansible-inventory --limit foo1 -i top_level_file.ini --list
```

##### EXPECTED RESULTS
I expect the only hosts in the output to be "foo1".

##### ACTUAL RESULTS
All hosts are returned. `--limit` or `-l` has no effect.

```
$ ansible-inventory -l foo1 -i top_level_file.ini --list
{
    "_meta": {
        "hostvars": {
            "bar1": {}, 
            "bar2": {}, 
            "foo1": {}, 
            "foo2": {}
        }
    }, 
    "all": {
        "children": [
            "barland", 
            "fooland", 
            "ungrouped"
        ]
    }, 
    "barland": {
        "hosts": [
            "bar1", 
            "bar2"
        ]
    }, 
    "fooland": {
        "hosts": [
            "foo1", 
            "foo2"
        ]
    }, 
    "ungrouped": {}
}
```

Trying `ansible-inventory --list-hosts foo1 -i top_level_file.ini --list` also doesn't work, although I didn't think its functionality makes sense with `ansible-inventory` anyway.


Discussion:
not sure it makes sense, `--limit` has never really affected the inventory itself, just the response to the play selection criteria aka `hosts:`.
Alright, I'm okay to close this. I get what you're saying about how this is really asking for novel functionality, and there's not any real need driving that.
keeping open, as we might need to remove the option from ansible-inventory if it is of no use, but it might  make sense for the --list-hosts option.
Either the --limit needs to be removed from the --help (and docs) or this needs to work as it's just silly as it stands right now.

Personally I'd love to see `ansible-inventory --limit` work as it's a great way to list variables for a given target, and being able to "pre filter" using that is much better than trying to "post filter" with jq etc  
I spent quite a while looking at this until I found this issue.  I too was hoping that ```--limit``` was going to filter inventory output.

Resolving patch:
diff --git a/lib/ansible/cli/inventory.py b/lib/ansible/cli/inventory.py
--- a/lib/ansible/cli/inventory.py
+++ b/lib/ansible/cli/inventory.py
@@ -78,6 +78,10 @@ def parse(self):
             basedir_opts=True,
         )
 
+        # remove unused default options
+        self.parser.remove_option('--limit')
+        self.parser.remove_option('--list-hosts')
+
         # Actions
         action_group = optparse.OptionGroup(self.parser, "Actions", "One of following must be used on invocation, ONLY ONE!")
         action_group.add_option("--list", action="store_true", default=False, dest='list', help='Output all hosts info, works as inventory script')
@@ -86,8 +90,6 @@ def parse(self):
                                 help='create inventory graph, if supplying pattern it must be a valid group name')
         self.parser.add_option_group(action_group)
 
-        # Options
-
         # graph
         self.parser.add_option("-y", "--yaml", action="store_true", default=False, dest='yaml',
                                help='Use YAML format instead of default JSON, ignored for --graph')


Test patch: