# Needs some explaining. Don't understand why ansible is trying to ssh to host

**URL:** <https://forum.ansible.com/t/needs-some-explaining-dont-understand-why-ansible-is-trying-to-ssh-to-host/3765>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [February 8, 2024, 7:31pm UTC](https://forum.ansible.com/t/needs-some-explaining-dont-understand-why-ansible-is-trying-to-ssh-to-host/3765 "2024-02-08T19:31:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dragao](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@Dragao](https://forum.ansible.com/u/Dragao)\
**Post date:** [February 8, 2024, 7:31pm UTC](https://forum.ansible.com/t/needs-some-explaining-dont-understand-why-ansible-is-trying-to-ssh-to-host/3765/1 "2024-02-08T19:31:18Z")

</div>

Hi All,

I am new to Ansible and I am trying to understand something.

I have a simple playbook which will read a csv and display its contents.  
But for some reason ansible is trying to ssh into the machine.

Here is my playbook:

```auto
---

- name: create list from csv
  hosts: localhost
  gather_facts: no
  tasks:
    - name: Read CSV
      read_csv:
        path: vlans.csv
      register: vlans
      delegate_to: localhost
    - name: Display CSV Data
      debug:
        var: vlans.dict
    - name: Iterate over CSV data
      debug:
        msg: "this is your ID:{{ item.0 }} , and this is the name:{{ item.1 }}"
      with_items: "{{ vlans.dict }}"

```

the ansible.cfg is empty.  
and I am using the command like this

```auto
ansible-playbook -i hosts.ini ./test.yml

```

The output is like this:

```auto
PLAY [create list from csv] ***************************************************************************************************************************************

TASK [Read CSV] ***************************************************************************************************************************************************
fatal: [localhost]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: connect to host 127.0.0.1 port 22: Connection refused", "unreachable": true}

PLAY RECAP ********************************************************************************************************************************************************
localhost : ok=0 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0   

```

host.ini contents:  
localhost ansible\_host=127.0.0.1

and insights are appreciated…

---

<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 8, 2024, 7:41pm UTC](https://forum.ansible.com/t/needs-some-explaining-dont-understand-why-ansible-is-trying-to-ssh-to-host/3765/2 "2024-02-08T19:41:08Z")

</div>

You need to set `ansible_connection` to `local` see [the documentation here](https://docs.ansible.com/ansible/latest/inventory_guide/connection_details.html#running-against-localhost), add it to your inventory, I use [a YAML inventory for cases this](https://git.coop/webarch/localhost/-/blob/main/hosts.yml?ref_type=heads):

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

```

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [February 8, 2024, 7:43pm UTC](https://forum.ansible.com/t/needs-some-explaining-dont-understand-why-ansible-is-trying-to-ssh-to-host/3765/3 "2024-02-08T19:43:09Z")

</div>

I guess `hosts.ini` contains an explicit definition for `localhost` without setting `ansible_connection` to `local`.

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [February 8, 2024, 7:44pm UTC](https://forum.ansible.com/t/needs-some-explaining-dont-understand-why-ansible-is-trying-to-ssh-to-host/3765/4 "2024-02-08T19:44:25Z")

</div>

> [@Dragao](#):
>
> host.ini contents:  
> localhost ansible\_host=127.0.0.1

Ah, I only saw that now - yes, that’s the reason. You’re telling ansible that `localhost` is a host ansible should connect to by SSHing to `127.0.0.1`. So ansible does that for you 🙂

Either remove `localhost` from `hosts.ini`, or set `ansible_collection=local`, like this:

```ini
localhost ansible_host=127.0.0.1 ansible_connection=local

```

---

<div class="post-metadata">

**Author:** ![Dragao](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@Dragao](https://forum.ansible.com/u/Dragao)\
**Post date:** [February 9, 2024, 7:36am UTC](https://forum.ansible.com/t/needs-some-explaining-dont-understand-why-ansible-is-trying-to-ssh-to-host/3765/5 "2024-02-09T07:36:57Z")

</div>

Ah, I see.

Thanks everyone for explaining!

---

<div class="post-metadata">

**Author:** ![akira6592](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akira6592/32/222_2.png) [@akira6592](https://forum.ansible.com/u/akira6592)\
**Post date:** [February 10, 2024, 2:08am UTC](https://forum.ansible.com/t/needs-some-explaining-dont-understand-why-ansible-is-trying-to-ssh-to-host/3765/6 "2024-02-10T02:08:36Z")

</div>

FYI, if localhost is not defined in the inventory, [Implicit ‘localhost’](https://docs.ansible.com/ansible/latest/inventory/implicit_localhost.html#implicit-localhost) is used.  
In this case also, no ssh connection is made to localhost.
