ansibleansible-inventory

Run Ansible playbook without inventory


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?


Solution

  • 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.