chrisluiz16

aws provision

Jul 3rd, 2023
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.49 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. ####---------------------------------- Tasks --------------------####
  6.  
  7.  
  8. #### Full playbook ####
  9.  
  10.  
  11.  
  12. ---
  13.  
  14. - name: "Creating the zomato webserver infra"
  15. hosts: localhost
  16. become: true
  17. vars:
  18. region: ap-south-1
  19. project_name: zomato
  20. project_env: dev
  21. region: ap-south-1
  22. state: present
  23. state_instance: present
  24. tags:
  25. - never
  26. - infra
  27. tasks:
  28. - name: "Install amazon.aws collection"
  29. community.general.ansible_galaxy_install:
  30. type: collection
  31. name: amazon.aws
  32. tags:
  33. - requirements
  34. - name: "Creating a new ssh keypair"
  35. become: false
  36. community.crypto.openssh_keypair:
  37. path: "{{ project_name }}-{{ project_env }}"
  38. passphrase: ""
  39. - name: "Installing python3-pip in localhost"
  40. yum:
  41. name: python3-pip
  42. state: latest
  43. tags:
  44. - requirements
  45. - name: "Installing pip3 modules for amazon.aws"
  46. pip:
  47. name: "{{ item }}"
  48. with_items:
  49. - boto3
  50. - botocore
  51. tags:
  52. - requirements
  53. - name: "creating a new EC2 key pair using lookup function"
  54. become: false
  55. amazon.aws.ec2_key:
  56. region: "{{ region }}"
  57. name: "{{ project_name }}-{{ project_env }}"
  58. key_material: "{{ lookup('file','{{ project_name }}-{{ project_env }}.pub') }}"
  59. state: "{{ state }}"
  60. - name: "Creating a Second key pair with default values"
  61. amazon.aws.ec2_key:
  62. region: "{{ region }}"
  63. name: "{{ project_name }}"
  64. state: "{{ state }}"
  65. register: key_status
  66. - name: "Printing the private_key value"
  67. when: key_status.changed == true
  68. debug:
  69. var: key_status
  70. - name: "Copying the private key to the current location"
  71. become: false
  72. when: key_status.changed == true
  73. copy:
  74. content: "{{ key_status.key.private_key }}"
  75. dest: "{{ project_name }}.pem"
  76. mode: 0400
  77. - name: "Create a security group name {{ project_name }}-{{ project_env }}"
  78. amazon.aws.ec2_security_group:
  79. name: "{{ project_name }}-{{ project_env }}"
  80. description: "{{ project_name }}-{{ project_env }}"
  81. region: "{{ region }}"
  82. state: "{{ state }}"
  83. rules:
  84. - proto: tcp
  85. ports:
  86. - 80
  87. - 22
  88. - 443
  89. cidr_ip: 0.0.0.0/0
  90. tags:
  91. Name: "{{ project_name }}-{{ project_env }}-webserver"
  92. Env: "{{ project_env }}"
  93. Project: "{{ project_name }}"
  94. register: sg_status
  95. tags:
  96. - prov
  97. - destroy
  98. - name: "set_fact"
  99. set_fact:
  100. project_name: "{{ project_name }}"
  101. project_env: "{{ project_env }}"
  102. region: "{{ region }}"
  103. sg_status: "{{ sg_status }}"
  104. tags:
  105. - prov
  106. - destroy
  107. - name: "Creating {{ project_name }}-{{ project_env }} ec2-intsances"
  108. amazon.aws.ec2_instance:
  109. name: "{{ project_name }}-{{ project_env }}-webserver"
  110. state: "{{ state_instance }}"
  111. region: "{{ region }}"
  112. instance_type: t2.micro
  113. key_name: "{{ project_name }}-{{ project_env }}"
  114. image_id: ami-0b9ecf71fe947bbdd
  115. exact_count: 4
  116. filters:
  117. "tag:Name": "{{ project_name }}-{{ project_env }}-webserver"
  118. "tag:Env": "{{ project_env }}"
  119. "tag:Project": "{{ project_name }}"
  120. security_groups:
  121. - "{{ sg_status.group_id }}"
  122. tags:
  123. Name: "{{ project_name }}-{{ project_env }}-webserver"
  124. Env: "{{ project_env }}"
  125. Project: "{{ project_name }}"
  126. register: instance_status
  127. tags:
  128. - prov
  129. - name: "Creating a dynamic Inventory"
  130. add_host:
  131. hostname: "{{ item.private_ip_address }}"
  132. groups: "{{ project_name }}-{{ project_env }}"
  133. ansible_ssh_hosts: "{{ item.private_ip_address }}"
  134. ansible_ssh_user: ec2-user
  135. ansible_ssh_private_key_file: "{{ project_name }}-{{ project_env }}"
  136. ansible_ssh_common_args: "-o StrictHostKeyChecking=no"
  137. ansible_python_interpreter: auto_silent
  138. with_items:
  139. - "{{ instance_status.instances }}"
  140. tags:
  141. - prov
  142. - name: "wait for 60 sec"
  143. wait_for:
  144. timeout: 60
  145. tags:
  146. - prov
  147. - name: "Provisioning httpd and php for {{ hostvars.localhost.project_name }}-{{ hostvars.localhost.project_env }}"
  148. vars:
  149. packages:
  150. - httpd
  151. - php-fpm
  152. httpd_owner: apache
  153. hosts: "{{ hostvars.localhost.project_name }}-{{ hostvars.localhost.project_env }}"
  154. become: true
  155. tags:
  156. - prov
  157. - never
  158. tasks:
  159. - name: "Install packages for php-website"
  160. when: hostvars.localhost.instance_status.changed == true
  161. yum:
  162. name: "{{ packages }}"
  163. state: present
  164. - name: "Sevice restart"
  165. when: hostvars.localhost.instance_status.changed == true
  166. service:
  167. name: "{{ item }}"
  168. state: started
  169. enabled: true
  170. with_items:
  171. - "{{ packages }}"
  172. - name: "Setting the index.html file"
  173. when: hostvars.localhost.instance_status.changed == true
  174. copy:
  175. content: "<h1><center>{{ hostvars.localhost.project_name }}-{{ hostvars.localhost.project_env }} Version 1</center></h1>"
  176. dest: /var/www/html/index.html
  177. owner: "{{ httpd_owner }}"
  178. group: "{{ httpd_owner }}"
  179. - name: "Delete aws infra"
  180. hosts: localhost
  181. tags:
  182. - destroy
  183. tasks:
  184. - name: "Deleting {{ project_name }}-{{ project_env }} ec2-intsance"
  185. amazon.aws.ec2_instance:
  186. region: "{{ region }}"
  187. filters:
  188. "tag:Name": "{{ project_name }}-{{ project_env }}-webserver"
  189. "tag:Env": "{{ project_env }}"
  190. "tag:Project": "{{ project_name }}"
  191. state: absent
  192. - name: "Delete the security group name {{ project_name }}-{{ project_env }}"
  193. amazon.aws.ec2_security_group:
  194. region: "{{ region }}"
  195. group_id: "{{ sg_status.group_id }}"
  196. state: absent
  197. - name: "Deleting key pair {{ project_name }}-{{ project_env }}"
  198. amazon.aws.ec2_key:
  199. name: "{{ item }}"
  200. region: "{{ region }}"
  201. state: absent
  202. with_items:
  203. - "{{ project_name }}-{{ project_env }}"
  204. - "{{ project_name }}"
  205. - name: "Deleting the keypair in localmachine"
  206. file:
  207. path: "{{ item }}"
  208. state: absent
  209. with_items:
  210. - "{{ project_name }}-{{ project_env }}.pub"
  211. - "{{ project_name }}-{{ project_env }}"
  212. - "{{ project_name }}.pem"
  213.  
  214.  
  215.  
  216.  
  217. #### Output with infra and prov tags ####
  218.  
  219. [ec2-user@ip-172-31-15-6 ~]$ ansible-playbook ansible.yml -t infra,prov --skip-tags=requirements
  220.  
  221.  
  222. PLAY [Creating the zomato webserver infra] *****************************************************************************
  223.  
  224. TASK [Gathering Facts] *************************************************************************************************
  225. ok: [localhost]
  226.  
  227. TASK [Creating a new ssh keypair] **************************************************************************************
  228. changed: [localhost]
  229.  
  230. TASK [set_fact] ********************************************************************************************************
  231. ok: [localhost]
  232.  
  233. TASK [creating a new EC2 key pair using lookup function] ***************************************************************
  234. changed: [localhost]
  235.  
  236. TASK [Creating a Second key pair with default values] ******************************************************************
  237. changed: [localhost]
  238.  
  239. TASK [Printing the private_key value] **********************************************************************************
  240. ok: [localhost] => {
  241. "key_status": {
  242. "changed": true,
  243. "failed": false,
  244. "key": {
  245. "fingerprint": "eb:2a:a3:ac:f2:6b:5b:2a:5c:ae:72:b8:ee:89:3f:39:c0:df:d8:79",
  246. "id": "key-0c555d9ec34e49a70",
  247. "name": "zomato",
  248. "private_key": "-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEAhV9mqQUDauoTt+BsTNn0k3ZVujp20822oAqf8vYi8bEZz3OV\nAZL7ls1OMPdiI5CoPL3O13QLqh31PNeNnLWF7970Iz8hfOOdXsz/u8O5IPdhpP/S\n5LtvZLCbUjxRoneJsxpHxlGX4TXTPILAPC+imVPm4WIPKd9BWg/NrdZt5kIK3O7o\nAK4UGP14ALmk2S/E8cWFcTsu1LhDLgNoeoKKsk83PHmT089x3FAR0S61rM3Kd1pm\n0B319TXWwwluZWENIdokGIa5VCVVHT8CHnLwJ8tApNUOKLRkHmQqXcOYlHsy8ohh\nBymCsVBBlFfeMNW6WfsBsd4rh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86Zj4PxR5gNH79hcGGFwG3Ha\nf0Mi0e0CgYBDsO/cYs6TTjs+BvJ9OcAiJMgdkJVvJWFBZckMYvOg5ZV35Y1rawrb\nM9hb835AioNVQlaZ/FRxcs9A9c/YTuShRWwyTH6S1erx/ah1ekg9O/qTmgYj778F\ndSPq14Utvuy51lKzIgW0RkAW59zjhd7Jf0AAytFG8CnpfUSkVzTTrA==\n-----END RSA PRIVATE KEY-----",
  249. "tags": {}
  250. },
  251. "msg": "key pair created"
  252. }
  253. }
  254.  
  255. TASK [Copying the private key to the current location] *****************************************************************
  256. changed: [localhost]
  257.  
  258. TASK [Create a security group name zomato-dev] *************************************************************************
  259. changed: [localhost]
  260.  
  261. TASK [Creating 3 ec2-intsance] *****************************************************************************************
  262. changed: [localhost]
  263.  
  264. TASK [Creating a dynamic Inventory] ************************************************************************************
  265.  
  266. TASK [wait for 60 sec] *************************************************************************************************
  267. ok: [localhost]
  268.  
  269. PLAY [Provisioning httpd and php for zomato-dev] ***********************************************************************
  270.  
  271. TASK [Gathering Facts] *************************************************************************************************
  272. ok: [172.31.39.23]
  273. ok: [172.31.44.39]
  274. ok: [172.31.46.17]
  275.  
  276. TASK [Install packages for php-website] ********************************************************************************
  277. changed: [172.31.39.23]
  278. changed: [172.31.44.39]
  279. changed: [172.31.46.17]
  280.  
  281. TASK [Sevice restart] **************************************************************************************************
  282. changed: [172.31.39.23] => (item=httpd)
  283. changed: [172.31.44.39] => (item=httpd)
  284. changed: [172.31.46.17] => (item=httpd)
  285. changed: [172.31.39.23] => (item=php-fpm)
  286. changed: [172.31.44.39] => (item=php-fpm)
  287. changed: [172.31.46.17] => (item=php-fpm)
  288.  
  289. TASK [Setting the index.html file] *************************************************************************************
  290. changed: [172.31.39.23]
  291. changed: [172.31.46.17]
  292. changed: [172.31.44.39]
  293.  
  294. PLAY [Delete aws infra] ************************************************************************************************
  295.  
  296. PLAY RECAP *************************************************************************************************************
  297. 172.31.39.23 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  298. 172.31.44.39 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  299. 172.31.46.17 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  300. localhost : ok=11 changed=7 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  301.  
  302.  
  303.  
  304. #### output after updating ec2-instance to 4 ####
  305.  
  306.  
  307. [ec2-user@ip-172-31-15-6 ~]$ ansible-playbook ansible.yml -t prov --skip-tags=requirements
  308.  
  309.  
  310. PLAY [Creating the zomato webserver infra] *****************************************************************************
  311.  
  312. TASK [set_fact] ********************************************************************************************************
  313. ok: [localhost]
  314.  
  315. TASK [Create a security group name zomato-dev] *************************************************************************
  316. ok: [localhost]
  317.  
  318. TASK [Creating zomato-dev ec2-intsances] *******************************************************************************
  319. changed: [localhost]
  320.  
  321. TASK [Creating a dynamic Inventory] ************************************************************************************
  322.  
  323. TASK [wait for 60 sec] *************************************************************************************************
  324. ok: [localhost]
  325.  
  326. PLAY [Provisioning httpd and php for zomato-dev] ***********************************************************************
  327.  
  328. TASK [Gathering Facts] *************************************************************************************************
  329. ok: [172.31.39.23]
  330. ok: [172.31.46.17]
  331. ok: [172.31.44.39]
  332. ok: [172.31.47.178]
  333.  
  334. TASK [Install packages for php-website] ********************************************************************************
  335. ok: [172.31.39.23]
  336. ok: [172.31.46.17]
  337. ok: [172.31.44.39]
  338. changed: [172.31.47.178]
  339.  
  340. TASK [Sevice restart] **************************************************************************************************
  341. ok: [172.31.39.23] => (item=httpd)
  342. ok: [172.31.44.39] => (item=httpd)
  343. ok: [172.31.46.17] => (item=httpd)
  344. ok: [172.31.39.23] => (item=php-fpm)
  345. ok: [172.31.46.17] => (item=php-fpm)
  346. ok: [172.31.44.39] => (item=php-fpm)
  347. changed: [172.31.47.178] => (item=httpd)
  348. changed: [172.31.47.178] => (item=php-fpm)
  349.  
  350. TASK [Setting the index.html file] *************************************************************************************
  351. ok: [172.31.44.39]
  352. ok: [172.31.39.23]
  353. ok: [172.31.46.17]
  354. changed: [172.31.47.178]
  355.  
  356. PLAY [Delete aws infra] ************************************************************************************************
  357.  
  358. PLAY RECAP *************************************************************************************************************
  359. 172.31.39.23 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  360. 172.31.44.39 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  361. 172.31.46.17 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  362. 172.31.47.178 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  363. localhost : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  364.  
  365.  
  366.  
  367. #### output when no change ####
  368.  
  369.  
  370. [ec2-user@ip-172-31-15-6 ~]$ ansible-playbook ansible.yml -t prov --skip-tags=requirements
  371.  
  372. PLAY [Creating the zomato webserver infra] *****************************************************************************
  373.  
  374. TASK [set_fact] ********************************************************************************************************
  375. ok: [localhost]
  376.  
  377. TASK [Create a security group name zomato-dev] *************************************************************************
  378. ok: [localhost]
  379.  
  380. TASK [Creating zomato-dev ec2-intsances] *******************************************************************************
  381. ok: [localhost]
  382.  
  383. TASK [Creating a dynamic Inventory] ***********************************************************************************
  384.  
  385. TASK [wait for 60 sec] *************************************************************************************************
  386. ok: [localhost]
  387.  
  388. PLAY [Provisioning httpd and php for zomato-dev] ***********************************************************************
  389.  
  390. TASK [Gathering Facts] *************************************************************************************************
  391. ok: [172.31.39.23]
  392. ok: [172.31.44.39]
  393. ok: [172.31.47.178]
  394. ok: [172.31.46.17]
  395.  
  396. TASK [Install packages for php-website] ********************************************************************************
  397. skipping: [172.31.46.17]
  398. skipping: [172.31.39.23]
  399. skipping: [172.31.44.39]
  400. skipping: [172.31.47.178]
  401.  
  402. TASK [Sevice restart] **************************************************************************************************
  403. skipping: [172.31.46.17] => (item=httpd)
  404. skipping: [172.31.46.17] => (item=php-fpm)
  405. skipping: [172.31.46.17]
  406. skipping: [172.31.39.23] => (item=httpd)
  407. skipping: [172.31.39.23] => (item=php-fpm)
  408. skipping: [172.31.39.23]
  409. skipping: [172.31.44.39] => (item=httpd)
  410. skipping: [172.31.44.39] => (item=php-fpm)
  411. skipping: [172.31.44.39]
  412. skipping: [172.31.47.178] => (item=httpd)
  413. skipping: [172.31.47.178] => (item=php-fpm)
  414. skipping: [172.31.47.178]
  415.  
  416. TASK [Setting the index.html file] *************************************************************************************
  417. skipping: [172.31.46.17]
  418. skipping: [172.31.39.23]
  419. skipping: [172.31.44.39]
  420. skipping: [172.31.47.178]
  421.  
  422. PLAY [Delete aws infra] ************************************************************************************************
  423.  
  424. PLAY RECAP *************************************************************************************************************
  425. 172.31.39.23 : ok=1 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
  426. 172.31.44.39 : ok=1 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
  427. 172.31.46.17 : ok=1 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
  428. 172.31.47.178 : ok=1 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
  429. localhost : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  430.  
  431.  
  432. #### Terminating the infra ####
  433.  
  434. [ec2-user@ip-172-31-15-6 ~]$ ansible-playbook ansible.yml -t destroy --skip-tags=requirements
  435. [WARNING]: No inventory was parsed, only implicit localhost is available
  436. [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match
  437. 'all'
  438. [WARNING]: While constructing a mapping from /home/ec2-user/ansible.yml, line 7, column 5, found a duplicate dict key
  439. (region). Using last defined value only.
  440.  
  441. PLAY [Creating the zomato webserver infra] *****************************************************************************
  442.  
  443. TASK [Create a security group name zomato-dev] *************************************************************************
  444. ok: [localhost]
  445.  
  446. TASK [set_fact] ********************************************************************************************************
  447. ok: [localhost]
  448. [WARNING]: Could not match supplied host pattern, ignoring: zomato-dev
  449.  
  450. PLAY [Provisioning httpd and php for zomato-dev] ***********************************************************************
  451. skipping: no hosts matched
  452.  
  453. PLAY [Delete aws infra] ************************************************************************************************
  454.  
  455. TASK [Gathering Facts] *************************************************************************************************
  456. ok: [localhost]
  457.  
  458. TASK [Deleting zomato-dev ec2-intsance] ********************************************************************************
  459. changed: [localhost]
  460.  
  461. TASK [Delete the security group name zomato-dev] ***********************************************************************
  462. changed: [localhost]
  463.  
  464. TASK [Deleting key pair zomato-dev] ************************************************************************************
  465. changed: [localhost] => (item=zomato-dev)
  466. changed: [localhost] => (item=zomato)
  467.  
  468. TASK [Deleting the keypair in localmachine] ****************************************************************************
  469. changed: [localhost] => (item=zomato-dev.pub)
  470. changed: [localhost] => (item=zomato-dev)
  471. changed: [localhost] => (item=zomato.pem)
  472.  
  473. PLAY RECAP *************************************************************************************************************
  474. localhost : ok=7 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  475.  
Advertisement
Add Comment
Please, Sign In to add comment