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

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:

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.

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

ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub > ~/.ssh/authorized_keys

and then the playbook seems to work on localhost

If you’re using implicit localhost, connection: local is implied, and it is not targetable as part of any group, so you’d need to include it in hosts.

- hosts: localhost,all
  tasks:
    - ping:

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

3 Likes

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.

3 Likes

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