33

Consider if I want to check something quickly. Something that doesn't really need connecting to a host (to check how ansible itself works, like, including of handlers or something). Or localhost will do. I'd probably give up on this, but man page says:

-i PATH, --inventory=PATH

The PATH to the inventory, which defaults to /etc/ansible/hosts. Alternatively, you can use a comma-separated list of hosts or a single host with a trailing comma host,.

And when I run ansible-playbook without inventory, it says:

[WARNING]: provided hosts list is empty, only localhost is available

Is there an easy way to run playbook against no host, or probably localhost?

Cadoiz
  • 1,446
  • 21
  • 31
x-yuri
  • 16,722
  • 15
  • 114
  • 161
  • I use `echo 'localhost' > hosts.ini`, because my real inventories are partitioned in production, reference, testing and development. – ceving Jan 26 '17 at 09:49

2 Answers2

44

Prerequisites. You need to have ssh server running on the host (ssh localhost should let you in).

Then if you want to use password authentication (do note the trailing comma):

$ ansible-playbook playbook.yml -i localhost, -k

In this case you also need sshpass.

In case of public key authentication:

$ ansible-playbook playbook.yml -i localhost,

And the test playbook, to get you started:

- hosts: all
  tasks:
    - debug: msg=test

You need to have a comma in the localhost, option argument, because otherwise it would be treated as a path to an inventory. The inventory plugin responsible for parsing the value can be found here.

x-yuri
  • 16,722
  • 15
  • 114
  • 161
  • -k asks for a password (I don't know why that's part of the answer). in any case `-i localhost` doesn't work because one is supposed to pass in an inventory source, not a host name. I get: "[WARNING]: Unable to parse /Users/ekkis/dev/x/ansible-blockchain-node/localhost as an inventory source" – ekkis Aug 14 '22 at 00:15
  • 1
    @ekkis `-i localhost` isn't supposed to work, see the updated answer. – x-yuri Aug 14 '22 at 03:47
1

You can define a default inventory with only localhost

See it is explained here: https://docs.ansible.com/ansible/latest/reference_appendices/config.html#the-configuration-file

And in your playbook add use this

- hosts: all
  connection: local
  tasks:
    - debug: msg=test

It will use local connection so no SSH is required (thus it doesn't expose your machine). It might be quicker unless you need to troubleshoot a ssh issue.

Also for quicker feedback loop you can use: gather_facts: no you already know your target.

Et7f3XIV
  • 562
  • 1
  • 7
  • 20