Document

SUBSCRIBE TO GET FULL ACCESS TO THE E-BOOKS FOR FREE 🎁SUBSCRIBE NOW

Professional Dropdown with Icon

SUBSCRIBE NOW TO GET FREE ACCESS TO EBOOKS

Ansible-Adhoc Commands

Prerequisite:   Ansible should be installed on the master node. Should have root access to Master and other nodes Not  mandatory but In my…

 Ansible file Module
 Ansible Playbook Assignment
 Ansible Playbooks Examples
 

 Prerequisite: 

 

  • Ansible should be installed on the master node.
  • Should have root access to Master and other nodes
  • Not  mandatory but In my example, I am working as a root user
  • Master node and Managed Nodes ( Agent nodes) should be configured and in the network.
  • Master node’s public and private ssh should generate (ssh-keygen)
  • Master node’s public is to be shared with Agent nodes ( /root/.ssh/authorized_keys) file

 

Current Setup: 3 Ubuntu Servers ( 1 master and 2 agent nodes)\

   Master server is also a dbserver.

   Master IP: 192.168.33.10

   Webserver1 IP: 192.168.33.11

   Webserver2 IP: 192.168.33.12

Host file Setup

Goto /etc/ansible/hosts file and add below entries

 [dbservers]

 192.168.33.10

[webservers]

192.168.33.11

192.168.33.12

Ansible Configuration file
/etc/ansible/ansible.cfg
 
uncomment(remove #) host file path
inventory      = /etc/ansible/hosts
 

Adhoc Commands

Ansible commands to manage the IaC.

If the configuration is not complex( do not have conditional statements or loops etc.) then we can use the Adhoc commands.

Challenge 1 ping Module:

 

  1. To check web servers are reachable or not using Adhoc commands.
  2. To check all hosted servers are reachable or not using Adhoc commands.
  3. To check webserver1 is reachable or not using Adhoc command.
  4. To check all hosted servers are reachable or not using Adhoc commands when the hosts file is in a different location (/home/vagrant/mydir/hosts)

 

The module used: ping

Solution

Run the below ansible adhoc command with ping module

 

  1. ansible webservers -m ping
  2. ansible all -m ping
  3. ansible 192.168.33.11 -m ping
  4. ansible -i /home/vagrant/mydir/hosts all -m ping

 

 

 



 

Challenge 2 Shell Module

 

  1. Install apache on webservers
  2. Uninstall apache on webservers
  3. Create a file in /tmp/1.txt on dbservers and webservers
  4. Change the permission of /tmp/1.txt file by give rw permission to owner, r permission to group, and other users.
  5. Copy /tmp/1.txt file to /tmp/2.txt file

 

Module:- Shell ( it is the module to run any Linux commands using Ansible)

Solution:

 

  1. ansible webservers -m shell -a ” apt update && apt install apache2 -y”
  2. ansible webservers -m shell -a ” apt purge apache2 -y”
  3. ansible webservers,dbservers -m shell -a “touch /tmp/1.txt”
  4. ansible webservers,dbservers -m shell -a “chmod 644 /tmp/1.txt”
  5. ansible webservers,dbservers -m shell -a “cp /tmp/1.txt /tmp/2.txt”

 

Challenge: apt/yum Module

 

  1. Install apache on webservers
  2. Uninstall apache package

 

Solution

 

  1. ansible webservers -m apt -a “name=apache2 state=present”
  2. ansible webservers -m apt -a “name=apache2 state=absent purge=yes”

 

Challenge 3 service module

 

  1. Start apache server on webservers
  2. Stop apache service on webservers
  3. Restart apache service on webservers

 

Solution

 

  1.  ansible webservers -m service -a “name=apache2 state=started”
  2.  ansible webservers -m service -a “name=apache2 state=stopped”
  3.  ansible webservers -m service -a “name=apache2 state=restarted”

 

Challenge 4 copy module

 

  1.  Copy a file /tmp/status.txt from master server to webservers
  2.  Copy a file /tmp/status.txt from master server to webservers at /tmp/newstatus.txt
  3. Create a file on webservers /tmp/4.txt with content Hello World

 

Solution

 

  1.   ansible webservers -m copy -a “src=/tmp/status.txt dest=/tmp/status.txt”
  2.   ansible webservers -m copy -a “src=/tmp/status.txt dest=/tmp/newstatus.txt”
  3.   ansible webservers -m copy -a “content=’Hello World’ dest=/tmp/4.txt”
Share your love

Leave a Reply

Your email address will not be published. Required fields are marked *