Sunday, February 20, 2022

ASSIGNMENT: Ansible Ad-hoc Commands (ping module, shell module, apt/yum module)



Reference: 


Challenge Part 1:
  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)

Solution Part 1:
The module used: ping
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 Part 2:
  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

Solution Part 2:
The module used: shell
Run the below ansible adhoc command with shell module:
  1. ansible webservers -m shell -a " 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 Part 3:
  1. To install apache2 on webservers using apt module.
  2. To uninstall apache2 on webservers using apt module.

Solution Part 3:
The module used: apt/yum (apt=debian, yum=redhat)
Run the below ansible adhoc command with apt/yum module:
  1. ansible webservers -m apt -a "name=apache2 state=present"
  2. ansible webservers -m apt -a "name=apache2 state=absent purge=yes"


Challenge Part 4:
  1. Start apache server on webservers
  2. Stop apache server on webservers
  3. Restart apache server on webservers
  4. Reload apache server on webservers

Solution Part 4:
The module used: service
Run the below ansible adhoc command with service module (apache2 is required to be installed first):
  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"
  4. ansible webservers -m service -a "name=apache2 state=reloaded"


Challenge Part 5:
  1. create a file /tmp/status.txt in master system and copy it to agent system using copy module with the destination having the same name.
  2. create a file /tmp/status.txt in master system and copy it to agent system using copy module with the destination having a different name (newstatus.txt).
  3. create a file with text: "Hello World" in agent system by using copy module from master system.

Solution Part 5:
The module used: copy
Run the below ansible adhoc command with copy module (apache2 is required to be installed first):
  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/helloworldcontent.txt"


No comments:

Post a Comment

Fluentd

Open-source log data collector > why logs? - for compliance (auditing, company, business) - for security (transparency, monitoring, admin...