How to install Ansible on Apple MacOS X using command line

Posted on in Categories , last updated February 17, 2017

How do I install the Ansible software to automate apps and IT infrastructure from my Apple Macbook pro running MacOS X?

Ansible is the simplest and easy to use software to automate apps and IT infrastructure. You can use Ansible for application deployment, configuration management, continuous delivery and many sysadmin related tasks.

Install Ansible on MacOS using brew command

Open the terminal app and type the following command:
$ brew install ansible
Sample outputs:

Fig. 01: Use brew to install ansible on MacOS X
Fig. 01: Use brew to install ansible on MacOS X

Your Macbook pro now act as a master node or control system.

Creating inventory hosts file

Ansible needs to know which servers to connect to and manage those boxes. You need to define or create Ansible’s inventory hosts file. The file contains a list, options, and groups for your servers. Its default location is /usr/local/etc/ansible/hosts on MacOS X. You can edit the file /usr/local/etc/ansible/hosts or create ~/hosts file:
$ vi ~/hosts
Here is my sample file:

box1
192.168.2.15

Save and close the file. Make sure you install the ssh keys in box1 and 192.168.2.15 servers. If you don’t have the ssh key on your MacOS X, generate the ssh key on the master node (MacOS X):
$ ssh-keygen -t rsa -C "OS X laptop ssh key"
Install the ssh key:
$ ssh-copy-id -i ~/.ssh/id_rsa.pub box1
$ ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.2.15

Test the connectivity:
$ ansible all --inventory-file=~/hosts -m ping
Sample outputs:

192.168.2.15 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
box1 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

You can set ANSIBLE_HOSTS to define your ~/hosts file:
$ export ANSIBLE_HOSTS=~/hosts
$ echo 'export ANSIBLE_HOSTS=~/hosts' >> ~/.bashrc
$ ansible all -m ping
## let us run uptime command all hosts ##
$ ansible all -a 'uptime'

Sample outputs:

192.168.2.15 | SUCCESS | rc=0 >>
 01:20:36 up 10:06,  2 users,  load average: 0.47, 0.46, 0.20

box1 | SUCCESS | rc=0 >>
 19:50:41 up 58 min,  1 user,  load average: 0.00, 0.00, 0.00

What next?

To manage your server you need to Ansible’s playbooks. See the following resources for more info:

  1. Linux Tutorial: Install Ansible Configuration Management And IT Automation Tool
  2. Ansible Documentation

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

1 comment

  1. Nixcraft is great, and homebrew is totally the way to go, but if you’re doing a write-up show them a little link love!

    Brew isn’t a native OS X command, but it is the best way to handle linux packages on a Mac, in my opinion. Mac Ports is another viable alternative, but I’ve found brew has better/more package support.

    https://brew.sh/

Leave a Comment