Effortless Orchestration: Connecting EC2 Instances to an Ansible Master Server for Seamless Automation
To know more About Ansible - What is it, Its importance, functions, and more please do follow this blog.
This Article's objective is to gain familiarity with connecting servers using Ansible:
Create and launch an EC2 instance With a New Generated key Pair.
And connect to the Instance.
For Ansible Installation Process You can Go Through This 'DigitalOcean' website for reference.
Connect and Follow the Installation step in the Ansible master Server (EC2 instance) that you have just created
Link: https://www.digitalocean.com/community/tutorial_collections/how-to-install-and-configure-ansible
Copy and paste the commands into the Server for Installation
Once installed, you can check to verify by running the version command
Or can check if the Ansible Inventory file exists or not by running the "cat" command. If it shows the inventory file then you can assure that Ansible is installed successfully.
Next is to launch multiple Ansible nodes or worker servers with the same new key pair that we have created while launching our first Ansible master EC2 Instance. Here we have created 3 instances for practical purposes.
3 more Instances were created, Now If you want then change the name of the node servers
Now to add the other 3 node server IP addresses we go to the location path to etc/ansible/hosts of the Ansible master server with sudo & use nano
editor to edit the hosts' file
Note: every work is done on the Ansible master server
Make Sure to copy the IP addresses of the Node servers
Now in the hosts' file add servers - "inventory entry" or "inventory host." and add all the other 3 node servers' IP addresses with syntax - server_1 ansible_host=
, here the syntax ansible_host=
is important to mention because this allows Ansible to establish the correct connection to the target server for executing tasks and configurations.
Now in order to have connection among all the servers we need the ssh key which gets downloaded to our system while launching the Instance in the Download location of our local system
For that, we need to upload the ssh key to our Ansible master server from our local system
Now on Ansible Master Node go to the .ssh directory/folder and copy the location using pwd
command
Next, open the terminal in your download folder in your local system and paste the ssh connection command from the AWS console of Ansible master node-connect through ssh
here we edit the ssh
command to scp
to copy and add sudo to the command and edit the command with the key pair name and at the end of the command add the ssh location path that we copied from the Ansible master node
Now in your Ansible master node in .ssh directory run the command ls
to list to check if the key was uploaded or not. Here we can see here our ansible-access-key.pem been uploaded
Note: the name ansible-access-key is the key pair that we launched our instances with and you can name your key pair with any name as you want.
Now again in the ansible hosts file add another entry for the python interpreter and ansible private key with the location path that we just uploaded to the ansible master server itself.
Adding entries for the Python interpreter and Ansible private key file location in the host file ensures that Ansible can locate the necessary components for establishing connections and executing tasks on remote servers.
Now run the chmod
command to give the user the read and execute permission to the key pair
Now try to ping the node servers with the command ansible server -m ping
. Ping all the 3 servers is successful
Lets try other commands to check free disk space on all the servers
And update the node servers just by a single command
And command to show the server uptime
what is the difference between using -m
and -a
in Ansible commands:
Ansible's -m and -a are both command line parameters that are used to specify the module and action that Ansible should use. The main difference between the two is that -m specifies the module to use, while -a specifies the action to use within that module.
-m is used to specify the module that Ansible should use. Modules are Ansible's building blocks, and they provide Ansible with the ability to perform a wide variety of tasks, such as configuring systems, deploying software, and managing users. There are many different modules available, and they can be found in the Ansible Galaxy repository.
-a is used to specify the action that Ansible should use within a module. Actions are the specific tasks that Ansible can perform within a module. For example, the file module has an action called copy, which can be used to copy a file from one location to another
Example: ansible mygroup -m shell -a "echo Hello, world!"
In this case, the shell
module is executed on the hosts in the mygroup
group, and the -a
option is used to pass the command (echo Hello, world!
) as an argument to the module.
In summary, -m is used to specify the module to be executed, while -a is used to provide the arguments or parameters to that module. They work together to define the task that Ansible performs on the target hosts.
Thank you for taking the time to read my blog! Your support mean a lot to me.