Repository: ansible/ansible

Issue:
 AttributeError: 'Host' object has no attribute 'remove_group' in 2.2.2.0
##### ISSUE TYPE
 - Bug Report

##### COMPONENT NAME
```ansible/inventory/__init__.py", line 181```

##### ANSIBLE VERSION
```
ansible 2.2.2.0
  config file = /home/ubuntu/hoist/ansible.cfg
  configured module search path = Default w/o overrides
```

##### OS / ENVIRONMENT
N/A

##### SUMMARY
In the upgrade to 2.2.2.0 we started seeing the error: 
```
ERROR! Unexpected Exception: 'Host' object has no attribute 'remove_group'
```
It _looks_ like when https://github.com/ansible/ansible/commit/36b462ba857361ba310c7967a721c025c13d2225 was cherry-picked but not https://github.com/ansible/ansible/commit/c4cff44e772e8b4631386243169d164bc0b0937c which actually adds the remove_group to the Host object.

##### STEPS TO REPRODUCE
The inventory file we are using was a stub to do a --syntax-check and looked like: 
```
test-bastion ansible_ssh_host=127.0.0.1 ansible_ssh_user=ubuntu

[bastion]
test-bastion
```
Full command was (playbook doesn't matter): 
```
ansible-playbook -vvvvv -i tests/files/test-inventory --syntax-check restart.yml
```

##### EXPECTED RESULTS
Runs syntax check in 2.2.1.0

##### ACTUAL RESULTS
```
Traceback (most recent call last):
  File "/home/ubuntu/.local/bin/ansible-playbook", line 103, in <module>
    exit_code = cli.run()
  File "/home/ubuntu/.local/venvs/ansible/local/lib/python2.7/site-packages/ansible/cli/playbook.py", line 132, in run
    inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=self.options.inventory)
  File "/home/ubuntu/.local/venvs/ansible/local/lib/python2.7/site-packages/ansible/inventory/__init__.py", line 97, in __init__
    self.parse_inventory(host_list)
  File "/home/ubuntu/.local/venvs/ansible/local/lib/python2.7/site-packages/ansible/inventory/__init__.py", line 181, in parse_inventory
    host.remove_group(ungrouped)
AttributeError: 'Host' object has no attribute 'remove_group'
```


Discussion:

Resolving patch:
diff --git a/lib/ansible/inventory/__init__.py b/lib/ansible/inventory/__init__.py
--- a/lib/ansible/inventory/__init__.py
+++ b/lib/ansible/inventory/__init__.py
@@ -185,7 +185,6 @@ def parse_inventory(self, host_list):
                 if length == 0 or (length == 1 and all in mygroups):
                     ungrouped.add_host(host)
 
-
     def _match(self, str, pattern_str):
         try:
             if pattern_str.startswith('~'):
diff --git a/lib/ansible/inventory/group.py b/lib/ansible/inventory/group.py
--- a/lib/ansible/inventory/group.py
+++ b/lib/ansible/inventory/group.py
@@ -114,6 +114,12 @@ def add_host(self, host):
         host.add_group(self)
         self.clear_hosts_cache()
 
+    def remove_host(self, host):
+
+        self.hosts.remove(host)
+        host.remove_group(self)
+        self.clear_hosts_cache()
+
     def set_variable(self, key, value):
 
         self.vars[key] = value
diff --git a/lib/ansible/inventory/host.py b/lib/ansible/inventory/host.py
--- a/lib/ansible/inventory/host.py
+++ b/lib/ansible/inventory/host.py
@@ -112,6 +112,10 @@ def add_group(self, group):
 
         self.groups.append(group)
 
+    def remove_group(self, group):
+
+        self.groups.remove(group)
+
     def set_variable(self, key, value):
 
         self.vars[key]=value


Test patch: