Table of contents
- What is Ansible?
- How Ansible Works?
- Ansible Architecture: Nodes and Modules
- Create 3 Instances on AWS EC2 with the following names:
- <mark>Installation of Ansible on AWS EC2 (Master Node)</mark>
- <mark>Create SSH key on Master Node.</mark>
- What is a Hosts file?
- Where is the Hosts file located?
- <mark>Now we will ping the Node server using Master Node by pasting the public SSH key of Master Node to the Node server.</mark>
- Create 3 Instances on AWS EC2 with the following names:
- <mark>Installation of Ansible on AWS EC2 (Master Node)</mark>
- <mark>Create SSH key on Master Node.</mark>
- What is a Hosts file?
- Where is the Hosts file located?
- <mark>Now we will ping the Node server using Master Node by pasting the public SSH key of Master Node to the Node server.</mark>
- Task-01
- <mark>Write an </mark> <mark>ansible ad hoc ping command to ping 2 servers from the inventory file.</mark>
- <mark>Write an ansible ad hoc command to check uptime.</mark>
- <mark>Now we will use the ansible ad-hoc command for checking the version of Python installed on Node_1 and Node_2.</mark>
- Happy Learning :)
What is Ansible?
Ansible is a software tool that provides simple but
powerful automation
for cross-platform computer support. It is primarily intended for IT professionals, who use it for application deployment, updates on workstations and servers, cloud provisioning, configuration management, intra-service orchestration, and nearly anything a systems administrator does on a weekly or daily basis.Ansible doesn't depend on agent software and has
no additional security infrastructure
, so it's easy to deploy.
How Ansible Works?
Ansible works by
connecting to nodes
(clients, servers, or whatever you'reconfiguring
) on a network, and then sending a small program called anAnsible module
to that node. Ansible executes these modules over SSH and removes them when finished.SSH keys are the most common way to provide access, but other forms of
authentication
are also supported.
Ansible Architecture: Nodes and Modules
Ansible's Architecture is based on the concept of a control node and a managed node. The platform is executed from the control node where a user runs the
ansible-playbook
command. There must be at least onecontrol node
; a backup control node can also exist. The devices being automated and managed by the control node are known as managed nodes.Ansible automates
Linux
andWindows
by connecting to managed nodes and pushing out small programs calledAnsible modules
. Ansible executes thesemodules
, which are the resource models of the desired system state, over Secure Socket Shell (SSH) by default andremoves
them when finished.Ansible
modules are written in Python and can be written in any language.Ansible
modules are reusable, standalone scripts that can be used by the Ansible API, Ansible Playbooks, or Ansible Galaxy.
For a more comprehensive version of this blog post, please refer to the previous entry where you can find a thorough and hands-on rephrasing of the content:
Create 3 Instances on AWS EC2
with the following names:
Ansible_Master_Server
Node_1
Node_2
Installation of Ansible on AWS EC2 (Master Node)
#!/bin/bash
sudo apt-add-repository ppa:ansible/ansible
sudo apt update
sudo apt install ansible
sudo chmod 777 install.sh
sudo ./install.sh
ansible --version
Create SSH key on Master Node.
cd ~/.ssh
ssh-keygen
What is a Hosts file?
- In the context of Ansible, a
host file
(also known as an inventory file) isconfiguration file
used to define and organize the list of target hosts that Ansible should manage.
Where is the Hosts file located?
Ansible uses this file to map target hosts to managed nodes. The host file is usually located in
/etc/ansible/hosts
.So, open the host file and add the IP addresses of the Nodes:
sudo vim /etc/ansible/hosts
[servers]
Node_1 ansible_host= <Public IP-Adddress of Node-1>
Node_2 ansible_host= <Public IP-Adddress of Node-2>
[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_private_key_file=/home/ubuntu/.ssh/id_rsa
Now we will ping the Node server using Master Node by pasting the public SSH key of Master Node to the Node server.
# Open Node_1
cd /home/ubuntu/.ssh
vim authorized_keys
# Open Node_2
cd /home/ubuntu/.ssh
vim authorized_keys
Task-01
Write an ansible ad hoc ping command to ping 2 servers from the inventory file.
ansible all -m ping
# or
ansible servers -m ping
# or
ansible all -m ping -i /etc/ansible/hosts
Write an ansible ad hoc command to check uptime.
ansible: This is the command-line tool used to interact with Ansible.
all: This refers to the group of hosts or servers on which you want to execute the specified command. In this case, "all" means that the command will be executed on all the hosts specified in the inventory.
-a: This option specifies that you are providing an ad-hoc command to execute on the targeted hosts.
uptime: This is the actual command you want to run on the remote hosts. In this case, it's the "uptime" command, which provides information about how long the system has been running.
-i /etc/ansible/hosts: This option specifies the inventory file to use. The inventory file contains a list of hosts or IP addresses that Ansible will manage. In this case, the inventory file is located at "/etc/ansible/hosts".
ansible all -a uptime -i /etc/ansible/hosts
Now we will use the ansible ad-hoc command for checking the version of Python installed on Node_1 and Node_2.
ansible all -b -m shell -a 'sudo python3 --version