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.

ADVERTISEMENTS

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
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig 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 VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
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>, <code>...</code> and <kbd>...</kbd> for code samples.