Ansible Excersise :
-------------------
Excesise 1: working with inventory
----------------------------------
* install ansible and sshpass "ssh with arguments":
sshpass allows you to pass password to ssh connection as an argument
# apt-get install ansible sshpass
* please edit your ansible.cfg file to ignore the SSH authenticity checking
# vi /etc/ansible.cfg
search for [defaults] and write this :
[defaults]
host_key_checking = False
------
to use cowsay in ansible "for more fun" , uncomment this line :
cow_selection=random
* open your inventory file and add your class groups as shown :
# vi /etc/ansible/hosts
and add the following :
[me]
localhost
[group1]
pc1.sse.ws.afnog.org
pc2.sse.ws.afnog.org
pc3.sse.ws.afnog.org
pc4.sse.ws.afnog.org
[group2]
pc[5:8].sse.ws.afnog.org
[group3]
pc[9:12].sse.ws.afnog.org
[group4]
pc[13:16].sse.ws.afnog.org
[group5]
pc[17:20].sse.ws.afnog.org
[group6]
pc[21:24].sse.ws.afnog.org
[group7]
pc[25:28].sse.ws.afnog.org
[group8]
pc[29:32].sse.ws.afnog.org
# setting user name and password that ansible going to use for all groups
[fqdn]
pc[21:24].sse.ws.afnog.org
pc[13:14].sse.ws.afnog.org
pc[16:19].sse.ws.afnog.org
pc[7:10].sse.ws.afnog.org
[all:vars]
ansible_ssh_user=afnog
ansible_ssh_pass=SSE@Success!
# save and quit your hosts file
* Copy the Public Key
# ssh-copy-id afnog@pcX.sse.ws.afnog.org
* Repeat the same process for other machines you wish to login automatically with.
--------------------------------------------------
Excesise 2: working with ansible ad-hoc commands
--------------------------------------------------
* check all my inventory hosts are ready to be managed by Ansible
# ansible all -m ping
* collect and display the discovered facts for the localhost
# ansible localhost -m setup
# ansible pcX.sse.ws.afnog.org -m setup -a "filter=*ipv4“
* excute echo in remote hosts :
# ansible all -m shell -a "echo hi its YourName > /home/afnog/YourName.txt" -b
* run the uptime command on all hosts in the group 2 group
# ansible group2 -m command -a "uptime“
* fetch hard drives utilization
# ansible -m command -a 'df -h’ pcX.sse.ws.afnog.org
* remove telnet package from remote servers on group1:
ansible group1 -m apt -a "name=telnet state=absent" -b
--------------------------------------------------
Excesise 3: working with ansible play-books
--------------------------------------------------
create a new .yaml file with name mypcX.yaml and write tasks to:
- task to install cowsay pakage
- task to configure apache and create a new virtual host with the name wwwX.pcY.sse.ws.afnog.org.conf
- task to create a new document root directory for the new virtual host under name wwwX.pcY.sse.ws.afnog.org
- task to create a simple website in the new created document root
---------------------------------------------
1) task to install cowsay pakage :
# vi mypcX.yaml
---
- hosts: groupX
tasks:
- name: install COWSAY
become: yes
apt:
name: cowsay
state: latest
save and quit then run your playbook :
# ansible-playbook mypcX.yaml -b
---------------------------------------------------
2) task to configure apache and create a new virtual host with the name wwwX.pcY.sse.ws.afnog.org.conf
edit your mypcX.yaml and add the folowing :
# vi mypcX.yaml
- name: Configuring Virtual-Host
become: yes
template:
src: pcX-vhost.j2
dest: "/etc/apache2/sites-enabled/wwwX.{{ ansible_fqdn }}.conf"
notify: restart apache2
handlers:
- name: restart apache2
become: yes
service :
name: apache2
state: restarted
save and quit
create the pcX-vhost.j2 file and add the virtual host configuration :
# vi pcX-vhost.j2
SSSSSSSSSSSSSSS SSSSSSSSSSSSSSS EEEEEEEEEEEEEEEEEEEEEE 222222222222222 000000000 1111111 999999999
SS:::::::::::::::S SS:::::::::::::::SE::::::::::::::::::::E 2:::::::::::::::22 00:::::::::00 1::::::1 99:::::::::99
S:::::SSSSSS::::::SS:::::SSSSSS::::::SE::::::::::::::::::::E 2::::::222222:::::2 00:::::::::::::00 1:::::::1 99:::::::::::::99
S:::::S SSSSSSSS:::::S SSSSSSSEE::::::EEEEEEEEE::::E 2222222 2:::::2 0:::::::000:::::::0111:::::19::::::99999::::::9
S:::::S S:::::S E:::::E EEEEEE 2:::::2 0::::::0 0::::::0 1::::19:::::9 9:::::9
S:::::S S:::::S E:::::E 2:::::2 0:::::0 0:::::0 1::::19:::::9 9:::::9
S::::SSSS S::::SSSS E::::::EEEEEEEEEE 2222::::2 0:::::0 0:::::0 1::::1 9:::::99999::::::9
SS::::::SSSSS SS::::::SSSSS E:::::::::::::::E 22222::::::22 0:::::0 000 0:::::0 1::::l 99::::::::::::::9
SSS::::::::SS SSS::::::::SS E:::::::::::::::E 22::::::::222 0:::::0 000 0:::::0 1::::l 99999::::::::9
SSSSSS::::S SSSSSS::::S E::::::EEEEEEEEEE 2:::::22222 0:::::0 0:::::0 1::::l 9::::::9
S:::::S S:::::S E:::::E 2:::::2 0:::::0 0:::::0 1::::l 9::::::9
S:::::S S:::::S E:::::E EEEEEE 2:::::2 0::::::0 0::::::0 1::::l 9::::::9
SSSSSSS S:::::SSSSSSSS S:::::SEE::::::EEEEEEEE:::::E 2:::::2 2222220:::::::000:::::::0111::::::111 9::::::9
S::::::SSSSSS:::::SS::::::SSSSSS:::::SE::::::::::::::::::::E 2::::::2222222:::::2 00:::::::::::::00 1::::::::::1 9::::::9
S:::::::::::::::SS S:::::::::::::::SS E::::::::::::::::::::E 2::::::::::::::::::2 00:::::::::00 1::::::::::1 9::::::9
SSSSSSSSSSSSSSS SSSSSSSSSSSSSSS EEEEEEEEEEEEEEEEEEEEEE 22222222222222222222 000000000 11111111111199999999
$$\ $$\ $$\ $$\
\$$\ $$ | \$$\ $$ |
\$$\ $$ /$$$$$$\ $$$$$$\ $$$$$$\ $$$$$$\\$$\ $$ /
\$$$$ / \____$$\ \____$$\ \____$$\ \____$$\\$$$$ /
\$$ / $$$$$$$ | $$$$$$$ | $$$$$$$ | $$$$$$$ |\$$ /
$$ | $$ __$$ |$$ __$$ |$$ __$$ |$$ __$$ | $$ |
$$ | \$$$$$$$ |\$$$$$$$ |\$$$$$$$ |\$$$$$$$ | $$ |
\__| \_______| \_______| \_______| \_______| \__|
--------------------------------------------------------------------------------------------------------------------------
;; save and quit your file
run your playbook:
# ansible-playbook mypcX.yaml -b
in your browser check your new created website ^_^
use your partners domain :
wwwX.pcY.sse.afnog.org
--------------------------------------------------------
--------------------------------------------------------
$$$$$$\ $$$$$$\ $$$$$$$$\ $$$$$$\ $$\
$$ __$$\ $$ __$$\ $$ _____| $$ __$$\ $$ |
$$ / \__|$$ / \__|$$ | $$ / \__|$$ | $$$$$$\ $$$$$$$\ $$$$$$$\
\$$$$$$\ \$$$$$$\ $$$$$\ $$$$$$\ $$ | $$ | \____$$\ $$ _____|$$ _____|
\____$$\ \____$$\ $$ __|\______|$$ | $$ | $$$$$$$ |\$$$$$$\ \$$$$$$\
$$\ $$ |$$\ $$ |$$ | $$ | $$\ $$ |$$ __$$ | \____$$\ \____$$\
\$$$$$$ |\$$$$$$ |$$$$$$$$\ \$$$$$$ |$$ |\$$$$$$$ |$$$$$$$ |$$$$$$$ |
\______/ \______/ \________| \______/ \__| \_______|\_______/ \_______/
Appendices:
-----------
-----------
### full mypcX.yaml file :
---
- hosts: pc15.sse.ws.afnog.org
tasks:
- name: installing cowsay package
apt:
name: cowsay
state: latest
- name: create virtual host file
template: src=pcX-vhost.j2 dest=/etc/apache2/sites-available/wwwX.{{ ansible_fqdn }}.conf
- name: a2ensite wwwX.{{ ansible_fqdn }}
command: a2ensite wwwX.{{ ansible_fqdn }}
args:
creates: /etc/apache2/sites-enabled/wwwX.{{ ansible_fqdn }}.conf
notify:
- restart apache2
- name: create the root directory
file:
path: /var/www/html/wwwX.{{ ansible_fqdn }}
state: directory
- name: creating index file
become: yes
template:
src: index.j2
dest: /var/www/html/wwwX.{{ ansible_fqdn }}/index.html
handlers:
- name: restart apache2
service:
name: apache2
state: restarted
------------------------------------------------------
#### full pcX-vhost.j2 file #####
SSSSSSSSSSSSSSS SSSSSSSSSSSSSSS EEEEEEEEEEEEEEEEEEEEEE 222222222222222 000000000 1111111 999999999
SS:::::::::::::::S SS:::::::::::::::SE::::::::::::::::::::E 2:::::::::::::::22 00:::::::::00 1::::::1 99:::::::::99
S:::::SSSSSS::::::SS:::::SSSSSS::::::SE::::::::::::::::::::E 2::::::222222:::::2 00:::::::::::::00 1:::::::1 99:::::::::::::99
S:::::S SSSSSSSS:::::S SSSSSSSEE::::::EEEEEEEEE::::E 2222222 2:::::2 0:::::::000:::::::0111:::::19::::::99999::::::9
S:::::S S:::::S E:::::E EEEEEE 2:::::2 0::::::0 0::::::0 1::::19:::::9 9:::::9
S:::::S S:::::S E:::::E 2:::::2 0:::::0 0:::::0 1::::19:::::9 9:::::9
S::::SSSS S::::SSSS E::::::EEEEEEEEEE 2222::::2 0:::::0 0:::::0 1::::1 9:::::99999::::::9
SS::::::SSSSS SS::::::SSSSS E:::::::::::::::E 22222::::::22 0:::::0 000 0:::::0 1::::l 99::::::::::::::9
SSS::::::::SS SSS::::::::SS E:::::::::::::::E 22::::::::222 0:::::0 000 0:::::0 1::::l 99999::::::::9
SSSSSS::::S SSSSSS::::S E::::::EEEEEEEEEE 2:::::22222 0:::::0 0:::::0 1::::l 9::::::9
S:::::S S:::::S E:::::E 2:::::2 0:::::0 0:::::0 1::::l 9::::::9
S:::::S S:::::S E:::::E EEEEEE 2:::::2 0::::::0 0::::::0 1::::l 9::::::9
SSSSSSS S:::::SSSSSSSS S:::::SEE::::::EEEEEEEE:::::E 2:::::2 2222220:::::::000:::::::0111::::::111 9::::::9
S::::::SSSSSS:::::SS::::::SSSSSS:::::SE::::::::::::::::::::E 2::::::2222222:::::2 00:::::::::::::00 1::::::::::1 9::::::9
S:::::::::::::::SS S:::::::::::::::SS E::::::::::::::::::::E 2::::::::::::::::::2 00:::::::::00 1::::::::::1 9::::::9
SSSSSSSSSSSSSSS SSSSSSSSSSSSSSS EEEEEEEEEEEEEEEEEEEEEE 22222222222222222222 000000000 11111111111199999999
$$\ $$\ $$\ $$\
\$$\ $$ | \$$\ $$ |
\$$\ $$ /$$$$$$\ $$$$$$\ $$$$$$\ $$$$$$\\$$\ $$ /
\$$$$ / \____$$\ \____$$\ \____$$\ \____$$\\$$$$ /
\$$ / $$$$$$$ | $$$$$$$ | $$$$$$$ | $$$$$$$ |\$$ /
$$ | $$ __$$ |$$ __$$ |$$ __$$ |$$ __$$ | $$ |
$$ | \$$$$$$$ |\$$$$$$$ |\$$$$$$$ |\$$$$$$$ | $$ |
\__| \_______| \_______| \_______| \_______| \__|
--------------
ANSIBLE TELEGRAM GROUP:
https://t.me/learnAnsible