Followers

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...

 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"

Adhoc Command Assignment





 








COMMENTS

Name

Ansible,6,AWS,1,Azure DevOps,1,Containerization with docker,2,DevOps,2,Docker Quiz,1,Docker Swarm,1,DockerCompose,1,ELK,2,git,2,Jira,1,Kubernetes,1,Kubernetes Quiz,5,SAST DAST Security Testing,1,SDLC Quiz,3,SonarQube,3,Splunk,2,vagrant kubernetes,1,YAML Basics,1,
ltr
item
DevOpsWorld: Ansible Adhoc Commands
Ansible Adhoc Commands
https://blogger.googleusercontent.com/img/a/AVvXsEhHJ6RvZDkXEKuM_P-iUWX7kvtEHJVA_RN-5BbXDJitlWbbmlVXr0svNVoobwKra68uJ1_1HEJL2qieczIXb0_5lfKfCprQWdxK2IlwT3MXtVb6GJW3oYikb1kKMjHMqEOLEYlgvuVdhqvO-pITfBb1jpweUF6zJZHgjo35T0usH2KIBShHQYWZbKSw=w455-h206
https://blogger.googleusercontent.com/img/a/AVvXsEhHJ6RvZDkXEKuM_P-iUWX7kvtEHJVA_RN-5BbXDJitlWbbmlVXr0svNVoobwKra68uJ1_1HEJL2qieczIXb0_5lfKfCprQWdxK2IlwT3MXtVb6GJW3oYikb1kKMjHMqEOLEYlgvuVdhqvO-pITfBb1jpweUF6zJZHgjo35T0usH2KIBShHQYWZbKSw=s72-w455-c-h206
DevOpsWorld
https://www.devopsworld.co.in/2022/02/ansible-adhoc-commands.html
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/
https://www.devopsworld.co.in/2022/02/ansible-adhoc-commands.html
true
5997357714110665304
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content