Setting up a Mac using Ansible

Mathew Kenny Thomas
Tensult Blogs
Published in
4 min readApr 30, 2018

This Blog has been moved from Medium to blogs.tensult.com. All the latest content will be available there. Subscribe to our newsletter to stay updated.

In this blog entry, I want to share a step by step guide in using Ansible to set up a MacBook with the required Applications.

Using Ansible Playbooks we can easily configure the applications we want installed on our local system. The existing code can be modified so as to cater to each individual’s needs.

First, we’ll take a basic look at Ansible. Ansible is an open-source IT automation engine used for infrastructure automation. It enables us to reduce time and risk associated with making changes to the environment. We take something we want to change and put it into a script which then codifies the change we want to make. Ansible playbooks are written in YAML where key value pairs are denoted by a colon followed by a space. Ansible can work with one or multiple systems at the same time in your infrastructure. In order to work with multiple servers, Ansible needs to establish connection with the multiple servers. This is done using SSH in Linux and Powershell Remoting in Windows. This makes Ansible agentless (no need to install any additional software on target machine to work with Ansible).

Ansible Playbooks are Ansibles orchestration language where we define what we want Ansible to do. It is the set of instructions we want Ansible to carry out. It is a single YAML file which defines a set of activities or tasks to be run on the hosts. Tasks can vary from executing a command, running a script, installing a package, shutdown/restart etc. Each play contains a task. Tasks are executed in the order they are written one by one before moving on to the next. The goal of each task is to execute a module. Every task should have a name.

Ansible roles are nothing more than specific playbooks. Role allows us to take something which is common in all our playbooks and put them into a more specific playbook. The files under roles have to be in certain directory names. The ones that are not being used can be discarded. Some examples of directory names under roles are tasks, defaults, vars, files etc. You can read more about Ansible Roles here.

Getting started

Assuming you don’t have Ansible installed in your MacBook the following steps will explain how to install Ansible on your machine using Homebrew. If you have Ansible installed on your machine you can skip this part.

  1. Install Homebrew by opening the Terminal and typing in the following command. Enter password when prompted. ( Homebrew simplifies the installation of software on the Mac OS X operating system. )

/usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2. Install Git by copying the below commands into the terminal.

brew install git

3. Install Ansible by using pip (Python package manager).

a. If pip isn’t already available on your computer then you can get pip by . running the following commands in the terminal.

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

sudo python get-pip.py

b. Install Ansible with the following commands in the terminal.

sudo pip install ansible

Enter password whenever prompted.

Defining Ansible Playbooks

The approach I have taken is to define playbooks for different applications and included all these playbooks in the main playbook. Each playbook is called from the main playbook which downloads the application from the web and then executes the set of codes depending on the type of file downloaded using roles in Ansible.

Below one of the playbooks (firefox.yml) can be seen

Under vars, we specify the name of the application (app_name), the download URL(app_url), the type of file downloaded (download_file_type) and the method of installation(install_method). Under roles we specify the roles we have set up for installing the applications. In this case, I have specified one for creating the downloads directory (setup), for downloading and installing the application (installer) and one for deleting the downloaded file after installation (cleanup).

The type of files downloaded could be either “zip”, “dmg” or a “app” file. Depending on the this the required steps are carried out as follows:

  • If the downloaded file is a “zip” file, it unzips the file so that either a “app” or a “dmg” file can be obtained.
  • For “dmg” files, the role will mount the image to Volumes from where it will be copied into the Applications directory on the machine.
  • For “app” files, the role will copy the application to the Applications directory on the machine.

This can be seen in the “main” YAML file under “installer” roles.

The downloaded files are automatically deleted with the help of “cleanup” role which automatically deletes the folder after installation.

Modifying Playbooks

The above playbook can be re-configured so that you can install applications you prefer as well. This can be done by editing the below code and saving it in the “playbooks” folder.

Depending on the type of file being downloaded enter the download_file_type as “app”, “dmg” or “zip”. Depending on the file obtained after unzipping, enter the install_method as “dmg” or “app”.

After saving the new playbook into the “playbooks” folder, insert the following line into the “main_playbook” YAML file in the space provided.

- import_playbook : playbooks/<app-playbook>.yml

The GitHub link for the above Ansible automation can be found here.

--

--