Skip to content

Instantly share code, notes, and snippets.

@huyong1979
Forked from ryantuck/ansible-localhost.md
Last active April 5, 2025 12:40
Show Gist options
  • Save huyong1979/a61c1dd72f6a036203cde8506d656cf5 to your computer and use it in GitHub Desktop.
Save huyong1979/a61c1dd72f6a036203cde8506d656cf5 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]
inventory = ~/.ansible-hosts

Note: used to be hostfile = ~/.ansible-hosts

~/.ansible-hosts:

localhost ansible_connection=local

make a test playbook and run!

helloworld.yml:

---

- hosts: localhost
  connection: local
  tasks:
    - name: "Print Message"
      ansible.builtin.debug:
        msg: "Hello World"

Old code: helloworld.yml:

---

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

run!

$ ansible-playbook helloworld.yml

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

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

TASK [Print Message] **********************************************************************************************
ok: [localhost] => {
    "msg": "Hello World"
}

PLAY RECAP ********************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment