Install and Setup Ansible on Ubuntu 20.04. Ansible is a configuration management tool which offers an architecture that doesn’t require special software to be installed on nodes, using SSH to execute the automation tasks and YAML files to define provisioning details.
In this guide you are going to learn how to install and configure Ansible on a Ubuntu 20.04 server.
This setup is tested on Google Cloud Platform, so it will work fine on any cloud service providers or VPS or any dedicated servers.
Prerequisites
Root access to your server or user access with sudo privileges
Step 1: Initial server setup
Start by updating the packages to the latest version available.
sudo apt update sudo apt upgrade
Step 2: Install Ansible
Once all packages are updated you can proceed to install Ansible.
sudo apt install ansible
Once the installation is completed you can check the Ansible version installed using the following command.
ansible --version
You will get an output similar to the one below.
Output
ansible 2.9.6
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.8.2 (default, Jul 16 2020, 14:00:26) [GCC 9.3.0]
Step 3: Setup Inventory File
The inventory file contains all information about the hosts that you will manage with Ansible.
To configure inventory file you can open the /etc/ansible/hosts
file and adjust the configurations.
sudo nano /etc/ansible/hosts file
Here you can configure multiple groups and their own variables.
Provided a sample configuration of remote server connection details.
[google_cloud] gcp_instance_1 ansible_host=EXTERNAL_IP [google_cloud:vars] ansible_ssh_user=username ansible_ssh_private_key_file=path_to_private-key [aws] aws_instance_1 ansible_host=EXTERNAL_IP [aws:vars] ansible_ssh_user=username ansible_ssh_private_key_file=path_to_private-key-or-pem-key [all:vars] ansible_python_interpreter=/usr/bin/python3
Here we have created two groups google_cloud
and aws
and there own corresponding variables with SSH username and the private keys.
You can check the inventory using the following command.
ansible-inventory --list -y
You will receive and output similar to the one below with the respective variables in the corresponding group.
Output
all:
children:
google_cloud:
hosts:
gcp_instance_1:
ansible_host: EXTERNAL_IP
ansible_python_interpreter: /usr/bin/python3
ansible_ssh_user=username
ansible_ssh_private_key_file=path_to_private-key
aws:
hosts:
aws_instance_1:
ansible_host: EXTERNAL_IP
ansible_python_interpreter: /usr/bin/python3
ansible_ssh_user=username
ansible_ssh_private_key_file=path_to_private-key-or-pem-key
ungrouped: {}
You need to add the public key to the remote host and configure the private key to make a successful connection.
Once you have configured all required details you can proceed to test the connection.
Step 4: Test Connection
Now Ansible should be able to connect to the servers listed in the inventory file using SSH.
To check connection on all servers you can use the following command.
ansible all -m ping
To check connection on a specific group you can use this command
ansible google_cloud -m ping
The ping module will test
- if hosts are accessible;
- if you have valid SSH credentials;
- if hosts are able to run Ansible modules using Python.
You will get an output similar to the one below.
Output
gcp_instance_1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
If the configuration is not made properly you will get this message.
Output gcp_instance_1 | UNREACHABLE! => { "changed": false, "msg": "Failed to connect to the host via ssh: [email protected]: Permission denied (publickey).", "unreachable": true }
If you have any connection problems you need to provide proper SSH credentials and make sure you can connect using that credentials.
Conclusion
Now you have learned how to install and setup Ansible and connect to remote servers.
Thanks for your time. If you face any problem or any feedback, please leave a comment below.