-
Notifications
You must be signed in to change notification settings - Fork 21
/
session9sl.txt
332 lines (241 loc) · 8.24 KB
/
session9sl.txt
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
#create 3 ec2 amazon linux vm , 1 Master and 2 Worker Nodes and install ansible
#connect to the servers
#switch to root
#update serverpackages
yum update -y
#5. Install the ansible
It we install Ansible we need to follow below pre-requisites.
#Download epel repository.
wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
#Verify epel repository
ls
#Install epel repository.
yum install epel-release-latest-7.noarch.rpm
#Update epel repository.
yum update -y
#Install all individual packages inside the repository.
yum install git python python-devel python-pip openssl ansible -y
#Chek the Ansible version
ansible --version
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#create a group add the private ip of the nodes run in master only
vi /etc/ansible/hosts
[demo]
<privateip node1>
<privateip node2>
:wq to save
#create a demo group run in master only
[demo]
<privateipnode1>
<privateipnode2>
:wq
#only in master
vi /etc/ansible/ansible.cfg
Remove # from inventory
Remove # from sudo su or sudo_user = root
esc :wq
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#create user in master and nodes
adduser ansible
#set password
passwd ansible
#login to the created user
su - ansible
####add ansible to the sudoers file
#switch to ansible user
su - ansible
#can this user create a file
#can this user download the s/w
#come out of ansible and move to root
exit
#sudoers file should be edited to add the users to give the user root similar access
visudo : it will let us enter sudoers file or vi /etc/sudoers
#add the user and come to bottam
<user> ALL=(ALL) NOPASSWD:ALL
esc :wq
#now try to download a s/w
sudo yum install httpd -y
#same process should be done for node1 and node2
#try to ssh node1/node2 from ansible user. you will get permission denied
ssh <privateip>
######provide permission in both master and nodes
#move to root
exit
vi /etc/ssh/sshd_config
remove # uncomment
PermitRootLogin yes
PasswordAuthentication yes
put # before comment out
PasswordAuthentication no
# restart the sshd servce
service sshd restart
#try to ssh node user <user>
ssh <node private ip>
create file it will show
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# create a ssh key at master and copy the same to nodes to do passwordless authentication
#Generate the Key in the master node
#generate without passphrase
ssh-keygen
#check if key got generated
cd ~/.ssh/
#copy the key to node server
ssh-copy-id user@privateip
[ansible@ip-172-31-57-116 .ssh]$ ssh-copy-id [email protected]
#Try to ssh now without password from ansible user , it will not work with other user
ssh <private-ip>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#List The all hosts adhoc command
ansible all --list-hosts
ansible demo --list-hosts
ansible demo[1] --list-hosts
#if you wanted to get last node of the group
ansible demo[-1] --list-hosts
#if you wanted to get last node of the all the groups
[root@ip-172-31-57-116 ~]# ansible all[-1] --list-hosts
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
what is adhoc command
what is module
what is playbook
++++++++++++++++++++++++ Exercise with Adhoc+++++++++++++++++++++++++++++++++
#usecase 1 , I will create manually files in nodes ansible users , I want list them using adhoc ansible command from the master
ansible <groupname> -a"linux command" -----> -a stands for arguments
ansible demo -a"ls"
#usecase 2 , If i want to list the files in node1
ansible demo[1] -a"ls"
#usecase 3 install httpd in nodes in nodes
ansible demo -a"yum install httpd -y"
ansible demo -a"sudo yum install httpd -y"
ansible demo -a"sudo yum remove httpd -y"
#create a file in nodes
ansible demo -a"touch niladri"
#create a directory in node1
ansible demo[1] -a"mkdir nil"
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#install docker using playbok #you need to understand
[ansible@ip-172-31-57-116 ~]$ vi myfirstplaybook.yaml
--- #our first playbook to install the docker
- hosts: demo
user: ansible
become: yes
connection: ssh
tasks:
- name: install docker on nodes
action: yum name=docker state=present
[ansible@ip-172-31-57-116 ~]$ ansible-playbook myfirstplaybook.yaml
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#Remove docker and install httpd using playbok #you need to understand
[ansible@ip-172-31-57-116 ~]$ cat myfirstplaybook.yaml
--- #our first playbook to install the docker
- hosts: demo
user: ansible
become: yes
connection: ssh
tasks:
- name: install docker on nodes
action: yum name=docker state=absent
- name: install httpd
action: yum name=httpd state=present
[ansible@ip-172-31-57-116 ~]$
+++++++++++++++++++++++++++++++++++++++++
#create a file in master and copy the same to node using play book
touch index.html
[ansible@ip-172-31-57-116 ~]$ cat myfirstplaybook.yaml
--- #our first playbook to install the docker
- hosts: demo
user: ansible
become: yes
connection: ssh
tasks:
- name: install docker on nodes
action: yum name=docker state=absent
- name: install httpd
action: yum name=httpd state=present
- name: copy index.html
ansible.builtin.copy:
src: /home/ansible/index.html
dest: /home/ansible/
[ansible@ip-172-31-57-116 ~]$
[ansible@ip-172-31-57-116 ~]$ cat myfirstplaybook.yaml
--- #our first playbook to install the docker
- hosts: demo
user: ansible
become: yes
connection: ssh
tasks:
- name: install docker on nodes
action: yum name=docker state=absent
- name: install httpd
action: yum name=httpd state=present
- name: copy index.html
ansible.builtin.copy:
src: /home/ansible/index.html
dest: /home/ansible/
- name: create directory
action: file name=niladridir state=directory
[ansible@ip-172-31-57-116 ~]$
+++++++++++++++++++++++++++++++
####run a module, using a module install docker
ansiable demo -b -m yum -a "pkg=docker state=present"
++++++++++++++++++++++++++++++++++++++++++
#host an application on the httpd server using handlers
[ansible@ip-172-31-57-116 ~]$ cat httpdserver.yaml
--- #Myweb For handlers
- hosts: demo #your host
user: ansible #user
become: yes #if you want to use root priviledge to perform some actions
connection: ssh #connect type
tasks:
- name: install httpd
action: yum name=httpd state=present
notify: restart httpd
- name: copy index.html
ansible.builtin.copy:
src: /home/ansible/index.html
dest: /var/www/html/
handlers:
- name: restart httpd
action: service name=httpd state=restarted
[ansible@ip-172-31-57-116 ~]$
+++++++++++++++++++++++++++++++
Simplilearn script
ansible -m shell -a "service apache2 status" webservers
ansible -m setup webservers
ansible webservers -m shell -a 'hostname'
ansible webservers -m apt -a 'name=git state=present' --become
ansible webservers -m file -a 'dest=/root/sample.txt state=touch mode=600 owner=root group=root' --become
+++++++++++++++++++++++++++++++++++++++++
mkdir terraformsw
wget https://releases.hashicorp.com/terraform/0.14.9/terraform_0.14.9_linux_amd64.zip
unzip terraform_0.14.9_linux_amd64.zip
mv terraform /usr/local/bin/
terraform --version
++++++++++++++++++++++++++++++++
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install terraform
++++++++++++++++++++++++++++++++++++
++++++++++++++++++++
mkdir s3back
cd s3back
nano creds.tf
provider "aws" {
access_key = ""
secret_key = ""
token = ""
region = "us-east-1"
}
#
nano main.tf
resource "aws_s3_bucket" "b" {
bucket = "niladri-test-bucket-1234"
acl = "private"
tags = {
Name = "My bucket"
Environment = "Dev"
}
}
terraform init
terraform plan
terraform apply
+++++++++++++++++++++++++++++++