Once we install ansible, we can start playing with it right away in the localhost. In production, we run stuff against remote servers but knowing to play with localhost makes it easy and handy when learning ansible.
Writing a hello world task
Lets first use an ansible adhoc command and then convert the same into a playbook.
ansible all -i "localhost," -c local -m shell -a 'echo hello world'
localhost | success | rc=0 >> hello world
Here, I’m using shell module to run ‘echo hello world’ in localhost. Now lets write a playbook helloworld.yml
--- - hosts: all tasks: - shell: echo "hello world"
Running the playbook
ansible-playbook -i "localhost," -c local 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
Much simpler way to run
Wouldn’t it be cool if we just have to say ansible-playbook helloworld.yml
instead ofansible-playbook -i "localhost," -c local helloworld.yml
It’s easy to do that using default inventory file /etc/ansible/hosts. Just add an entry for localhost as given below:
localhost ansible_connection=local
That’s it. Now run playbooks in localhost just by saying
$ ansible-playbook helloworld.yml
and for adhoc command run,
$ ansible all -m shell -a ‘echo hello world’