-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day3_Ansible
841 lines (691 loc) · 29.6 KB
/
Day3_Ansible
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
Day3 - Ansible
-----------------
Grouuping the info with folders
root@ubuntu:~/verizon# mkdir Day3
root@ubuntu:~/verizon# cd Day3/
root@ubuntu:~/verizon/Day3# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f17825cdffcf verizon/ansible-ubuntu "/usr/sbin/sshd -D" 22 hours ago Up 17 hours 0.0.0.0:2002->22/tcp, 0.0.0.0:8002->80/tcp ubuntu3
cb673103fe9f verizon/ansible-ubuntu "/usr/sbin/sshd -D" 22 hours ago Up 17 hours 0.0.0.0:2001->22/tcp, 0.0.0.0:8001->80/tcp ubuntu2
19d5af557c82 verizon/ansible-ubuntu "/usr/sbin/sshd -D" 23 hours ago Up 17 hours 0.0.0.0:2000->22/tcp, 0.0.0.0:8000->80/tcp ubuntu1
root@ubuntu:~/verizon/Day3# vim hosts
root@ubuntu:~/verizon/Day3# cat hosts
[all]
ubuntu[1:3]
[dev]
ubuntu1
[qa]
ubuntu[1:2]
[prod]
ubuntu3
root@ubuntu:~/verizon/Day3# mkdir host_vars group_vars
root@ubuntu:~/verizon/Day3# ls -lrth
total 12K
-rw-r--r-- 1 root root 68 Sep 4 21:24 hosts
drwxr-xr-x 2 root root 4.0K Sep 4 21:24 host_vars
drwxr-xr-x 2 root root 4.0K Sep 4 21:24 group_vars
Create 3 files under host_vars
-------------------------------
root@ubuntu:~/verizon/Day3# touch ubuntu1 ubuntu2 ubuntu3
root@ubuntu:~/verizon/Day3# echo "ansible_port: 2000" > ubuntu1
root@ubuntu:~/verizon/Day3# echo "ansible_port: 2001" > ubuntu2
root@ubuntu:~/verizon/Day3# echo "ansible_port: 2003" > ubuntu3
root@ubuntu:~/verizon/Day3#
root@ubuntu:~/verizon/Day3#
root@ubuntu:~/verizon/Day3# cat ubuntu1
ansible_port: 2000
root@ubuntu:~/verizon/Day3# cat ubuntu2
ansible_port: 2001
root@ubuntu:~/verizon/Day3# cat ubuntu3
ansible_port: 2003
root@ubuntu:~/verizon/Day3# tree
.
├── group_vars
├── hosts
└── host_vars
├── ubuntu1
├── ubuntu2
└── ubuntu3
2 directories, 4 files
Create a file for group params (common parms file named after all group under group_vars)
----------------------------------
root@ubuntu:~/verizon/Day3# cd group_vars/
root@ubuntu:~/verizon/Day3/group_vars# vim all
root@ubuntu:~/verizon/Day3/group_vars# cat all
ansible_user: root
ansible_become_user: root
ansible_become_password: root
ansible_host: localhost
ansible_private_key_file: /root/.ssh/id_rsa
root@ubuntu:~/verizon/Day3/group_vars#
Run the ping command
------------------------
root@ubuntu:~/verizon/Day3# ansible -i hosts all -m ping
ubuntu2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
ubuntu1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
ubuntu3 | SUCCESS => {
"changed": false,
"ping": "pong"
}
Run with ansible config file
-----------------------
Create a config file ansible.cfg and mark the inventory details to it.,
root@ubuntu:~/verizon/Day3# cat ansible.cfg
[defaults]
inventory=hosts
root@ubuntu:~/verizon/Day3# ansible all -m ping
ubuntu2 | SUCCESS => {
"changed": false,
"ping": "pong"
}
ubuntu1 | SUCCESS => {
"changed": false,
"ping": "pong"
}
ubuntu3 | SUCCESS => {
"changed": false,
"ping": "pong"
}
root@ubuntu:~/verizon/Day3#
Download/clone the centos-passwordless image from github
--------------------------
oot@ubuntu:~/verizon/Day3 # git clone https://github.com/tektutor/centos-sshd-passwordless.git
root@ubuntu:~/verizon/Day3/centos-sshd-passwordless# ls -lrth
total 68K
-rw-r--r-- 1 root root 458 Sep 4 23:00 sudoers
-rw-r--r-- 1 root root 2.5K Sep 4 23:00 sshd_config
-rw-r--r-- 1 root root 41K Sep 4 23:00 myproject.log
-rw-r--r-- 1 root root 305 Sep 4 23:00 inventory
-rw-r--r-- 1 root root 829 Sep 4 23:00 installapache.yml
-rw-r--r-- 1 root root 852 Sep 4 23:00 Dockerfile
-rw-r--r-- 1 root root 179 Sep 4 23:00 ansible.cfg
Copy the publick key to authorized keys
------------------------
root@ubuntu:~/verizon/Day3/centos-sshd-passwordless# cp /root/.ssh/id_rsa.pub authorized_keys
root@ubuntu:~/verizon/Day3/centos-sshd-passwordless# ls -lrth
total 72K
-rw-r--r-- 1 root root 458 Sep 4 23:00 sudoers
-rw-r--r-- 1 root root 2.5K Sep 4 23:00 sshd_config
-rw-r--r-- 1 root root 41K Sep 4 23:00 myproject.log
-rw-r--r-- 1 root root 305 Sep 4 23:00 inventory
-rw-r--r-- 1 root root 829 Sep 4 23:00 installapache.yml
-rw-r--r-- 1 root root 852 Sep 4 23:00 Dockerfile
-rw-r--r-- 1 root root 179 Sep 4 23:00 ansible.cfg
-rw-r--r-- 1 root root 393 Sep 4 23:04 authorized_keys
Edit the Dockerfile with below contents
------------------------
root@ubuntu:~/verizon/Day3/centos-sshd-passwordless# cat Dockerfile
FROM centos:6.7
MAINTAINER Jeganathan Swaminathan <[email protected]> <http://www.tektutor.org>
RUN yum install -y openssh-server openssh-clients
# Set the password of root user to root
RUN echo 'root:root' | chpasswd
RUN usermod -aG wheel root
RUN sed -ri 's/^session\s+required\s+pam_loginuid.so$/session optional pam_loginuid.so/' /etc/pam.d/sshd
# Disable root login &
# Disable password login, only allow public key.
COPY sshd_config /etc/ssh/sshd_config
COPY sudoers /etc/sudoers
RUN mkdir -p /root/.ssh
COPY authorized_keys /root/.ssh/authorized_keys
# Add sshd running directory.
RUN mkdir -m 700 /var/run/sshd
# Add ssh key directory.
RUN /sbin/service sshd start && /sbin/service sshd stop
EXPOSE 22
EXPOSE 80
CMD ["/usr/sbin/sshd", "-D"]
Now build a new docker image with centos
---------------------------------
root@ubuntu:~/verizon/Day3/centos-sshd-passwordless# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
verizon/ansible-ubuntu latest 5b27e1379914 25 hours ago 234MB
wordpress latest 41e689eea0cd 4 days ago 408MB
nginx latest 71c43202b8ac 6 days ago 109MB
ubuntu 16.04 52b10959e8aa 13 days ago 115MB
ubuntu latest 16508e5c265d 13 days ago 84.1MB
mysql latest 29e0ae3b69b9 3 weeks ago 484MB
bash latest 505b35eff06a 5 weeks ago 14MB
hello-world latest 2cb0d9787c4d 8 weeks ago 1.85kB
root@ubuntu:~/verizon/Day3/centos-sshd-passwordless#
root@ubuntu:~/verizon/Day3/centos-sshd-passwordless#
root@ubuntu:~/verizon/Day3/centos-sshd-passwordless# docker build -t verizon/ansible-centos .
Sending build context to Docker daemon 130.6kB
Step 1/15 : FROM centos:6.7
.
.
.
Stopping sshd: [ OK ]
Removing intermediate container ad248e09b30b
---> cdfcc74189d6
Step 13/15 : EXPOSE 22
---> Running in 85dbcc716bdd
Removing intermediate container 85dbcc716bdd
---> 3e11dd30f694
Step 14/15 : EXPOSE 80
---> Running in 974ba4728cb1
Removing intermediate container 974ba4728cb1
---> af461f8b5a25
Step 15/15 : CMD ["/usr/sbin/sshd", "-D"]
---> Running in e349abf8e234
Removing intermediate container e349abf8e234
---> 2f24e301bb78
Successfully built 2f24e301bb78
Successfully tagged verizon/ansible-centos:latest
root@ubuntu:~/verizon/Day3/centos-sshd-passwordless# docker images | grep verizon
verizon/ansible-centos latest 2f24e301bb78 5 minutes ago 285MB
verizon/ansible-ubuntu latest 5b27e1379914 25 hours ago 234MB
root@ubuntu:~/verizon/Day3/centos-sshd-passwordless#
Now we are going to create centos containers
--------------------------------------------
root@ubuntu:~/verizon/Day3/centos-sshd-passwordless# docker run -d --name centos1 --hostname centos1 -p 3000:22 -p 9000:80 verizon/ansible-centos
39f0807f0d58a3d18cdd250fb8a4844c635c507213e0c1ded538299c0aab809b
root@ubuntu:~/verizon/Day3/centos-sshd-passwordless# docker run -d --name centos2 --hostname centos2 -p 3001:22 -p 9001:80 verizon/ansible-centos
e82a52a605674d5c245676664c442d169d2da68da027b8a0535cb38ebd11d5ab
root@ubuntu:~/verizon/Day3/centos-sshd-passwordless# docker run -d --name centos3 --hostname centos3 -p 3002:22 -p 9002:80 verizon/ansible-centos
6e2ad652fcca242284a0d7c9200939210a1c2a37aeb247e208256bf06226fc10
root@ubuntu:~/verizon/Day3/centos-sshd-passwordless#
Note: We dont want to specify any bash -d as we have done it at image level using CMD ["/usr/sbin/sshd", "-D"] at Dockerfile.
Quick check to see if the containers are getting worked with ssh
------------------------------------
root@ubuntu:~/verizon/Day3/centos-sshd-passwordless# ssh -p 3000 root@localhost
The authenticity of host '[localhost]:3000 ([127.0.0.1]:3000)' can't be established.
RSA key fingerprint is SHA256:SISWSG675W77tIuIva/azCqLOergGdbgYctBePrcdMg.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:3000' (RSA) to the list of known hosts.
[root@centos1 ~]# exit
logout
Connection to localhost closed.
root@ubuntu:~/verizon/Day3/centos-sshd-passwordless# ssh -p 3001 root@localhost
The authenticity of host '[localhost]:3001 ([127.0.0.1]:3001)' can't be established.
RSA key fingerprint is SHA256:SISWSG675W77tIuIva/azCqLOergGdbgYctBePrcdMg.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:3001' (RSA) to the list of known hosts.
[root@centos2 ~]# exit
logout
Connection to localhost closed.
root@ubuntu:~/verizon/Day3/centos-sshd-passwordless# ssh -p 3002 root@localhost
The authenticity of host '[localhost]:3002 ([127.0.0.1]:3002)' can't be established.
RSA key fingerprint is SHA256:SISWSG675W77tIuIva/azCqLOergGdbgYctBePrcdMg.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[localhost]:3002' (RSA) to the list of known hosts.
[root@centos3 ~]# exit
logout
Connection to localhost closed.
Now edit the hosts file
--------------------
root@ubuntu:~/verizon/Day3# cat hosts
[all]
ubuntu[1:3]
centos[1:3]
[dev]
ubuntu1
centos1
[qa]
ubuntu[1:2]
centos[1:2]
[prod]
ubuntu3
centos3
Make ready of the host_vars file (just add the port numbers)
root@ubuntu:~/verizon/Day3# cd host_vars/
root@ubuntu:~/verizon/Day3/host_vars# echo "ansible_port: 3000" > centos1
root@ubuntu:~/verizon/Day3/host_vars# echo "ansible_port: 3001" > centos2
root@ubuntu:~/verizon/Day3/host_vars# echo "ansible_port: 3002" > centos3
root@ubuntu:~/verizon/Day3/host_vars#
Final structure
---------------
root@ubuntu:~/verizon/Day3# tree
.
├── ansible.cfg
├── centos-sshd-passwordless
│ ├── ansible.cfg
│ ├── authorized_keys
│ ├── Dockerfile
│ ├── installapache.yml
│ ├── inventory
│ ├── myproject.log
│ ├── sshd_config
│ └── sudoers
├── group_vars
│ └── all
├── hosts
└── host_vars
├── centos1
├── centos2
├── centos3
├── ubuntu1
├── ubuntu2
└── ubuntu3
3 directories, 17 files
create a install file for installing nginx in both ubuntu and centos
----------------------------------------------------
root@ubuntu:~/verizon/Day3# cat installnginx.yml
- name: Demo for conditional installation - installing,config and deployment of webpage to nginx web server
hosts: all
vars:
- greeting_msg: Devops
- provitioner_tool: Docker
- automation_tool: Ansible
tasks:
#This is a comment
- name: Install nginx web server in Ubuntu
when: (ansible_distribution == "Ubuntu" and ansible_os_family == "Debian")
apt:
name: nginx
state: latest
update_cache: yes
- name: Install EPEL (Extra Package for Enterprise Linux) in CentOS
when: (ansible_distribution == "CentOS" and ansible_os_family == "RedHat")
yum: name=epel-release state=latest
- name: Install nginx web server in CentOS
when: (ansible_distribution == "CentOS" and ansible_os_family == "RedHat")
yum:
name: nginx
state: latest
#update_cache: yes
- name: Configure nginx web root folder in Ubuntu
when: (ansible_distribution == "Ubuntu" and ansible_os_family == "Debian")
copy: src=default dest=/etc/nginx/sites-available/default
- name: Configure nginx web root folder in CentOS
when: (ansible_distribution == "CentOS" and ansible_os_family == "RedHat")
copy: src=default.conf dest=/etc/nginx/conf.d/default.conf
- name: Restart the nginx server
command: service nginx restart
- name: Create the web root folder
file: path=/var/html state=directory mode=0777
- name: Deploy a web page into nginx web server
template: src=index.html.j2 dest=/var/html/index.html
#when: (ansible_distribution == "Ubuntu" and ansible_os_family == "Debian")
root@ubuntu:~/verizon/Day3#
root@ubuntu:~/verizon/Day3# vi installnginx.yml
root@ubuntu:~/verizon/Day3#
Run the playbook
-------------------
root@ubuntu:~/verizon/Day3# ansible-playbook -i hosts installnginx.yml
PLAY [Demo for conditional installation - installing,config and deployment of webpage to nginx web server] ***********************************
TASK [Gathering Facts] ***********************************************************************************************************************
[WARNING]: sftp transfer mechanism failed on [localhost]. Use ANSIBLE_DEBUG=1 to see detailed information
[WARNING]: sftp transfer mechanism failed on [localhost]. Use ANSIBLE_DEBUG=1 to see detailed information
ok: [ubuntu3]
ok: [ubuntu1]
ok: [ubuntu2]
[WARNING]: sftp transfer mechanism failed on [localhost]. Use ANSIBLE_DEBUG=1 to see detailed information
ok: [centos1]
ok: [centos3]
ok: [centos2]
TASK [Install nginx web server in Ubuntu] ****************************************************************************************************
skipping: [centos3]
skipping: [centos1]
skipping: [centos2]
ok: [ubuntu3]
ok: [ubuntu1]
ok: [ubuntu2]
TASK [Install EPEL (Extra Package for Enterprise Linux) in CentOS] ***************************************************************************
skipping: [ubuntu3]
skipping: [ubuntu1]
skipping: [ubuntu2]
[WARNING]: sftp transfer mechanism failed on [localhost]. Use ANSIBLE_DEBUG=1 to see detailed information
[WARNING]: sftp transfer mechanism failed on [localhost]. Use ANSIBLE_DEBUG=1 to see detailed information
[WARNING]: sftp transfer mechanism failed on [localhost]. Use ANSIBLE_DEBUG=1 to see detailed information
ok: [centos1]
ok: [centos3]
ok: [centos2]
TASK [Install nginx web server in CentOS] ****************************************************************************************************
skipping: [ubuntu3]
skipping: [ubuntu1]
skipping: [ubuntu2]
[WARNING]: sftp transfer mechanism failed on [localhost]. Use ANSIBLE_DEBUG=1 to see detailed information
[WARNING]: sftp transfer mechanism failed on [localhost]. Use ANSIBLE_DEBUG=1 to see detailed information
[WARNING]: sftp transfer mechanism failed on [localhost]. Use ANSIBLE_DEBUG=1 to see detailed information
changed: [centos1]
changed: [centos3]
changed: [centos2]
TASK [Configure nginx web root folder in Ubuntu] *********************************************************************************************
skipping: [centos3]
skipping: [centos1]
skipping: [centos2]
ok: [ubuntu1]
ok: [ubuntu2]
ok: [ubuntu3]
TASK [Configure nginx web root folder in CentOS] *********************************************************************************************
skipping: [ubuntu3]
skipping: [ubuntu1]
skipping: [ubuntu2]
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: /root/verizon/Day3/default.conf
fatal: [centos3]: FAILED! => {"changed": false, "msg": "Could not find or access 'default.conf'\nSearched in:\n\t/root/verizon/Day3/files/default.conf\n\t/root/verizon/Day3/default.conf\n\t/root/verizon/Day3/files/default.conf\n\t/root/verizon/Day3/default.conf"}
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: /root/verizon/Day3/default.conf
fatal: [centos1]: FAILED! => {"changed": false, "msg": "Could not find or access 'default.conf'\nSearched in:\n\t/root/verizon/Day3/files/default.conf\n\t/root/verizon/Day3/default.conf\n\t/root/verizon/Day3/files/default.conf\n\t/root/verizon/Day3/default.conf"}
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: /root/verizon/Day3/default.conf
fatal: [centos2]: FAILED! => {"changed": false, "msg": "Could not find or access 'default.conf'\nSearched in:\n\t/root/verizon/Day3/files/default.conf\n\t/root/verizon/Day3/default.conf\n\t/root/verizon/Day3/files/default.conf\n\t/root/verizon/Day3/default.conf"}
TASK [Restart the nginx server] **************************************************************************************************************
[WARNING]: Consider using the service module rather than running service. If you need to use command because service is insufficient you
can add warn=False to this command task or set command_warnings=False in ansible.cfg to get rid of this message.
changed: [ubuntu1]
changed: [ubuntu2]
changed: [ubuntu3]
TASK [Deploy a web page into nginx web server] ***********************************************************************************************
ok: [ubuntu3]
ok: [ubuntu1]
ok: [ubuntu2]
to retry, use: --limit @/root/verizon/Day3/installnginx.retry
PLAY RECAP ***********************************************************************************************************************************
centos1 : ok=3 changed=1 unreachable=0 failed=1
centos2 : ok=3 changed=1 unreachable=0 failed=1
centos3 : ok=3 changed=1 unreachable=0 failed=1
ubuntu1 : ok=5 changed=1 unreachable=0 failed=0
ubuntu2 : ok=5 changed=1 unreachable=0 failed=0
ubuntu3 : ok=5 changed=1 unreachable=0 failed=0
The error is expected as we have to copy the default.conf frile from installed nginx centos machine
--------------------------
root@ubuntu:~/verizon/Day3# docker cp centos1:/etc/nginx/conf.d/default.conf .
Now make the below changes to default.conf (root /var/html;)
-------------------------
root@ubuntu:~/verizon/Day3# cat default.conf
#
# The default server
#
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
Now create the index.html.js file to current working dir,
---------------------------------------------------
root@ubuntu:~/verizon/Day3# cat index.html.j2
<html>
<title>Welcome to {{greeting_msg}}</title>
<body>
<h1>Provitioner Tool used is {{provitioner_tool}}</h1>
<h1>Automation Tool used is {{automation_tool}}</h1>
</body>
</html>
Also to ignore the ssh warning message modify the amsible.cfg
----------------------------------
root@ubuntu:~/verizon/Day3# cat ansible.cfg
[defaults]
inventory=hosts
[ssh_connection]
scp_if_ssh = True
Now re-run the playbook with retry option to run the failed commands
-----------------------------------------------------------
root@ubuntu:~/verizon/Day3# ansible-playbook installnginx.yml --limit @/root/verizon/Day3/installnginx.retry
PLAY [Demo for conditional installation - installing,config and deployment of webpage to nginx web server] ***********************************
TASK [Gathering Facts] ***********************************************************************************************************************
ok: [centos1]
ok: [centos3]
ok: [centos2]
TASK [Install nginx web server in Ubuntu] ****************************************************************************************************
skipping: [centos3]
skipping: [centos1]
skipping: [centos2]
TASK [Install EPEL (Extra Package for Enterprise Linux) in CentOS] ***************************************************************************
ok: [centos1]
ok: [centos2]
ok: [centos3]
TASK [Install nginx web server in CentOS] ****************************************************************************************************
ok: [centos2]
ok: [centos3]
ok: [centos1]
TASK [Configure nginx web root folder in Ubuntu] *********************************************************************************************
skipping: [centos3]
skipping: [centos1]
skipping: [centos2]
TASK [Configure nginx web root folder in CentOS] *********************************************************************************************
ok: [centos2]
ok: [centos1]
ok: [centos3]
TASK [Restart the nginx server] **************************************************************************************************************
[WARNING]: Consider using the service module rather than running service. If you need to use command because service is insufficient you
can add warn=False to this command task or set command_warnings=False in ansible.cfg to get rid of this message.
changed: [centos3]
changed: [centos1]
changed: [centos2]
TASK [Create the web root folder] ************************************************************************************************************
changed: [centos3]
changed: [centos1]
changed: [centos2]
TASK [Deploy a web page into nginx web server] ***********************************************************************************************
changed: [centos1]
changed: [centos3]
changed: [centos2]
PLAY RECAP ***********************************************************************************************************************************
centos1 : ok=7 changed=3 unreachable=0 failed=0
centos2 : ok=7 changed=3 unreachable=0 failed=0
centos3 : ok=7 changed=3 unreachable=0 failed=0
root@ubuntu:~/verizon/Day3# curl http://localhost:9000
<html>
<title>Welcome to Devops</title>
<body>
<h1>Provitioner Tool used is Docker</h1>
<h1>Automation Tool used is Ansible</h1>
</body>
</html>
root@ubuntu:~/verizon/Day3# curl http://localhost:9001
<html>
<title>Welcome to Devops</title>
<body>
<h1>Provitioner Tool used is Docker</h1>
<h1>Automation Tool used is Ansible</h1>
</body>
</html>
root@ubuntu:~/verizon/Day3# curl http://localhost:9002
<html>
<title>Welcome to Devops</title>
<body>
<h1>Provitioner Tool used is Docker</h1>
<h1>Automation Tool used is Ansible</h1>
</body>
</html>
root@ubuntu:~/verizon/Day3# curl http://localhost:8000
<html>
<title>Welcome to Devops</title>
<body>
<h1>Provitioner Tool used is Docker</h1>
<h1>Automation Tool used is Ansible</h1>
</body>
</html>
root@ubuntu:~/verizon/Day3# curl http://localhost:8001
<html>
<title>Welcome to Devops</title>
<body>
<h1>Provitioner Tool used is Docker</h1>
<h1>Automation Tool used is Ansible</h1>
</body>
</html>
root@ubuntu:~/verizon/Day3# curl http://localhost:8002
<html>
<title>Welcome to Devops</title>
<body>
<h1>Provitioner Tool used is Docker</h1>
<h1>Automation Tool used is Ansible</h1>
</body>
</html>
Final tree structure
---------------------
root@ubuntu:~/verizon/Day3# tree
.
├── ansible.cfg
├── centos-sshd-passwordless
│ ├── ansible.cfg
│ ├── authorized_keys
│ ├── Dockerfile
│ ├── installapache.yml
│ ├── inventory
│ ├── myproject.log
│ ├── sshd_config
│ └── sudoers
├── default
├── default.conf
├── group_vars
│ └── all
├── hosts
├── host_vars
│ ├── centos1
│ ├── centos2
│ ├── centos3
│ ├── ubuntu1
│ ├── ubuntu2
│ └── ubuntu3
├── index.html.j2
├── installnginx.retry
└── installnginx.yml
3 directories, 22 files
Handlers
-----------
Used to notify when a activity happens., here we need the nginx server to restart only if the default files are copied. To check this modify the default and default.conf files in loacal.
root@ubuntu:~/verizon/Day3# cat installnginx.yml
- name: Demo for conditional installation - installing,config and deployment of webpage to nginx web server
hosts: all
vars:
- greeting_msg: Devops
- provitioner_tool: Docker
- automation_tool: Ansible
tasks:
#This is a comment
- name: Install nginx web server in Ubuntu
when: (ansible_distribution == "Ubuntu" and ansible_os_family == "Debian")
apt:
name: nginx
state: latest
update_cache: yes
- name: Install EPEL (Extra Package for Enterprise Linux) in CentOS
when: (ansible_distribution == "CentOS" and ansible_os_family == "RedHat")
yum: name=epel-release state=latest
- name: Install nginx web server in CentOS
when: (ansible_distribution == "CentOS" and ansible_os_family == "RedHat")
yum:
name: nginx
state: latest
#update_cache: yes
- name: Configure nginx web root folder in Ubuntu
when: (ansible_distribution == "Ubuntu" and ansible_os_family == "Debian")
copy: src=default dest=/etc/nginx/sites-available/default
notify: Restart the nginx server
- name: Configure nginx web root folder in CentOS
when: (ansible_distribution == "CentOS" and ansible_os_family == "RedHat")
copy: src=default.conf dest=/etc/nginx/conf.d/default.conf
notify: Restart the nginx server
handlers:
- name: Restart the nginx server
command: service nginx restart
- name: Create the web root folder
file: path=/var/html state=directory mode=0777
- name: Deploy a web page into nginx web server
template: src=index.html.j2 dest=/var/html/index.html
#when: (ansible_distribution == "Ubuntu" and ansible_os_family == "Debian")
root@ubuntu:~/verizon/Day3#
Execute teh playbook now,
-------------------------
root@ubuntu:~/verizon/Day3# ansible-playbook installnginx.yml
PLAY [Demo for conditional installation - installing,config and deployment of webpage to nginx web server] ***********************************
TASK [Gathering Facts] ***********************************************************************************************************************
ok: [ubuntu2]
ok: [ubuntu3]
ok: [ubuntu1]
ok: [centos1]
ok: [centos3]
ok: [centos2]
TASK [Install nginx web server in Ubuntu] ****************************************************************************************************
skipping: [centos3]
skipping: [centos1]
skipping: [centos2]
ok: [ubuntu1]
ok: [ubuntu2]
ok: [ubuntu3]
TASK [Install EPEL (Extra Package for Enterprise Linux) in CentOS] ***************************************************************************
skipping: [ubuntu3]
skipping: [ubuntu1]
skipping: [ubuntu2]
ok: [centos3]
ok: [centos1]
ok: [centos2]
TASK [Install nginx web server in CentOS] ****************************************************************************************************
skipping: [ubuntu3]
skipping: [ubuntu1]
skipping: [ubuntu2]
ok: [centos3]
ok: [centos1]
ok: [centos2]
TASK [Configure nginx web root folder in Ubuntu] *********************************************************************************************
skipping: [centos3]
skipping: [centos1]
skipping: [centos2]
changed: [ubuntu1]
changed: [ubuntu2]
changed: [ubuntu3]
TASK [Configure nginx web root folder in CentOS] *********************************************************************************************
skipping: [ubuntu3]
skipping: [ubuntu1]
skipping: [ubuntu2]
changed: [centos3]
changed: [centos1]
changed: [centos2]
RUNNING HANDLER [Restart the nginx server] ***************************************************************************************************
[WARNING]: Consider using the service module rather than running service. If you need to use command because service is insufficient you
can add warn=False to this command task or set command_warnings=False in ansible.cfg to get rid of this message.
changed: [centos3]
changed: [centos1]
changed: [ubuntu1]
changed: [ubuntu2]
changed: [centos2]
changed: [ubuntu3]
PLAY RECAP ***********************************************************************************************************************************
centos1 : ok=5 changed=2 unreachable=0 failed=0
centos2 : ok=5 changed=2 unreachable=0 failed=0
centos3 : ok=5 changed=2 unreachable=0 failed=0
ubuntu1 : ok=4 changed=2 unreachable=0 failed=0
ubuntu2 : ok=4 changed=2 unreachable=0 failed=0
ubuntu3 : ok=4 changed=2 unreachable=0 failed=0
List module
-------------------
root@ubuntu:~/verizon/Day3# cat list.yml
- name: Dem use of list var and its navigation as a loop
hosts: localhost
gather_facts: false
vars:
- softwares_tools:
- tree
- vim
- default-jdk
- maven
tasks:
- name: Print all the list of software tools
debug: msg = "{{item}}"
with_list: "{{softwares_tools}}"
Run the playbook
---------------------
root@ubuntu:~/verizon/Day3# ansible-playbook list.yml
PLAY [Dem use of list var and its navigation as a loop] **************************************************************************************
TASK [Print all the list of software tools] **************************************************************************************************
ok: [localhost] => (item=None) => {
"msg": "Hello world!"
}
ok: [localhost] => (item=None) => {
"msg": "Hello world!"
}
ok: [localhost] => (item=None) => {
"msg": "Hello world!"
}
ok: [localhost] => (item=None) => {
"msg": "Hello world!"
}
PLAY RECAP ***********************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0