-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScripts.txt
182 lines (161 loc) · 3.83 KB
/
Scripts.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
==================================
SCRIPT NAME: test_facts.yml
==================================
--- # Gather EC2 Facts from my remote instance(s)
- hosts: aws
remote_user: ec2-user
become: yes
connection: ssh
gather_facts: yes
tasks:
- name: Gather the EC2 Facts about running instance(s)
ec2_facts:
register: all_facts
- name: Display the facts
debug: var=all_facts
execute using:
ansible-playbook test_facts.yml
2>
==================================
SCRIPT NAME: awsshell.yml
==================================
--- # AWS EC2 Instance Communication Shell Command Example
- hosts: aws
remote_user: ec2-user
become_method: sudo
gather_facts: yes
connection: ssh
tasks:
- name: Execute a Shell Command for Listing the Directory
shell: ls -al ~
register: result
- name: Display the results in JSON format
debug: var=result
ansible-playbook awsshell.yml
3>create aws key pair:
awsec2key_create.yml
- hosts: localhost
connection: local
remote_user: admin
become: yes
gather_facts: no
vars_files:
- files/awscreds.yml
tasks:
- name: Create a New Key Pair - MyKeyPairTestAWS
ec2_key:
aws_access_key: "{{ aws_id }}"
aws_secret_key: "{{ aws_key }}"
name: MyKeyPairTestAWS
region: us-east-2
state: present
4>remove aws key pair:
awsec2key_remove.yml
- hosts: localhost
connection: local
remote_user: admin
become: yes
gather_facts: no
vars_files:
- files/awscreds.yml
tasks:
- name: Create a New Key Pair - MyKeyPairTestAWS
ec2_key:
aws_access_key: "{{ aws_id }}"
aws_secret_key: "{{ aws_key }}"
name: MyKeyPairTestAWS
region: us-east-2
state: absent
5> Create AWS VPC:
awsec2key_create.yml
- hosts: localhost
connection: local
remote_user: admin
become: yes
gather_facts: no
vars_files:
- files/awscreds.yml
tasks:
- name: Create a New VPC Called MyNewVPC
ec2_vpc_net:
aws_access_key: "{{ aws_id }}"
aws_secret_key: "{{ aws_key }}"
region: "{{ aws_region }}"
name: MyNewVPC
state: present
cidr_block: 172.17.1.0/24
register: vpcnetblock
- name: Print the resulting JSON output
debug: var=vpcnetblock
6>
AWS START/STOP/TERMINATE EC2 INSTANCE
To stop ec2 instance
- hosts: localhost
connection: local
remote_user: admin
become: yes
gather_facts: no
vars_files:
- files/awscreds.yml
tasks:
- name: Manage the state of an instance
ec2:
aws_access_key: "{{ aws_id }}"
aws_secret_key: "{{ aws_key }}"
region: "{{ aws_region }}"
instance_ids: i-04e61e270251a6233
state: stopped
7>
To start EC2 Instance
- hosts: localhost
connection: local
remote_user: admin
become: yes
gather_facts: no
vars_files:
- files/awscreds.yml
tasks:
- name: Manage the state of an instance
ec2:
aws_access_key: "{{ aws_id }}"
aws_secret_key: "{{ aws_key }}"
region: "{{ aws_region }}"
instance_ids: i-04e61e270251a6233
state: running
8>
To Terminate EC2 Instance
- hosts: localhost
connection: local
remote_user: admin
become: yes
gather_facts: no
vars_files:
- files/awscreds.yml
tasks:
- name: Manage the state of an instance
ec2:
aws_access_key: "{{ aws_id }}"
aws_secret_key: "{{ aws_key }}"
region: "{{ aws_region }}"
instance_ids: i-04e61e270251a6233
state: absent
9>
To Provision new ec2 instance
--- # EC2 MODULE PROVISIONING EXAMPLE
- hosts: localhost
connection: local
remote_user: admin
become: yes
gather_facts: no
vars_files:
- files/awscreds.yml
tasks:
- name: Basic provisioning of two t2.micro EC2 instances
ec2:
aws_access_key: "{{ aws_id }}"
aws_secret_key: "{{ aws_key }}"
region: "{{ aws_region }}"
image: ami-25615740
instance_type: t2.micro
count: 1
if count is 2 it will create 2 instance