# How to allow a playbook with \`hosts: all\` to be executed on localhost without \`connection: local\`?

**URL:** <https://forum.ansible.com/t/how-to-allow-a-playbook-with-hosts-all-to-be-executed-on-localhost-without-connection-local/6318>\
**Category:** Get Help\
**Tags:** ansible, ubuntu\
**Created:** [June 12, 2024, 1:33pm UTC](https://forum.ansible.com/t/how-to-allow-a-playbook-with-hosts-all-to-be-executed-on-localhost-without-connection-local/6318 "2024-06-12T13:33:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShanDesai](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@ShanDesai](https://forum.ansible.com/u/ShanDesai)\
**Post date:** [June 12, 2024, 1:33pm UTC](https://forum.ansible.com/t/how-to-allow-a-playbook-with-hosts-all-to-be-executed-on-localhost-without-connection-local/6318/1 "2024-06-12T13:33:30Z")

</div>

## System Description

I am building a custom QEMU image (Ubuntu 24.04 LTS) which has ansible and ansible-runner.

I have a testing logic where I use `pytest` and I use fixtures as small runner projects that I wish to execute in order to conduct tests.

I have two use-cases:

- Single Instance (`localhost`)
- Multiple Devices

My test playbooks need to be diverse in the sense that they need to be executed on a single host (`localhost`) as well as multiple devices.

For that reason, I have `hosts: all` and **no** `connection: local`. During my test fixtures I create an inventory e.g.`inventory/hosts` file and the content for single device is `localhost`.

However, I keep getting the error:

```bash
failed to connect to the host via SSH: admin@localhost: Permission Denied (publickey, password).'

```

What would be an optimal way to make my testing playbook more versatile in order to use it for localhost and multiple devices without changing the playbook content.

---

<div class="post-metadata">

**Author:** ![ShanDesai](https://avatars.discourse-cdn.com/v4/letter/s/838e76/32.png) [@ShanDesai](https://forum.ansible.com/u/ShanDesai)\
**Post date:** [June 12, 2024, 1:39pm UTC](https://forum.ansible.com/t/how-to-allow-a-playbook-with-hosts-all-to-be-executed-on-localhost-without-connection-local/6318/2 "2024-06-12T13:39:21Z")

</div>

One possible solution I have found which seems more of a hack is to generate a Key pair

```bash
ssh-keygen -t rsa

```

```bash
cat ~/.ssh/id_rsa.pub > ~/.ssh/authorized_keys

```

and then the playbook seems to work on localhost

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [June 12, 2024, 1:52pm UTC](https://forum.ansible.com/t/how-to-allow-a-playbook-with-hosts-all-to-be-executed-on-localhost-without-connection-local/6318/3 "2024-06-12T13:52:20Z")

</div>

If you’re using [implicit localhost](https://docs.ansible.com/ansible/latest/inventory/implicit_localhost.html), `connection: local` is implied, and it is not targetable as part of any group, so you’d need to include it in `hosts`.

```yaml
- hosts: localhost,all
  tasks:
    - ping:

```

Then to execute the play against localhost or all, `--limit localhost|all`.

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [June 12, 2024, 4:53pm UTC](https://forum.ansible.com/t/how-to-allow-a-playbook-with-hosts-all-to-be-executed-on-localhost-without-connection-local/6318/4 "2024-06-12T16:53:11Z")

</div>

> [@ShanDesai](#):
>
> During my test fixtures I create an inventory e.g.`inventory/hosts` file and the content for single device is `localhost`

For an explicit localhost you should configure it in your inventory to use the connection plugin that you want. Assuming you’re using the INI format, that would be `localhost ansible_connection=local`.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 12, 2024, 4:53pm UTC](https://forum.ansible.com/t/how-to-allow-a-playbook-with-hosts-all-to-be-executed-on-localhost-without-connection-local/6318/5 "2024-07-12T16:53:15Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
