Introduction
Prerequisites
- Ubuntu 20.04, 22.04, or 24.04 installed
- Regular user with sudo rights
- 2 CPUs / vCPUs
- 2 GB RAM or more
- 20 GB Hard drive
- Internet Connection (In case you don’t have a local configured apt repository server)
Installation Steps
Step 1: Update the System
apt
commands to install all available updates:$ sudo apt update
$ sudo apt upgrade -y
Once all the updates are installed, reboot the system:
$ sudo reboot
Step 2: Add Ansible PPA Repository
$ sudo apt install -y software-properties-common
$ sudo add-apt-repository --yes --update ppa:ansible/ansible
Now, update the repository package index by running the following apt
command:
$ sudo apt update
Step 3: Install Ansible
apt
command:$ sudo apt install -y ansible
Step 4: Verify Installation
Once the installation is complete, you can verify that Ansible is installed correctly by checking its version:
$ ansible --version
You should see the installed version of Ansible displayed on your terminal.
Configure SSH Keys
sysops
user. Generate the SSH keys for the sysops
user from the control node and share it among managed hosts. Run the ssh-keygen
command:$ ssh-keygen -t rsa
Hit enter when prompting for the input.
Note: Add managed host entries in /etc/hosts
file on the control node. This is only required when you don’t have a local DNS server configured.
192.168.1.200 node1.example.com
192.168.1.77 node2.example.com
To share the SSH keys between the control node and managed hosts, run the ssh-copy-id
command:
$ ssh-copy-id node1.example.com
$ ssh-copy-id node2.example.com
Create Ansible Configuration and Inventory Files
ansible.cfg
and inventory
files for each project. For the demonstration purpose, I am going to use demo
as the project name. So, create the project folder first by running mkdir
command:$ mkdir demo && cd demo
Next, generate the ansible.cfg
file using the following command:
$ ansible-config init --disabled | tee ansible.cfg
Now, edit the ~/demo/ansible.cfg
file, set the following parameters:
$ vi ~/demo/ansible.cfg
Under the default
section:
inventory = /home/sysops/demo/inventory
remote_user = sysops
host_key_checking = False
Under the privilege_escalation
section:
become=True
become_method=sudo
become_user=root
become_ask_pass=False
Save and close the file.
Let’s create the inventory file that we have defined in ~/demo/ansible.cfg
file:
$ vi ~/demo/inventory
[dev]
node2.example.com
[prod]
node1.example.com
Save and exit the file.
Re-run ansible –version
command to verify that the new config file is reflected:
$ cd demo/
$ ansible --version
Great, Ansible is now reading our project’s configuration file. Let’s verify the managed nodes connectivity using ansible ad-hoc
command:
$ ansible all -m ping
Note: Make sure to run the Ansible command from the demo
folder.
Test Ansible Installation
Create a Demo Playbook
change_hostname.yaml
under the demo
folder. This playbook will change the hostname on managed nodes.$ vi change_hostname.yaml
---
- name: Playbook to Change Server Hostname
hosts:
- dev
- prod
tasks:
- name: Change hostname to new_hostname
hostname:
name: "new_hostname"
Save and close the file.
Run the Playbook
Next, run the playbook using the ansible-playbook
command:
$ ansible-playbook change_hostname.yaml
Output:
PLAY [Playbook to Change Server Hostname] *************************************
TASK [Change hostname to new_hostname] ****************************************
changed: [node2.example.com]
changed: [node1.example.com]
PLAY RECAP *******************************************************************
node1.example.com : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2.example.com : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Output above confirms that the playbook has been executed successfully. To verify the result, run the following ad-hoc commands:
$ ansible dev -m shell -a 'hostname'
$ ansible prod -m shell -a 'hostname'