Reference:
- Ansible documentation main page: docs.ansible.com
- Modules (Ansible has 1000s of modules!):
- ping: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/ping_module.html
- shell: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/shell_module.html#ansible-collections-ansible-builtin-shell-module
- apt/yum: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/yum_module.html
- service: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/service_module.html
Challenge Part 1:
- 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)
Solution Part 1:
The module used: ping
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 Part 2:
Solution Part 2:
The module used: shell
- 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
Solution Part 2:
The module used: shell
Run the below ansible adhoc command with shell module:
- ansible webservers -m shell -a " 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 Part 3:
Solution Part 3:
The module used: apt/yum (apt=debian, yum=redhat)
- To install apache2 on webservers using apt module.
- 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:
- ansible webservers -m apt -a "name=apache2 state=present"
- ansible webservers -m apt -a "name=apache2 state=absent purge=yes"
Challenge Part 4:
Solution Part 4:
The module used: service
- Start apache server on webservers
- Stop apache server on webservers
- Restart apache server on webservers
- Reload apache server on webservers
The module used: service
Run the below ansible adhoc command with service module (apache2 is required to be installed first):
- 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"
- ansible webservers -m service -a "name=apache2 state=reloaded"
Challenge Part 5:
Solution Part 5:
The module used: copy
- 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.
- 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).
- create a file with text: "Hello World" in agent system by using copy module from master system.
The module used: copy
Run the below ansible adhoc command with copy module (apache2 is required to be installed first):
- 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/helloworldcontent.txt"
No comments:
Post a Comment