how to run local command via ansible-playbook


I am trying to run some local command, iterating over inventory file and taking each hostname as an argument to the local command.

Eg: I wanted to run a command “knife node create {{ hostname }}” in my local machine(laptop). The playbook is:

and my inventory file looks like:

Ofcourse, it wont work as the inventory has ‘qa-hosts’ and the play is for ‘’, as I wanted the play to run from my local machine.

Would anyone help me with an idea how to get it done. Basically, I want get the variable ‘hostname’ and pass it to above play block.


You could access the hostname by using the following play, as the inventory information are available as hostvars.

Leave a Reply