Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Kubernetes is an orchestration tool but Ansible is a configuration tool.

Create 3 Amazon Linux AMI - t2.micro – sg: All traffic

In controller & all client instances run

sudo -i
yum update -y
yum install python-pip -y
python –version

In controller run
sudo amazon-linux-extras install ansible2 [to add ansible repository]
yum install ansible
exit

Run the following on controller instance only


ssh-keygen [to generate key]
cd .ssh/
ls
id_rsa – private key, id_rsa.pub – public key(we need to copy this key)

cat id_rsa.pub (copy the key)

Now go to a client instance

exit [to exit from root user]


cd .ssh/
ls
vi authorized_keys [go to the end and paste the copied key. DON’T DELETE PREVIOUS KEY]

Now go to controller

cd ..
cd /etc/ansible/
ls
sudo vi hosts

Go to the end & type

[webserver]
<Private_ip-of-webserver>

[database]
<Private_ip-of-database>

ansible -m ping all

ping means you are requesting connection & pong means you got response.

Now we will install git & vsftpd (used for file transfer) in webserver from controller

ansible -m yum -a "name=git state=present" webserver --become

ansible -m yum -a "name=vsftpd state=present" webserver --become

Now go to webserver & check whether git & vsftpd was installed properly

git version

service vsftpd status

Go to controller & start the vsftpd service

ansible -m service -a "name=vsftpd state=restarted" webserver --become

yum – installation purpose


service – service related purpose
-a – action
--become – to work as root user

Now check whether ftp is working properly on webserver or not – go to browser & type
ftp://<public-ip of webserver>
Now install ftp in webserver

sudo yum install ftp -y


ftp
Ctrl+C
cd /var/ftp
ls (you can see pub)
cd pub
sudo vi test1

Now go to browser & check – you can see ‘test1’ file inside pub.

04october2020

In controller instance
ansible -m yum -a "name=httpd state=present" webserver --become

Now check in webserver whether httpd is installed properly


cd /var/www/html/
If this command gets executed successfully then it’s installed. The folders gets created automatically
when httpd is installed.
service httpd status

In controller run
ansible -m service -a "name=httpd state=restarted" webserver --become
(to start httpd service)

Topic: Copying a file from controller to webserver


In controller inside /home/ec2-user

mkdir dir1
cd dir1/
vi index.html
<html>
<head>
<title>
A Simple HTML Document
</title>
</head>
<body>
<p>This is a very simple HTML document</p>
<p>It only has two paragraphs</p>
</body>
</html>

ansible -m copy -a "src=/home/ec2-user/dir1/index.html dest=/var/www/html/" webserver –


become
Now go to webserver
cd /var/www/html/
ls (you can see index.html which was copied from controller)
sudo -i
chmod 777 /var/www/html/index.html
Go to browser & type <public-ip-of-webserver>/index.html (you can see the html page which you
created)

In controller
sudo mkdir -m 777 /tmp1
cd /tmp1
vi index1.html
<html>
<head>
<title>
A Simple HTML Document
</title>
</head>
<body>
<p>This is a very simple HTML document</p>
<p>It only has two paragraphs</p>
</body>
</html>

ansible -m copy -a "src=/tmp1/index1.html dest=/var/www/html/ mode=777" webserver –become

Go to browser & type <public-ip-of-webserver>/index1.html (you can see the html page which you
created)

Topic: Uninstall httpd service


In controller run
ansible -m yum -a "name=httpd state=absent" webserver --become

Topic: Playbook
In controller run
cd /
sudo mkdir -m 777 dir1
cd dir1/
sudo vi file1.yml
---
- name: install vsftpd
hosts: webserver
become: true
become_user: root

tasks:
- name: install package vsftpd
yum: name=vsftpd state=present
- name: write the apache config file
service: name=vsftpd state=restarted
ansible-playbook file1.yml

In webserver run
cd /var/ftp/pub
(If this command gets executed successfully then it’s installed)
Now go to browser & type
ftp:// <public-ip-of-webserver>/pub

(installing tomcat is continued in next lecture)

ftp is a protocol & vsftpd is the corresponding service

httpd is the corresponding server of http

https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html

10october2020

Q: If you can use shell then what is the need for playbook?

A: We can implement Infrastructure as a Code (IaaC) with the help of script files in playbook.

nohup - means that you don’t have to start the server again & again, even if the server stops then as
soon as the service starts the server will also start automatically

Topic: Install tomcat with Playbook


In controller run
sudo mkdir –m 777 playbook
cd playbook
sudo vi myplaybook1.yml
---
- name: install tomcat server
hosts: database
become: true

tasks:
- name: install java
yum:
name: java
state: latest
- name: download tomcat
get_url:
url: https://downloads.apache.org/tomcat/tomcat-8/v8.5.58/bin/apache-tomcat-8.5.58.tar.gz
dest: /usr/local
- name: extract tomcat
unarchive:
src: /usr/apache-tomcat-8.5.58.tar.gz
dest: /usr/local
remote_src: yes

- name: start tomcat service


shell: nohup /usr/local/apache-tomcat-8.5.58/bin/startup.sh

ansible-playbook myplaybook1.yml
ansible-playbook myplaybook1.yml --syntax -check

11october2020

Configure Jenkins in an instance to create ‘myproject.war’ file

Connect to Jenkins instance & Controller instance using putty

Note : if you want to send a file on Jenkins(sender) instance to Controller(receiver) instance, then
the public key of Controller instance should be present in Jenkins instance.

On Jenkins instance

cd /var/lib/Jenkins/workspace/Deploy/target/
ls (you can see addressbook.war)
cd
mkdir copy1
sudo mv /var/lib/Jenkins/workspace/Deploy/target/addressbook.war copy1
cd copy1
ls (you can see addressbook.war)

Now go to Downloads folder in local drive of your PC and open the pem file in Notepad++ and copy
the entire content

sudo vi key1.pem (paste the copied content)


sudo chmod 777 key1.pem
scp -i "key1.pem" addressbook.war ec2-user@<private-ip-of-controller>:/home/ec2-
user/playbook/
In Controller instance
cd playbook
ls (you can see addressbook.war copied from Jenkins instance)

Now we have to create a playbook to host the war file

Connect to the agent instance (database) with putty

On Controller instance
sudo vi myplaybook3.yml

---
- name: install tomcat server
hosts: database
become: true

tasks:
- name: install java
yum:
name: java
state: latest
- name: download tomcat
get_url:
url: https://downloads.apache.org/tomcat/tomcat-8/v8.5.58/bin/apache-tomcat-8.5.58.tar.gz
dest: /usr/local
- name: extract tomcat
unarchive:
src: /usr/local/apache-tomcat-8.5.58.tar.gz
dest: /usr/local
remote_src: yes
- name: copy war file
copy: src=/home/ec2-user/playbook/addressbook.war dest=/usr/local/apache-tomcat-
8.5.58/webapps

- name: start tomcat service


shell: nohup /usr/local/apache-tomcat-8.5.58/bin/startup.sh

ansible-playbook myplaybook3.yml --syntax --check


ansible-playbook myplaybook3.yml

Now connect to agent instance (database) using putty

cd /usr/local/apache-tomcat-8.5.58/webapps/
ls (you can see addressbook.war)

Go to browser & enter


<public-ip-of-database>:8080/addressbook

You might also like