Table of contents
Prerequisites
Before you begin, ensure you have the following:
A Ubuntu server to act as the Ansible controller server.
Access to the target server(s) where you want to execute Ansible commands.
Step 1: Access the Control Server
Log in to your Ansible control server using SSH. In my case, I'm using Gcloud VMS
gcloud compute ssh Nikita2.Shinde@practice-vm-1
Step 2: Gain Root Access
Switch to the root user to have the necessary permissions for Ansible tasks.
sudo su -
a) Create a User
I'll create an ansible user, to execute my application and give sudo rights to it
you can replace user_name with the name of the user, in my case it is Ubuntu,
b) Granting a User - Sudo Privileges
switch to the user
Step 3: Configure Ansible Control Node, Host, and SSH Key Pair
a) Setting up an SSH Key pair From the Control Node
1. Enter the following command in the Ansible control node terminal:
ssh-keygen
2. Hitting Enter shows the following output:
3. The system displays the output below if you already have an SSH key pair set up. Decide whether to overwrite the existing SSH key pair.
b) Configure Remote Servers: to allow access from your control server.
log in to your host node, Paste the SSH public key from your control server into the authorized_keys file, and save the changes, to establish an SSH connection
vim .ssh/authorized_keys
Step 4: Confirm Access
To confirm that you can access the remote server, execute the following command in the controller node
Once SSH is done, The below command will return the server's IP address, confirming that you have successfully accessed it.
Step 5: Logout
Exit the remote server: CRTL + D
Step 6: Install Ansible and Python
Now we need to install Ansible on the controller node and Python on ansible_host node
The managed/host node (the machine that Ansible is managing) does not require Ansible to be installed but requires Python to run Ansible-generated Python code.
1. Make sure your system’s package index is up to date. Refresh the package index with the command:
$ sudo apt update
2. Next, install Ansible on Ubuntu with the command:
$ sudo apt install ansible
3. The installation will prompt you to press Y
to confirm, with the rest of the installation process being automated.
Once installed, the Ansible control node can manage the remote hosts.
Step 7: Setting up the Inventory File
On the Control Node, we will create ansible directory
In the Ansible Dir, we will add Ansible scripts and an inventory file
Create a File named inventory and next, we will open it in editorial mode using
vim inventory
[webservers]
10.166.169.2
10.166.168.2
[webservers:vars]
ansible_user = ubuntu
In this example, we added a remote host by using its IP address and sorted it into the [webserver]
category:
After you’ve set up the inventory file, you can always check it again by using:
$ ansible-inventory --list -y
The terminal window will display an output listing the hosts infrastructure
Step 8: Testing the Connection
The final step is making sure the Ansible control node connects to the remote hosts and runs commands.
1. To test the connection with the hosts, use the following command in the terminal on your control node:
$ ansible all -m ping -i inventory
If you’re connecting to the remote hosts for the first time, Ansible will ask you to confirm that the hosts are authentic. Once prompted, type ‘yes’ and hit Enter to confirm the authenticity.
When all the remote hosts reply with a ‘pong’ back, you are ready to start running commands through the Ansible control node
After following the steps in this guide, you’ve successfully installed Ansible on Ubuntu 20.04.
You are now ready to use Ansible to execute commands and playbooks on remote servers.
Let's consider a simple example, where we will create an ansible script to create a file in all the servers mentioned in the inventory file.
vim file.yaml
The Ansible script, written in YAML, incorporates various modules for specific configurations and employs a declarative language.
Check out Ansible Documentation to explore more about different modules
- hosts: webservers
remote_user: root
become_method: sudo
become: yes
tasks:
- name: Create a new file
file:
path: /home/ubuntu/new_file
state: touch
owner: root
group: root
mode: "0644"
become: yes
Let's execute our playbook across all servers with a single command line now
ansible-playbook -i inventory file.yaml
And voilà, your playbook has successfully been executed once you see the status 'OK'. We've now set up and configured Ansible!
If you have any doubts or questions, feel free to let me know in the comments – happy learning!