Latest Updates

Post Top Ad

24 August, 2021

Ansible Basic Commands and Demo

 

Ansible Basic Commands and Demo


In this blog, we will see ansible basic ad hoc commands with the demo.












Requirements:

  • Three ec2 instances
  • Ad Hoc commands

Ansible Architecture


1. Three EC2 Instance


We have created three AWS ec2 machines. One ec2 machine act as an ansible server. The other two ec2 machines act as nodes. 

On the Ansible server, we will run ansible commands and as per concept, the same command should execute on the other two nodes.

Let us keep all the three ec2 machines up and running. Login to all Linux machines as an ansible user.


2. Ad Hoc Commands


Command:

Run-on ansible server
[ansible@ip-172-31-4-81 ~]$ ansible all -a "touch piyush"


Command ran successfully. It has changed on all nodes. Let us verify if the file was created on two other nodes or not?
Login to each node one by one and verify the piyush file.

Node1:

Node2:



Command:

Run-on ansible server
[ansible@ip-172-31-4-81 ~]$ ansible demo -a "sudo yum install httpd -y"

[ansible@ip-172-31-4-81 ~]$ ansible demo -a "sudo yum install httpd -y"
[WARNING]: Consider using 'become', 'become_method', and 'become_user' rather than running sudo
[WARNING]: Platform linux on host 172.31.11.87 is using the discovered Python interpreter at
/usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more
information.
172.31.11.87 | CHANGED | rc=0 >>
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.48-2.amzn2 will be installed
--> Processing Dependency: mod_http2 for package: httpd-2.4.48-2.amzn2.x86_64
--> Running transaction check
---> Package mod_http2.x86_64 0:1.15.19-1.amzn2.0.1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package         Arch         Version                    Repository        Size
================================================================================
Installing:
 httpd           x86_64       2.4.48-2.amzn2             amzn2-core       1.3 M
Installing for dependencies:
 mod_http2       x86_64       1.15.19-1.amzn2.0.1        amzn2-core       149 k

Transaction Summary
================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 1.5 M
Installed size: 4.4 M
Downloading packages:
--------------------------------------------------------------------------------
Total                                              601 kB/s | 1.5 MB  00:02
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : httpd-2.4.48-2.amzn2.x86_64                                  1/2
  Installing : mod_http2-1.15.19-1.amzn2.0.1.x86_64                         2/2
  Verifying  : mod_http2-1.15.19-1.amzn2.0.1.x86_64                         1/2
  Verifying  : httpd-2.4.48-2.amzn2.x86_64                                  2/2

Installed:
  httpd.x86_64 0:2.4.48-2.amzn2

Dependency Installed:
  mod_http2.x86_64 0:1.15.19-1.amzn2.0.1

Complete!
[WARNING]: Platform linux on host 172.31.0.17 is using the discovered Python interpreter at
/usr/bin/python, but future installation of another Python interpreter could change this. See
https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html for more
information.
172.31.0.17 | CHANGED | rc=0 >>
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.48-2.amzn2 will be installed
--> Processing Dependency: mod_http2 for package: httpd-2.4.48-2.amzn2.x86_64
--> Running transaction check
---> Package mod_http2.x86_64 0:1.15.19-1.amzn2.0.1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package         Arch         Version                    Repository        Size
================================================================================
Installing:
 httpd           x86_64       2.4.48-2.amzn2             amzn2-core       1.3 M
Installing for dependencies:
 mod_http2       x86_64       1.15.19-1.amzn2.0.1        amzn2-core       149 k

Transaction Summary
================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 1.5 M
Installed size: 4.4 M
Downloading packages:
--------------------------------------------------------------------------------
Total                                              376 kB/s | 1.5 MB  00:04
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : httpd-2.4.48-2.amzn2.x86_64                                  1/2
  Installing : mod_http2-1.15.19-1.amzn2.0.1.x86_64                         2/2
  Verifying  : mod_http2-1.15.19-1.amzn2.0.1.x86_64                         1/2
  Verifying  : httpd-2.4.48-2.amzn2.x86_64                                  2/2

Installed:
  httpd.x86_64 0:2.4.48-2.amzn2

Dependency Installed:
  mod_http2.x86_64 0:1.15.19-1.amzn2.0.1

Complete!
[ansible@ip-172-31-4-81 ~]$



Verify on Node1:

Verify on Node2:







Command:

Run-on ansible server:

[ansible@ip-172-31-4-81 ~]$ ansible demo -ba "yum remove httpd -y"



-b stands for sudo access. Rather than using sudo we can give -b argument.

This ansible command will uninstall httpd on all nodes.



Command:

Run-on ansible server:

[ansible@ip-172-31-4-81 ~]$ ansible demo -ba "df -hP"




This ansible command gave the output of df -hP from all nodes.


Learn GitHub Click Here







No comments:

Post Top Ad

Your Ad Spot

Pages