In the realm of IT automation, Ansible shines as a radiant star, illuminating the path to seamless and efficient management of the infrastructure. This journey begins with two enchanting spells: Ad-Hoc Commands & Ansible Playbook
Prerequisites:
Two Servers (In this case I have created two severs on AWS both Ubuntu, free tier
and named them as
Ansible_server
target_server (the host)
On the Ansible_server
Connect to the server using "ssh -i keypair.pem ubuntu@publicipaddress"
I was able to connect to the ansible_server.
Next step update using sudo apt update
.
The next step would be Installing ansible by sudo apt install ansible
.
Next to check if we have successfully installed Ansible go with " ansible --version
."
Now establishing password-less communication with the target_server we can achieve this by doing ssh-keygen
.
This act will create a bunch of files like authorized_keys
, and private, and public keys in a certain location. (/home/unbuntu/.ssh)
Navigate to the location of this public key and open the id_rsa.pub key using cat id_rsa.pub
On the target_server
Take a new terminal and Connect to the server using "ssh -i keypair.pem ubuntu@publicipaddress". The same way we did on the other server.
Next step update using sudo apt update
. The same way we did on the other server.
Then ssh-keygen
which will create a bunch of files like authorized_keys
, and private, and public keys in a certain location. (/home/unbuntu/.ssh). The same way we did on the other server.
Now Navigate to the (/home/unbuntu/.ssh) location and open the authorized_keys
and place the public key id_rsa.pub
of the ansible_server using
"vim /home/unbuntu/.ssh/authorized_keys". Then save the file.
Now let's check the connection, hop on to the ansible_server terminal and "ssh privateipaddress". (Why private ip address because both the instances are on the same VPC).
Now we have successfully been able to establish a password-less connection between both the ansible_server and target_server.
Now log out of the target_server.
The next thing is to create an "inventory file" using vim editor. In this inventory file, include the private IP of the target_server and save the file.
Now after creating an inventory file and adding the private ip address of the target_server, its time to create a file.
Ad-hoc Commands | Ansible-playbook |
Ad-hoc commands are used when we want to perform single tasks. For example, if we want to create a file, we use Ad-hoc commands | Ansible playbooks are used when we want to perform a set/series of tasks. For example, if we want to install nginx and start nginx, then we will need to create a playbook as this task comprises two tasks. |
First Ansible Ad-Hoc Command:
With ansible -i inventory -m "shell" -a "touch ansiblefile1"
command we can create a file called "ansiblefile1" on the target_server, where "-m
" corresponds to the module and the -a
flag corresponds to the "arguments" or "parameters" that you want to pass to the module specified by the -m
flag.
- Now hop on to the target_server terminal and check for the existence of "ansiblefile1" with
ls -ltr
. This command will reveal the creation of the file with the exact timestamp.
Hence, we have established a connection between ansible_server and target_server by a password-less authentication method and can create a file on ansible_server and deploy it on the target_server.
Installing Nginx and starting Nginx:
- For this task let us create a playbook using
vim first-playbook.yml
.
Then include the following script. Ansible playbooks are written in "YAML" language, which is a widely used language by DevOps Engineers/AWS Engineers.
---
- name: Install Nginx
hosts: all
become: true
tasks:
- name: Install Nginx
apt:
name: nginx
state: present
- name: Start Nginx
service:
name: nginx
state: started
Save the file and run the following command ansible-playbook -i inventory first-playbook.yml
.
This shows that Nginx has successfully installed and it is up and running on the target_server.
To check, Now hop on to the target_server and check using sudo systemctl status nginx
.
This should display the Nginx server installed on the targer_server and the status as up and running.