Ansible Installation & Configuration
Last Updated: December 5, 2022
Ansible is an open-source tool that can be used to automate your server setup. You might currently have bash scripts to semi-automate some setup tasks for your servers, but Ansible provides a more convenient and efficient way of doing all this.
Installing Ansible
Firstly, you'll need to install Ansible on your "control machine". In this case, your local machine will work fine.
Ensure you have the pip
package manager for Python:
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py
If you get the following error xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun
then ensure that you have the Command Line Tools package installed:
xcode-select --install
Now you can install Ansible with pip
:
pip3 install ansible
Confirm Ansible has been installed by running:
ansible version
In my case, it seems that Ansible was installed, but wasn't on the path.
To fix this, check the installation directory. In my case this was /Users/bradsi/Library/Python/3.9/bin/ansible
Run the following command to add Ansible to your path:
export PATH=$PATH:/Users/bradsi/Library/Python/3.9/bin
Note the above command will add the whole bin
directory to your PATH. This is better as it will cover everything you install at a later date as well.
Inventory Configuration
Now that you have Ansible installed, you'll want to configure an inventory. An inventory is basically a list of the servers that you want to manage through Ansible.
Before we get into this, I'm going to create 3 Droplets on Digital Ocean
Before using Ansible on these servers, we'll need to manually SSH into each server first to validate the ECDSA key fingerprint is valid.
Create a hosts
file and add your IP:
sudo mkdir /etc/ansible
sudo touch /etc/ansible/hosts
Edit your hosts
file to include the servers that you just created:
sudo nano /etc/ansible/hosts
# /etc/ansible/hosts
[production]
142.93.37.70
165.227.226.88
134.209.24.142
Verify that the hosts have been added correctly:
ansible all --list-hosts
If you haven't made any changes to your new servers then you'll only have the root
user setup. We'll need to pass the -u
flag to ansible and specify root
as the username. Otherwise, Ansible will try to connect with your local username:
ansible all -u root -m ping
You should get a response like the below. This means that Ansible has been able to connect to all your servers.