Repository: ansible/ansible-runner

Issue:
Allow use of Inventory defined in ansible.cfg
If `inventory` is not given to the runner, the configuration defaults to `private_data_dir/inventory`.
https://github.com/ansible/ansible-runner/blob/31b4174663e3562af30e85175e833bf047e3df14/ansible_runner/runner_config.py#L200-L201

So with this, its always defined an `-i` option to the ansible playbook. I want to run playbooks in a git repository, which provides own inventories from the repository.

I think of defaulting to no `-i` given if no inventory is specified, this leads in use of the inventory defined in `ansible.cfg` and its precedence.

Discussion:
ansible-zuul[bot]: Build failed.

- [ansible-tox-py36 ](https://logs.zuul.ansible.com/09/309/deaeca9548d3fdcc17bba40a929003203a378838/check/ansible-tox-py36/bf52091/) : FAILURE in 6m 41s
- [ansible-tox-py37 ](https://logs.zuul.ansible.com/09/309/deaeca9548d3fdcc17bba40a929003203a378838/check/ansible-tox-py37/57b6baf/) : FAILURE in 8m 20s
- [ansible-build-python-tarball ](https://logs.zuul.ansible.com/09/309/deaeca9548d3fdcc17bba40a929003203a378838/check/ansible-build-python-tarball/0367337/) : SUCCESS in 2m 28s
- [ansible-tox-linters ](https://logs.zuul.ansible.com/09/309/deaeca9548d3fdcc17bba40a929003203a378838/check/ansible-tox-linters/b3003dc/) : SUCCESS in 4m 34s
- [ansible-tox-py27 ](https://logs.zuul.ansible.com/09/309/deaeca9548d3fdcc17bba40a929003203a378838/check/ansible-tox-py27/ddd68c6/) : FAILURE in 5m 34s

ansible-zuul[bot]: Build failed.

- [ansible-tox-py36 ](https://logs.zuul.ansible.com/09/309/f6a147c30c09327d9fdfddc4ec4446efc5f772fb/check/ansible-tox-py36/ceb73b6/) : FAILURE in 7m 47s
- [ansible-tox-py37 ](https://logs.zuul.ansible.com/09/309/f6a147c30c09327d9fdfddc4ec4446efc5f772fb/check/ansible-tox-py37/9336fd7/) : FAILURE in 7m 11s
- [ansible-build-python-tarball ](https://logs.zuul.ansible.com/09/309/f6a147c30c09327d9fdfddc4ec4446efc5f772fb/check/ansible-build-python-tarball/991f9f2/) : SUCCESS in 2m 24s
- [ansible-tox-linters ](https://logs.zuul.ansible.com/09/309/f6a147c30c09327d9fdfddc4ec4446efc5f772fb/check/ansible-tox-linters/64f792b/) : SUCCESS in 4m 42s
- [ansible-tox-py27 ](https://logs.zuul.ansible.com/09/309/f6a147c30c09327d9fdfddc4ec4446efc5f772fb/check/ansible-tox-py27/c0941c0/) : FAILURE in 13m 03s


Patch:
diff --git a/ansible_runner/runner_config.py b/ansible_runner/runner_config.py
index 46d1f7d..69291f3 100644
--- a/ansible_runner/runner_config.py
+++ b/ansible_runner/runner_config.py
@@ -201,7 +201,7 @@ class RunnerConfig(object):
         """
         Prepares the inventory default under ``private_data_dir`` if it's not overridden by the constructor.
         """
-        if self.inventory is None:
+        if self.inventory is None and os.path.exists(os.path.join(self.private_data_dir, "inventory")):
             self.inventory  = os.path.join(self.private_data_dir, "inventory")
 
     def prepare_env(self):
@@ -316,7 +316,9 @@ class RunnerConfig(object):
         except ConfigurationError:
             pass
 
-        if isinstance(self.inventory, list):
+        if self.inventory is None:
+            pass
+        elif isinstance(self.inventory, list):
             for i in self.inventory:
                 exec_list.append("-i")
                 exec_list.append(i)


Test patch:
diff --git a/test/unit/test_runner_config.py b/test/unit/test_runner_config.py
index dd5d15b..606ccc6 100644
--- a/test/unit/test_runner_config.py
+++ b/test/unit/test_runner_config.py
@@ -22,7 +22,6 @@ from ansible_runner.runner_config import RunnerConfig, ExecutionMode
 from ansible_runner.loader import ArtifactLoader
 from ansible_runner.exceptions import ConfigurationError
 
-
 try:
     Pattern = re._pattern_type
 except AttributeError:
@@ -190,7 +189,8 @@ def test_prepare_env_directory_isolation():
         assert rc.cwd == '/tmp/foo'
 
 
-def test_prepare_inventory():
+@patch('os.path.exists', return_value=True)
+def test_prepare_inventory(path_exists):
     rc = RunnerConfig(private_data_dir='/')
     rc.prepare_inventory()
     assert rc.inventory == '/inventory'
@@ -200,11 +200,17 @@ def test_prepare_inventory():
     rc.inventory = 'localhost,anotherhost,'
     rc.prepare_inventory()
     assert rc.inventory == 'localhost,anotherhost,'
+    path_exists.return_value = False
+    rc.inventory = None
+    rc.prepare_inventory()
+    assert rc.inventory is None
 
 
 def test_generate_ansible_command():
     rc = RunnerConfig(private_data_dir='/', playbook='main.yaml')
-    rc.prepare_inventory()
+    with patch('os.path.exists') as path_exists:
+        path_exists.return_value=True
+        rc.prepare_inventory()
     rc.extra_vars = None
 
     cmd = rc.generate_ansible_command()
@@ -235,8 +241,14 @@ def test_generate_ansible_command():
     assert cmd == ['ansible-playbook', 'main.yaml']
     rc.inventory = None
 
+    with patch('os.path.exists', return_value=False) as path_exists:
+        rc.prepare_inventory()
+        cmd = rc.generate_ansible_command()
+    assert cmd == ['ansible-playbook', 'main.yaml']
+
     rc.verbosity = 3
-    rc.prepare_inventory()
+    with patch('os.path.exists', return_value=True) as path_exists:
+        rc.prepare_inventory()
     cmd = rc.generate_ansible_command()
     assert cmd == ['ansible-playbook', '-i', '/inventory', '-vvv', 'main.yaml']
     rc.verbosity = None
@@ -265,7 +277,9 @@ def test_generate_ansible_command():
 
 def test_generate_ansible_command_with_api_extravars():
     rc = RunnerConfig(private_data_dir='/', playbook='main.yaml', extravars={"foo":"bar"})
-    rc.prepare_inventory()
+    with patch('os.path.exists') as path_exists:
+        path_exists.return_value=True
+        rc.prepare_inventory()
 
     cmd = rc.generate_ansible_command()
     assert cmd == ['ansible-playbook', '-i', '/inventory', '-e', 'foo="bar"', 'main.yaml']
@@ -277,7 +291,9 @@ def test_generate_ansible_command_with_api_extravars():
 ])
 def test_generate_ansible_command_with_cmdline_args(cmdline):
     rc = RunnerConfig(private_data_dir='/', playbook='main.yaml')
-    rc.prepare_inventory()
+    with patch('os.path.exists') as path_exists:
+        path_exists.return_value=True
+        rc.prepare_inventory()
     rc.extra_vars = {}
 
     cmdline_side_effect = partial(load_file_side_effect, 'env/cmdline', cmdline)
@@ -408,7 +424,9 @@ def test_process_isolation_defaults():
     rc.playbook = 'main.yaml'
     rc.command = 'ansible-playbook'
     rc.process_isolation = True
-    rc.prepare()
+    with patch('os.path.exists') as path_exists:
+        path_exists.return_value=True
+        rc.prepare()
 
     assert rc.command == [
         'bwrap',
@@ -426,14 +444,20 @@ def test_process_isolation_defaults():
 @patch('tempfile.mkdtemp', return_value="/tmp/dirisolation/foo")
 @patch('os.chmod', return_value=True)
 @patch('shutil.rmtree', return_value=True)
-def test_process_isolation_and_directory_isolation(mock_makedirs, mock_copytree, mock_mkdtemp, mock_chmod, mock_rmtree):
+def test_process_isolation_and_directory_isolation(mock_makedirs, mock_copytree, mock_mkdtemp,
+                                                   mock_chmod, mock_rmtree):
+    def new_exists(path):
+        if path == "/project":
+            return False
+        return True
     rc = RunnerConfig('/')
     rc.artifact_dir = '/tmp/artifacts'
     rc.directory_isolation_path = '/tmp/dirisolation'
     rc.playbook = 'main.yaml'
     rc.command = 'ansible-playbook'
     rc.process_isolation = True
-    rc.prepare()
+    with patch('os.path.exists', new=new_exists):
+        rc.prepare()
 
     assert rc.command == [
         'bwrap',