Skip to Content

Ansible – Local Playbook Execution (Complete Guide)

Ansible – Local Playbook Execution (Complete Guide)

There are several ways to run Ansible on a local system that I could find whilst searching the net. In this guide, 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 connection: to 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.

[alert-announce]

playbook.yml

  1. – name: run the playbook tasks on the localhost
  2. hosts: 127.0.0.1
  3. connection: local
  4. become: yes
  5. tasks:
  6. – name: print out the hostname of target
  7. command: hostname
  8. – name: ensure aptitude is installed
  9. command: apt-get -y install aptitude
  10. – name: update the apt package index i.e. apt-get update
  11. apt: update_cache=yes
  12. – name: upgrade system packages i.e. apt-get upgrade
  13. apt: upgrade=yes

[/alert-announce]

Run it as usual like any standard playbook – inclusive of -K as it’ll need sudo privileges to complete.

[alert-announce]

  1. $ ansible-playbook -K playbook.yml

[/alert-announce]

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 ansible.cfg file.

[alert-announce]

  1. $ vim ansible.cfg

[/alert-announce]

Add these contents to the file as they’re shown:

[alert-announce]

ansible.cfg

  1. [defaults]
  2. hostfile = hosts

[/alert-announce]

Save and exit the new file.

Then create another file, this time the custom hosts one.

[alert-announce]

  1. $ vim hosts

[/alert-announce]

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.

[alert-announce]

hosts

  1. [local]
  2. localhost ansible_connection=local

[/alert-announce]

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.

[alert-announce]

playbook.yml

  1. – name: run the playbook tasks on the localhost
  2. hosts: localhost
  3. become: yes
  4. tasks:
  5. – name: print out the hostname of target
  6. command: hostname
  7. – name: ensure aptitude is installed
  8. command: apt-get -y install aptitude
  9. – name: update the apt package index i.e. apt-get update
  10. apt: update_cache=yes
  11. – name: upgrade system packages i.e. apt-get upgrade
  12. apt: upgrade=yes

[/alert-announce]

You’ll need to provide your sudo password with this again, to run the playbook.

[alert-announce]

  1. $ ansible-playbook -K playbook.yml

[/alert-announce]

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 /etc/ansible/hosts file.

These would be the commands:

[alert-announce]

  1. $ sudo vim /etc/ansible/hosts

[/alert-announce]

Append this new host group to the file.

[alert-announce]

/etc/ansible/hosts

  1. [local]
  2. localhost ansible_connection=local

[/alert-announce]

Write your opening lines of playbooks with the hosts: all definition. Here’s the example from before, adapted to this:

[alert-announce]

/etc/ansible/hosts

  1. – name: run the playbook tasks on the localhost
  2. hosts: all
  3. become: yes
  4. tasks:
  5. – name: print out the hostname of target
  6. command: hostname
  7. – name: ensure aptitude is installed
  8. command: apt-get -y install aptitude
  9. – name: update the apt package index i.e. apt-get update
  10. apt: update_cache=yes
  11. – name: upgrade system packages i.e. apt-get upgrade
  12. apt: upgrade=yes

[/alert-announce]

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.

[alert-announce]

  1. $ ansible-playbook -K -l localhost playbook.yml

[/alert-announce]

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.

Read More On Ansible For Developers

Now that you’ve learned how to run Ansible playbooks locally, what’s next? Here are some additional resources.

Docker For Developers

We have a number of resources for folks looking to learn how to utilize docker for app development, take a look at the free resources:

More From TricksOfTheTrades

Here are some of the posts in the same category with running ansible playbooks locally people are reading:

Trending On DroidRant

A list of recently published articles on the main site people are also reading: