Ansible Inventory Issue: $PWD appended ahead of inventory file

I’ve got a very odd issue with my Ansible inventory that has just started and I cannot find the culprit. This script is one that we’ve run for years. It collects some data from the user and then runs the ansible playbook using the python3 subprocess module.

When I enable verbosity in the script I think I’ve found what is causing the issues. For some reason the present working directory from where I run the script is appended with a space ahead of the inventory file that is defined.

For example, if I’m in /etc/ansible when I run the script, then the inventory in the output appears as:
inventory: (‘/etc/ansible /etc/ansible/inventory/routers’)

If I’m in my home directory when I run the script, then the inventory in the output appears as:
inventory: (‘/home/ /etc/ansible/inventory/routers’)

However, if I run the playbook with the same variables directly, without the script, the inventory ansible is now looking for is:
inventory: (‘/etc/ansible/inventory/routers’)

And the device I’m working on is now found in the inventory and the playbook proceeds to run.

Prior to debugging this, the inventory wasn’t defined in the script (or on the CLI) and was defined in the /etc/ansible/ansible.cfg with:
inventory = /etc/ansible/inventory/

But since the issue started I added the explicit inventory file to use in an attempt to test and that still did not fix the issue.

I also have verified that the ansible version and directories are all the same both on the script run and my manual run.

$ ansible --version
ansible [core 2.12.6]
config file = /etc/ansible/ansible.cfg
configured module search path = [‘/home//.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/local/lib/python3.9/site-packages/ansible
ansible collection location = /etc/ansible/collections:/home//.ansible/collections
executable location = /usr/local/bin/ansible
python version = 3.9.7 (default, Sep 13 2021, 08:18:39) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
jinja version = 3.1.2
libyaml = True

Anyone have an idea on where I can look for the culprit that has suddenly started appending the $PWD ahead of the inventory file, but only when run from the script.

I’ve got a very odd issue with my Ansible inventory that has just started and I cannot find the culprit. This script is one that we’ve run for years. It collects some data from the user and then runs the ansible playbook using the python3 subprocess module.

When I enable verbosity in the script I think I’ve found what is causing the issues. For some reason the present working directory from where I run the script is appended with a space ahead of the inventory file that is defined.

For example, if I’m in /etc/ansible when I run the script, then the inventory in the output appears as:
inventory: (‘/etc/ansible /etc/ansible/inventory/routers’)

If I’m in my home directory when I run the script, then the inventory in the output appears as:
inventory: (‘/home/ /etc/ansible/inventory/routers’)

However, if I run the playbook with the same variables directly, without the script, the inventory ansible is now looking for is:
inventory: (‘/etc/ansible/inventory/routers’)

And the device I’m working on is now found in the inventory and the playbook proceeds to run.

Prior to debugging this, the inventory wasn’t defined in the script (or on the CLI) and was defined in the /etc/ansible/ansible.cfg with:
inventory = /etc/ansible/inventory/

But since the issue started I added the explicit inventory file to use in an attempt to test and that still did not fix the issue.

I also have verified that the ansible version and directories are all the same both on the script run and my manual run.

$ ansible --version
ansible [core 2.12.6]
config file = /etc/ansible/ansible.cfg
configured module search path = [‘/home//.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/local/lib/python3.9/site-packages/ansible
ansible collection location = /etc/ansible/collections:/home//.ansible/collections
executable location = /usr/local/bin/ansible
python version = 3.9.7 (default, Sep 13 2021, 08:18:39) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]
jinja version = 3.1.2
libyaml = True

Anyone have an idea on where I can look for the culprit that has suddenly started appending the $PWD ahead of the inventory file, but only when run from the script.

If things work without the script, and fail with the script, then logic dictates that the script is causing the problem.

If this mysterious script has suddenly started changed its behaviour, then you need to find out what why, for example what changed on that host, and when.
Perhaps run it on another host?
With the supplied amount information this is about as much help there is possible.

Just an update:

In trying to fix an issue for the user that reported the problem I created an additional issue that I was overlooking.

In haste I added the -i flag as a single string ( […, ‘-i /etc/ansible/inventory/routers’, …] ) in the subprocess.run() list when it should have been 2 strings ( […, ‘-i’, ‘/etc/ansible/inventory/routers’, …] )

Now I can move on to debugging the real problem for the user and not the problem that I created.

I’m confused … does the original problem reported above still exist?