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
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:
- To check web servers are reachable or not using Adhoc commands.
- To check all hosted servers are reachable or not using Adhoc commands.
- To check webserver1 is reachable or not using Adhoc command.
- 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
- ansible webservers -m ping
- ansible all -m ping
- ansible 192.168.33.11 -m ping
- ansible -i /home/vagrant/mydir/hosts all -m ping
Challenge 2 Shell Module
- Install apache on webservers
- Uninstall apache on webservers
- Create a file in /tmp/1.txt on dbservers and webservers
- Change the permission of /tmp/1.txt file by give rw permission to owner, r permission to group, and other users.
- Copy /tmp/1.txt file to /tmp/2.txt file
Module:- Shell ( it is the module to run any Linux commands using Ansible)
Solution:
- ansible webservers -m shell -a " apt update && apt install apache2 -y"
- ansible webservers -m shell -a " apt purge apache2 -y"
- ansible webservers,dbservers -m shell -a "touch /tmp/1.txt"
- ansible webservers,dbservers -m shell -a "chmod 644 /tmp/1.txt"
- ansible webservers,dbservers -m shell -a "cp /tmp/1.txt /tmp/2.txt"
Challenge: apt/yum Module
- Install apache on webservers
- Uninstall apache package
Solution
- ansible webservers -m apt -a "name=apache2 state=present"
- ansible webservers -m apt -a "name=apache2 state=absent purge=yes"
Challenge 3 service module
- Start apache server on webservers
- Stop apache service on webservers
- Restart apache service on webservers
Solution
- ansible webservers -m service -a "name=apache2 state=started"
- ansible webservers -m service -a "name=apache2 state=stopped"
- ansible webservers -m service -a "name=apache2 state=restarted"
Challenge 4 copy module
- Copy a file /tmp/status.txt from master server to webservers
- Copy a file /tmp/status.txt from master server to webservers at /tmp/newstatus.txt
- Create a file on webservers /tmp/4.txt with content Hello World
Solution
- ansible webservers -m copy -a "src=/tmp/status.txt dest=/tmp/status.txt"
- ansible webservers -m copy -a "src=/tmp/status.txt dest=/tmp/newstatus.txt"
- ansible webservers -m copy -a "content='Hello World' dest=/tmp/4.txt"
COMMENTS