How to install Ansible on Apple MacOS X using command line

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

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:


Save and close the file. Make sure you install the ssh keys in box1 and 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/ box1
$ ssh-copy-id -i ~/.ssh/

Test the connectivity:
$ ansible all --inventory-file=~/hosts -m ping
Sample outputs: | 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: | 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: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.