Saturday, October 15, 2016

how to deploy web server with ansible

*** information
target server  ip: 192.168.202.242
                       os: ubuntu yakkety yat 16.10


*** client
1- install packages
$ sudo apt-get install ansible

2- create ssh key pair using root account
$ sudo su root
# ssh-keygen

- after that two file will be created in /root/.ssh
# ls ~/.ssh
id_rsa  id_rsa.pub

3- copy the public key to remote server
# ssh-copy-id -i  ~/.ssh/id_rsa.pub root@192.168.202.242

or you just copy the  file, id_rsa.pub, to remote server and put it in /root/.ssh with the name authorized_keys


4- add host to ansible by edit file /etc/ansible/hosts, and add the following line
192.168.202.242

5- how we ready to test whether the ansible is working or not,

- ping
#  ansible all -m ping
192.168.202.242 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}


- execute command on remote server
# ansible all -a "ifconfig"
192.168.202.242 | SUCCESS | rc=0 >>
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.202.242  netmask 255.255.255.0  broadcast 192.168.202.255
        inet6 fe80::20c:29ff:fe9e:23be  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:9e:23:be  txqueuelen 1000  (Ethernet)
        RX packets 8512  bytes 3154758 (3.1 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1680  bytes 239556 (239.5 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 70535  bytes 4237757 (4.2 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 70535  bytes 4237757 (4.2 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0



**note if you want to execute as another use, you have to add public key to that user like we have don on step 3

- ping as user anser
# ansible all -m ping -u anser
192.168.202.242 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}


- execute whoami command
# ansible all -a "whoami" -u anser
192.168.202.242 | SUCCESS | rc=0 >>
anser




6- create an index.html file

# echo "this web server is installed by ansible" > index.html

7- create playbook that list all the rule that it have to be done in order to get our web server setup and with the index file that we just created

so we create file web.yaml with the following content
- hosts: 192.168.202.242
  remote_user: root

  tasks:
  - name: ensure apache is at the latest version
    apt: name=apache2 state=latest
    notify:
        - start apache2
  - name: Upload default index.html for host
    copy: src=index.html dest=/var/www/html/ mode=0644


8- run the ansible-playbook with the playbook we just created

# ansible-playbook web.yaml

PLAY [192.168.202.242] *********************************************************

TASK [setup] *******************************************************************
ok: [192.168.202.242]

TASK [ensure apache is at the latest version] **********************************
ok: [192.168.202.242]

TASK [Upload default index.html for host] **************************************
ok: [192.168.202.242]

PLAY RECAP *********************************************************************
192.168.202.242            : ok=3    changed=0    unreachable=0    failed=0 



- access the web server with url; http://192.168.202.242
and below is the snapshot of it



so we can automate as many web servers as we like with this tools with ease.


No comments:

Post a Comment