# How to get ansible inventory file when tasks are run on localhost

**URL:** <https://forum.ansible.com/t/how-to-get-ansible-inventory-file-when-tasks-are-run-on-localhost/4082>\
**Category:** Get Help\
**Created:** [February 27, 2024, 9:25am UTC](https://forum.ansible.com/t/how-to-get-ansible-inventory-file-when-tasks-are-run-on-localhost/4082 "2024-02-27T09:25:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![JoJo](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@JoJo](https://forum.ansible.com/u/JoJo)\
**Post date:** [February 27, 2024, 9:25am UTC](https://forum.ansible.com/t/how-to-get-ansible-inventory-file-when-tasks-are-run-on-localhost/4082/1 "2024-02-27T09:25:52Z")

</div>

Hi, I have a python script, that parses host file given on command line like this:

```auto
ansible-playbook -i hosts main.yml 

```

and in python script I used

```auto
from ansible.inventory.manager import InventoryManager
from ansible.parsing.dataloader import DataLoader
from ansible.vars.manager import VariableManager

dl = DataLoader()
im = InventoryManager(loader=dl, sources=['{{ inventory_file }}'])

```

The problem is using `localhost` makes `inventory_file` undefined. And python script does not need to be run on remote server, I hope it could run on local server.  
I verified above by command:

```auto
 ansible localhost -m debug -a "msg={{inventory_file}}"

```

And that `inverntory_file` is undefined, while given any other host group, it prints correct inventory file path, like this:

```auto
ansible -i hosts cseg -m debug -a "msg={{inventory_file}}"

```

So my question is, in order to get python script work on local machine, how can Python script get inventory\_file given on command line:

```auto
ansible-playbook -i hosts main.yml 

```

(main.yml has a task run python script on local machine.)

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [February 27, 2024, 12:52pm UTC](https://forum.ansible.com/t/how-to-get-ansible-inventory-file-when-tasks-are-run-on-localhost/4082/2 "2024-02-27T12:52:16Z")

</div>

Does having a `ansible.cfg` with the inventory file path in it help?

For example:

```ini
[defaults]
inventory = hosts

```

---

<div class="post-metadata">

**Author:** ![binbashroot](https://avatars.discourse-cdn.com/v4/letter/b/ad7895/32.png) [@binbashroot](https://forum.ansible.com/u/binbashroot)\
**Post date:** [February 27, 2024, 1:42pm UTC](https://forum.ansible.com/t/how-to-get-ansible-inventory-file-when-tasks-are-run-on-localhost/4082/3 "2024-02-27T13:42:19Z")

</div>

You have a couple of options here. One is to add localhost to your inventory and set the ansible\_connection for that host as “local”. You don’t have to add the host to a group. Another option is to run your command as such:

```auto
 ansible-playbook -i localhost, main.yml

```

Notice there is a comma right after the word localhost.

---

<div class="post-metadata">

**Author:** ![JoJo](https://avatars.discourse-cdn.com/v4/letter/j/ecd19e/32.png) [@JoJo](https://forum.ansible.com/u/JoJo)\
**Post date:** [February 28, 2024, 3:42am UTC](https://forum.ansible.com/t/how-to-get-ansible-inventory-file-when-tasks-are-run-on-localhost/4082/4 "2024-02-28T03:42:16Z")

</div>

Hi, thank you for your help. This is what I did after reading your post:  
For option 1, I added group `local` with one machine `localhost` or `127.0.0.1` in host file:

```auto
[local]
localhost

```

ansible-playbook result in an error “can not connect”. I think I may not understand option 1 in your post.  
for option 2, ansible-playbook -i hosts main.yml, python script needs inventory file on command line, and it is run locally, specified in role `host` as `localhost`.

---

<div class="post-metadata">

**Author:** ![ptn](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ptn/32/481_2.png) [@ptn](https://forum.ansible.com/u/ptn)\
**Post date:** [February 28, 2024, 8:40am UTC](https://forum.ansible.com/t/how-to-get-ansible-inventory-file-when-tasks-are-run-on-localhost/4082/5 "2024-02-28T08:40:32Z")

</div>

Ansible defaults to ssh connection mode for every host you target in your play, including localhost. You have to instruct it to use another mode (here local) to not use ssh; that’s what @binbashroot was referring to.

See: [Connection plugins — Ansible Documentation](https://docs.ansible.com/ansible/latest/plugins/connection.html) and [Controlling where tasks run: delegation and local actions — Ansible Documentation](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_delegation.html#local-playbooks)

Edit: To clarify, second option they proposed is about tricking Ansible to target an host not present in inventory. Sort of passing an inline inventory. Though it doesn’t change the connection mode, so you’d still have to mention it somewhere.

---

<div class="post-metadata">

**Author:** ![chris](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/chris/32/293_2.png) [@chris](https://forum.ansible.com/u/chris)\
**Post date:** [February 28, 2024, 10:11am UTC](https://forum.ansible.com/t/how-to-get-ansible-inventory-file-when-tasks-are-run-on-localhost/4082/6 "2024-02-28T10:11:05Z")

</div>

I use a YAML inventory works for this, in `ansible.cfg`:

```ini
[defaults]
inventory = hosts.yml

```

And in `hosts.yml`:

```yaml
---
all:
  children:
    localhosts:
      hosts:
        localhost:
          ansible_connection: local
...

```
