Skip to content

Instantly share code, notes, and snippets.

@ryantuck
Last active April 3, 2025 00:46
Show Gist options
  • Save ryantuck/9771990cfdf16b016929 to your computer and use it in GitHub Desktop.
Save ryantuck/9771990cfdf16b016929 to your computer and use it in GitHub Desktop.
super fast way to start testing ansible stuff locally without VMs

set up ansible to work on localhost

i've found this useful for debugging ansible modules and syntax without having to use VMs or test in dev environments.

install ansible

pip install ansible

make some relevant config files

~/.ansible.cfg:

[defaults]
hostfile = ~/.ansible-hosts

~/.ansible-hosts:

localhost ansible_connection=local

make a test playbook and run!

helloworld.yml:

---

- hosts: all
  tasks:
    - shell: echo 'hello world'

run!

$ ansible-playbook helloworld.yml

PLAY [all] ********************************************************************

GATHERING FACTS ***************************************************************
ok: [localhost]

TASK: [shell echo 'hello world'] **********************************************
changed: [localhost]

PLAY RECAP ********************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0
@coffeegist
Copy link

For future users that stumble on this, the hostfile variable in .ansible.cfg was changed to inventory after 1.9, so that file should be:

~/.ansible.cfg

[defaults]
inventory = ~/.ansible-hosts

@bleaktradition
Copy link

If you don't want to add or change your config file, consider this way

  1. Install ansible
  2. Add Playbook
---
- hosts: localhost
  connection: local
  tasks:
    - shell: echo 'hello world'
  1. Run the Playbook

This way some warnings will be thrown about implicit localhost and an empty hosts list, but it works just fine for me.

@ramirez368
Copy link

very nice @bleaktradition thank you

@huyong1979
Copy link

I am using ansible v-2.16.3. echo seems not working for me: I do not see TASK: [shell echo 'hello world']. Instead, this is my output: TASK [shell] *********

@Abdullah8006
Copy link

I am using ansible v-2.16.3. echo seems not working for me: I do not see TASK: [shell echo 'hello world']. Instead, this is my output: TASK [shell] *********

The following works for me in v-2.16.3

- hosts: localhost
  connection: local
  tasks:
   - name: Print message
     ansible.builtin.debug:
       msg: Hello world

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment