Saturday, October 15, 2016

how to deploy web server with ansible

*** information
target server  ip:
                       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

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

or you just copy the  file,, 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

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

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

- execute command on remote server
# ansible all -a "ifconfig" | SUCCESS | rc=0 >>
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet  netmask  broadcast
        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  netmask
        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 | SUCCESS => {
    "changed": false,
    "ping": "pong"

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

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:
  remote_user: root

  - name: ensure apache is at the latest version
    apt: name=apache2 state=latest
        - 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 [] *********************************************************

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

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

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

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

- access the web server with url;
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