How to install Ansible on MacOS using CLI

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. This page explains how to install Ansible on your MacBook Pro/Air/Mini powered by macOS operating systems.

Install Ansible on MacOS using brew command

Open the terminal app and type the following commands. Make sure, you install Homebrew on macOS and then type the following brew command to grab Ansible:
$ brew install ansible

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

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

Your Macbook pro/Mac computer now act as a primary node or control system for your servers. Let us print or check Ansible version on macOS:
ansible --version

Installing Ansbile on macOS using pip method

First install Xcode if not installed on your macOS:
xcode-select --install
Then all you have to do is type the following commands:
sudo easy_install pip
sudo pip install ansible --quiet

Then, we can upgrade Ansible when a new version released by typing the following command:
sudo pip install ansible --upgrade

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 primary node (MacOS X desktop/laptop):
$ 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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
1 comment… add one
  • Jeff Feb 19, 2017 @ 8:39

    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 Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum