C.J. Scarlett

Ansible - Local Playbook Execution

Ansible Logo

Preamble

There are a 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 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.

playbook.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
---

- name: run the playbook tasks on the localhost
hosts: 127.0.0.1
connection: local
become: yes
tasks:

- name: print out the hostname of target
command: hostname

- name: ensure aptitude is installed
command: apt-get -y install aptitude

- name: update the apt package index i.e. apt-get update
apt: update_cache=yes

- name: upgrade system packages i.e. apt-get upgrade
apt: upgrade=yes

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

1
$ ansible-playbook -K playbook.yml

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.

1
$ vim ansible.cfg

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

ansible.cfg
1
2
[defaults]
hostfile = hosts

Save and exit the new file.

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

1
$ vim hosts

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.

hosts
1
2
[local]
localhost ansible_connection=local

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.

playbook.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
---

- name: run the playbook tasks on the localhost
hosts: localhost
become: yes
tasks:

- name: print out the hostname of target
command: hostname

- name: ensure aptitude is installed
command: apt-get -y install aptitude

- name: update the apt package index i.e. apt-get update
apt: update_cache=yes

- name: upgrade system packages i.e. apt-get upgrade
apt: upgrade=yes

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

1
$ ansible-playbook -K playbook.yml

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:

1
$ sudo vim /etc/ansible/hosts

Append this new host group to the file.

/etc/ansible/hosts
1
2
[local]
localhost ansible_connection=local

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

playbook.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
---

- name: run the playbook tasks on the localhost
hosts: all
become: yes
tasks:

- name: print out the hostname of target
command: hostname

- name: ensure aptitude is installed
command: apt-get -y install aptitude

- name: update the apt package index i.e. apt-get update
apt: update_cache=yes

- name: upgrade system packages i.e. apt-get upgrade
apt: upgrade=yes

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.

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

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.


More Information

Easily deploy an SSD cloud server on Digital Ocean in 55 seconds. Sign up using my link and receive $10.00 in free credit: https://www.digitalocean.com/?refcode=e91058dbfc7b

– Scarlz: @5car1z