-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathAnsibleNotes_LMS.txt
More file actions
260 lines (169 loc) · 4.79 KB
/
AnsibleNotes_LMS.txt
File metadata and controls
260 lines (169 loc) · 4.79 KB
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
Ansible Notes
Create 2 instances and start them
give them instance names to have
*ansible-controller-machine* &
*ansible-host-machine*
Set up on ACM:
********************************
Step1:
**************
go to vim /etc/ssh/sshd_config
i
scroll down
remove the # for password Authentication Yes
Add # for password Authentication No
:wq!
Step 2:
**************
Create password for ec2-user
# passwd ec2-user
Newpassword:
ConfirmPassword:
********************
Step 3:
***********
Add ec2-user in sudoers files and give all permission
# vim /etc/sudoers
i
scroll down
under root add like this
ec2-user ALL=NOPASSWD: ALL
**************************
Step 4:
Restart your sshd connection
# systemctl restart sshd
*******************
Repeat all the 4 steps on Host machine also and restart the sshd connection on host machine.
************************
Install Ansible on *ansible-controller-machine* ONLY:
Step 1:
*************
# yum list | grep ansible // you will not find ansible package as default in yum
So install epel replositories and it will ass ansible to yum repository
# yum install epel-release
// copy the command sudo amazon-linux-extras install epel and execute it
# sudo amazon-linux-extras install epel
give y
Complete!
You will ansible package is now available with yum
# yum list | grep ansible
//ansible will be available (wait for few seconds)
# yum install ansible
press y
again press y
Complete!
# ansible --version
version of ansible isntalled will be displayed.
*****************************************
CREATE INVENTORY ON ACM and ADD Host details
*****************************************
# vim /etc/ansible/hosts
Scroll down to end and add
[webservers]
paste private ip of host ==> 172.31.39.169
(may have to use the 'Private IP DNS name (IPv4 only)' instead)
:wq!
***********************************************
OPEN SECURE SHELL CONNECTION
***********************************************
Step 1: change to ec2-user
# su - ec2-user
# ssh-keygen
press enter
press enter
press enter
ssh key will be generated
Step 2: copy key on to the host
# ssh-copy-id -i ec2-user@private ip of host
(may have to use the 'Private IP DNS name (IPv4 only)' instead)
press yes
enter passowrd ==>
ssh connection will be done
In the message we will have a command to log into the host from ACM, execute it
# ssh 'ec2-user@172.31.35.169'
(may have to use the 'Private IP DNS name (IPv4 only)' instead)
we will be connected to the host
exit
back to ACM
*****************************************
Modules in ANSIBLE
1. adhoc ping command
# ansible -m ping webservers
response will be a pong from all hosts under webservers
Module2 : command
# ansible -m command -a "uptime" webservers
will give the uptime of hosts in webservers
To check all modules in ansible
# ansible-doc -l
use up arrow key and down arrow key to scroll up and down.
press q to exit
********************************
Playbook2 -- For TomCat
# sudo vim playbook2.yml
i
---
- hosts: webservers
become: true
become_user: root
tasks:
- name: install tomcat
yum: name=tomcat state=present
- name: start tomcat
service: name=tomcat state=started
- name: deploy war file
get_url:
url=https://tomcat.apache.org/tomcat-7.0-doc/appdev/sample/sample.war
dest=/usr/share/tomcat/webapps
notify: restart tomcat
handlers:
- name: restart tomcat
service: name=tomcat state=restarted
Syntax check:
# ansible-playbook playbook2.yml --syntax-check
# ansible-playbook playbook2.yml
********************************************
playbook.yml (httpd) and deploy a html page
# sudo vim playbook.yml
i
---
- hosts: webservers
become: true
become_user: root
tasks:
- name: install httpd
yum: name=httpd state=present
- name: start httpd
service: name=httpd state=started
- name: deploy html file
copy: src=/etc/ansible/index.html dest=/var/www/html
notify: restart httpd
handlers:
- name: restart httpd
service: name=httpd state=restarted
:wq!
Create file:
/etc/ansible/index.html
Check syntax
# ansible-playbook playbook.yml --syntax-check
Now run the playbook
# ansible-playbook playbook.yml
*************************************
docker deployment
---
- hosts: webservers
become: true
become_user: root
tasks:
- name: install docker
yum: name=docker state=present
- name: start docker
service: name=docker state=started
- name: install git
yum: name=git state=present
- name: get the dockerfile from githib
git: repo=https://github.com/Sonal0409/dockerdemo.git dest=/tmp/gitrepo
- name: build dockerfile
command: chdir=/tmp/gitrepo docker build -t deployimage:ansible .
- name: run the dockerfile
command: docker run -itd -P deployimage:ansible
*****************************************