Repository: ansible/ansible

Issue:
ec2.py Script .ini Filename Incorrectly Generated
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report


##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
ec2.py Dynamic Inventory Script

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.3.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4]
```

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->
Defaults
##### 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 -->
This PR has broken the expected behaviour for the `ec2.py` Dynamic Inventory script - https://github.com/ansible/ansible/pull/21567/files

The docs recommend the following -
>The second option is to copy the script to /etc/ansible/hosts and chmod +x it. You will also need to copy the ec2.ini file to /etc/ansible/ec2.ini. Then you can run ansible as you would normally.

Since this PR was merged the script looks for a corresponding `.ini` (in the same directory) based on the script filename rather than previous default of `ec2.ini`.

Therefore if the docs are followed as quoted above it is looking for `/etc/ansible/hosts.ini` rather than `/etc/ansible/ec2.ini`.
##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->

1. Copy `ec2.py` to `/etc/ansible/hosts` and `chmod +x` it
2. Copy `ec2.ini` to `/etc/ansible/ec2.ini`
3. Run `/etc/ansible/hosts --list`

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Entire ec2 Inventory should be output in JSON format.
##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
Script fails with the following Traceback -
<!--- Paste verbatim command output between quotes below -->
```
$ /etc/ansible/hosts --list
Traceback (most recent call last):
  File "/etc/ansible/hosts", line 1587, in <module>
    Ec2Inventory()
  File "/etc/ansible/hosts", line 181, in __init__
    self.read_settings()
  File "/etc/ansible/hosts", line 252, in read_settings
    configRegions = config.get('ec2', 'regions')
  File "/usr/lib/python2.7/ConfigParser.py", line 607, in get
    raise NoSectionError(section)
ConfigParser.NoSectionError: No section: 'ec2'
```

ec2.py Script .ini Filename Incorrectly Generated
<!---
Verify first that your issue/request is not already reported on GitHub.
Also test if the latest release, and master branch are affected too.
-->

##### ISSUE TYPE
<!--- Pick one below and delete the rest: -->
 - Bug Report


##### COMPONENT NAME
<!--- Name of the module/plugin/task/feature -->
ec2.py Dynamic Inventory Script

##### ANSIBLE VERSION
<!--- Paste verbatim output from “ansible --version” between quotes below -->
```
ansible 2.3.0.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.7.6 (default, Oct 26 2016, 20:30:19) [GCC 4.8.4]
```

##### CONFIGURATION
<!---
Mention any settings you have changed/added/removed in ansible.cfg
(or using the ANSIBLE_* environment variables).
-->
Defaults
##### 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 -->
This PR has broken the expected behaviour for the `ec2.py` Dynamic Inventory script - https://github.com/ansible/ansible/pull/21567/files

The docs recommend the following -
>The second option is to copy the script to /etc/ansible/hosts and chmod +x it. You will also need to copy the ec2.ini file to /etc/ansible/ec2.ini. Then you can run ansible as you would normally.

Since this PR was merged the script looks for a corresponding `.ini` (in the same directory) based on the script filename rather than previous default of `ec2.ini`.

Therefore if the docs are followed as quoted above it is looking for `/etc/ansible/hosts.ini` rather than `/etc/ansible/ec2.ini`.
##### STEPS TO REPRODUCE
<!---
For bugs, show exactly how to reproduce the problem, using a minimal test-case.
For new features, show how the feature would be used.
-->

1. Copy `ec2.py` to `/etc/ansible/hosts` and `chmod +x` it
2. Copy `ec2.ini` to `/etc/ansible/ec2.ini`
3. Run `/etc/ansible/hosts --list`

<!--- You can also paste gist.github.com links for larger files -->

##### EXPECTED RESULTS
<!--- What did you expect to happen when running the steps above? -->
Entire ec2 Inventory should be output in JSON format.
##### ACTUAL RESULTS
<!--- What actually happened? If possible run with extra verbosity (-vvvv) -->
Script fails with the following Traceback -
<!--- Paste verbatim command output between quotes below -->
```
$ /etc/ansible/hosts --list
Traceback (most recent call last):
  File "/etc/ansible/hosts", line 1587, in <module>
    Ec2Inventory()
  File "/etc/ansible/hosts", line 181, in __init__
    self.read_settings()
  File "/etc/ansible/hosts", line 252, in read_settings
    configRegions = config.get('ec2', 'regions')
  File "/usr/lib/python2.7/ConfigParser.py", line 607, in get
    raise NoSectionError(section)
ConfigParser.NoSectionError: No section: 'ec2'
```



Discussion:
I thought when we introduced that change to look for an ini-file matching its own name that it was supposed to fall back to ec2.ini, but it seems it isn't. 

In the meantime, you can either rename to hosts.ini, or use an older version of the dynamic inventory script. 
Yeah we've managed to workaround using a symlink from `hosts.ini` to `ec2.ini` but it did have us scratching our heads for about a day.

Side note - The error isn't particularly helpful - It would have been easier to troubleshoot if it said file not found or similar.
I thought when we introduced that change to look for an ini-file matching its own name that it was supposed to fall back to ec2.ini, but it seems it isn't. 

In the meantime, you can either rename to hosts.ini, or use an older version of the dynamic inventory script. 
Yeah we've managed to workaround using a symlink from `hosts.ini` to `ec2.ini` but it did have us scratching our heads for about a day.

Side note - The error isn't particularly helpful - It would have been easier to troubleshoot if it said file not found or similar.

Resolving patch:
diff --git a/contrib/inventory/ec2.py b/contrib/inventory/ec2.py
--- a/contrib/inventory/ec2.py
+++ b/contrib/inventory/ec2.py
@@ -226,6 +226,7 @@ def read_settings(self):
 
         defaults = {
             'ec2': {
+                'ini_fallback': os.path.join(os.path.dirname(__file__), 'ec2.ini'),
                 'ini_path': os.path.join(os.path.dirname(__file__), '%s.ini' % scriptbasename)
             }
         }
@@ -236,6 +237,10 @@ def read_settings(self):
             config = configparser.SafeConfigParser()
         ec2_ini_path = os.environ.get('EC2_INI_PATH', defaults['ec2']['ini_path'])
         ec2_ini_path = os.path.expanduser(os.path.expandvars(ec2_ini_path))
+
+        if not os.path.isfile(ec2_ini_path):
+            ec2_ini_path = os.path.expanduser(defaults['ec2']['ini_fallback'])
+
         config.read(ec2_ini_path)
 
         # is eucalyptus?


Test patch: