Repository: ansible/ansible

Issue:
Hostnames can't start with a range in inventory as of 2.0.1.0
<!--- Verify first that your issue/request is not already reported in GitHub -->
##### ISSUE TYPE

<!--- Pick one below and delete the rest: -->
- Bug Report
##### ANSIBLE VERSION

```
ansible 2.0.1.0
```
##### CONFIGURATION

<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->

```
% cat ansible.cfg
[defaults]
hostfile = inventory

% cat inventory
[test]
[1:2].fail
fail.[1:2]
fail.[1:2].fail
```
##### OS / ENVIRONMENT

<!---
Mention the OS you are running Ansible from, and the OS you are
managing, or say “N/A” for anything that is not platform-specific.
-->

N/A
##### SUMMARY

<!--- Explain the problem briefly -->

The fix for this issue breaks hosts that start with a range:
https://github.com/ansible/ansible/issues/13917
This commit:
https://github.com/ansible/ansible/commit/a7dd425620e0a40911908a41e1b378183ae690f4
##### STEPS TO REPRODUCE

<!---
For bugs, show exactly how to reproduce the problem.
For new features, show how the feature would be used.
-->

setup config and inventory as shown above, then run:

```
ansible test --list-hosts

```

<!--- You can also paste gist.github.com links for larger files -->
##### EXPECTED RESULTS

<!--- What did you expect to happen when running the steps above? -->

```
ansible test --list-hosts
  hosts (6):
    1.fail
    2.fail
    fail.1
    fail.2
    fail.1.fail
    fail.2.fail
```
##### ACTUAL RESULTS

<!--- What actually happened? If possible run with high verbosity (-vvvv) -->

```
ansible test --list-hosts
ERROR! inventory:2: Invalid section entry: '[1:2].fail'. Please make sure that there are no spacesin the section entry, and that there are no other invalid characters

```

Changing line 127 in ansible/inventory/ini.py to the following might fix both issues, since ranges have to have something either before or after or both for them to be useful. In other words, if it starts and ends with square brackets and doesn't match the section regex, then it is an actual error.

```
            elif line.startswith('[') and line.endswith(']'):
```



Discussion:
Hello,

I tried the change in the ini.py file but the error persists:

`ansible-playbook -vvvvvv playbook.yml 
Using /etc/ansible/ansible.cfg as config file
ERROR! /etc/ansible/hosts:39: Invalid section entry: '[Serverlist]'. Please make sure that there are no spacesin the section entry, and that there are no other invalid characters`

I am running 2.0.1 as well.

It looks like your use case is different from mine. The problem that this bug and fix addresses are cases where an Ansible range starts the line. Like "^[1:2].fail$" (with ^ and $ added as start and end of line).

Your error doesn't include a range at all, and I suspect that you have some unicode characters or something invalid before line 39. If you add part of your /etc/ansible/hosts I'll take a look at it.

Well, if I comment out the lines with brackets everything works perfectly. I upgraded from 2.0.0 where everything worked fine with the same hosts file.

Here is a section copied from a freshly created hosts file, the error still persists:

```
server1.subdomain.domain.org

[Serverlist]
myserver.subdomain.domain.org
```

I setup ansible.cfg the same as earlier, with inventory/hosts containing exactly what you showed above and got the following results:

% ansible --version
ansible 2.0.1.0
  config file = /home/test/ansible_db/ansible.cfg
  configured module search path = Default w/o overrides

% ansible all --list-hosts
  hosts (2):
    server1.subdomain.domain.org
    myserver.subdomain.domain.org

So I'm still not sure what's causing the error in your case, I'd need more details to be able to reproduce it.


Resolving patch:
diff --git a/lib/ansible/inventory/ini.py b/lib/ansible/inventory/ini.py
--- a/lib/ansible/inventory/ini.py
+++ b/lib/ansible/inventory/ini.py
@@ -124,7 +124,7 @@ def _parse(self, lines):
                     del pending_declarations[groupname]
 
                 continue
-            elif line.startswith('['):
+            elif line.startswith('[') and line.endswith(']'):
                 self._raise_error("Invalid section entry: '%s'. Please make sure that there are no spaces" % line + \
                                   "in the section entry, and that there are no other invalid characters")
 


Test patch: