There are several ways to run Ansible on a local system that I could find whilst searching the net. In this I’m covering three of the ones that were the most popular or the most prevalent. The first two seem great for their different contexts, and the third is not as necessary but worth considering perhaps.
Being able to do this can be very useful for setting up your own machines or workstations (dotfiles anyone?) at least in the event that you don’t want to use traditional scripting. Also when creating playbooks that involve interacting with developer API’s this is an important component – see the “more information” section at the end, for a link to an example of this.
1 – Local Play Directives
This is the easiest way I found and probably most suited for when writing one or two individual playbooks.
Simply put, using both
127.0.0.1 for the
hosts: directive and setting
local in a playbook ensures any tasks carried out are executed on your local machine.
This is an example playbook that prints “localhost” during execution to show local playback, then updates and upgrades Apt system packages; so it’s intended for Debian and or Ubuntu.
Run it as usual like any standard playbook – inclusive of
-K as it’ll need
sudo privileges to complete.
2 – Repository Config and Hosts File
This second method works best in the context of a version control repository, which features multiple local playbook files and is intended to be passed around from person to person or host to host. It works by forcing Ansible to use a custom config file and in turn local hosts file.
Here are the step you’d need to carry out in order to set this up in a Git repository, after setting up the repo itself. There’s also no commands for checking in, writing and pushing files to the remote.
In the Git repository, create the custom
Add these contents to the file as they’re shown:
Save and exit the new file.
Then create another file, this time the custom
The contents here consist of a group named
[local] and a host entry listed as
localhost. The host variable for local host
ansible_connection=local as expected forces a local connection whenever it is targeted in a playbook.
Again on a Debian/Ubuntu host you could use an adapted version of the earlier playbook as a test example, to ensure everything is working as intended. The difference here is the
localhost value for
hosts: and no requirement to mention the
connection: local directive.
You’ll need to provide your
sudo password with this again, to run the playbook.
3 – Global Inventory Hosts Group
One further alternative solution (in a non-version control scenario where there’s no need for portability) is to instead add the
[local] host group to your global
These would be the commands:
Append this new host group to the file.
Write your opening lines of playbooks with the
hosts: all definition. Here’s the example from before, adapted to this:
Then afterwards when you want to run a playbook locally, use the
-l switch and provide the
local group or
localhost as the target host.
It’s still wise and maybe more convenient to keep local playbook execution isolated to one directory or Git repository however (using the method in the former step). I would probably not recommend this method over the other two, but whatever works best for your particular needs I guess.
Thanks for reading, and I hope this has helped you out with local playbook execution in some way or another.