chrisluiz16

Ansible day 4

Jun 22nd, 2023
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.04 KB | None | 0 0
  1.  
  2.  
  3. -------------------------------------------------------------- Task 1-------------------------------------------
  4.  
  5.  
  6. #### Inventory ####
  7.  
  8.  
  9. [ec2-user@ip-172-31-10-28 ~]$ cat inventory
  10. [amazon]
  11. 172.31.5.81 ansible_port=22 ansible_ssh_user="ec2-user" ansible_ssh_private_key_file="aws.pem"
  12.  
  13. [ubuntu]
  14. 172.31.0.96 ansible_port=22 ansible_ssh_user="ubuntu" ansible_ssh_private_key_file="aws.pem"
  15.  
  16.  
  17.  
  18. [ec2-user@ip-172-31-10-28 ~]$ echo "The SQL database running on the server is {{ package }} version {{ version }}">myfile.txt
  19. [ec2-user@ip-172-31-10-28 ~]$
  20. [ec2-user@ip-172-31-10-28 ~]$
  21. [ec2-user@ip-172-31-10-28 ~]$ cat myfile.txt
  22. The SQL database running on the server is {{ package }} version {{ version }}
  23. [ec2-user@ip-172-31-10-28 ~]$
  24. [ec2-user@ip-172-31-10-28 ~]$
  25. [ec2-user@ip-172-31-10-28 ~]$
  26.  
  27.  
  28.  
  29.  
  30. #### Copy and template module running in localhost ####
  31.  
  32. [ec2-user@ip-172-31-10-28 ~]$ cat copy.yml
  33. ---
  34.  
  35. - name: "Copying files"
  36. hosts: localhost
  37. vars:
  38. package: mysql-server
  39. version: 5.8
  40. tasks:
  41. - name: "Copying file using the copy module"
  42. copy:
  43. src: ./myfile.txt
  44. dest: /tmp/copy.txt
  45. - name: "Copying files using the template module"
  46. template:
  47. src: ./myfile.txt
  48. dest: /tmp/template.txt
  49.  
  50.  
  51.  
  52. [ec2-user@ip-172-31-10-28 ~]$ ansible-playbook copy.yml
  53. [WARNING]: No inventory was parsed, only implicit localhost is available
  54. [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match
  55. 'all'
  56.  
  57. PLAY [Copying files] ***************************************************************************************************
  58.  
  59. TASK [Gathering Facts] *************************************************************************************************
  60. ok: [localhost]
  61.  
  62. TASK [Copying file using the copy module] ******************************************************************************
  63. changed: [localhost]
  64.  
  65. TASK [Copying files using the template module] *************************************************************************
  66. changed: [localhost]
  67.  
  68. PLAY RECAP *************************************************************************************************************
  69. localhost : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  70.  
  71. [ec2-user@ip-172-31-10-28 ~]$
  72. [ec2-user@ip-172-31-10-28 ~]$
  73. [ec2-user@ip-172-31-10-28 ~]$
  74. [ec2-user@ip-172-31-10-28 ~]$ cat /tmp/copy.txt
  75. The SQL database running on the server is {{ package }} version {{ version }}
  76. [ec2-user@ip-172-31-10-28 ~]$
  77. [ec2-user@ip-172-31-10-28 ~]$
  78. [ec2-user@ip-172-31-10-28 ~]$ cat /tmp/template.txt
  79. The SQL database running on the server is mysql-server version 5.8
  80. [ec2-user@ip-172-31-10-28 ~]$
  81.  
  82.  
  83.  
  84. #### Copy module with remote_src ####
  85.  
  86. ---
  87.  
  88. - name: "Copying files"
  89. hosts: amazon
  90. vars:
  91. package: mysql-server
  92. version: 5.8
  93. tasks:
  94. - name: "Copying file using the copy module"
  95. copy:
  96. src: /etc/os-release
  97. dest: /tmp
  98. remote_src: true
  99.  
  100.  
  101.  
  102.  
  103. ec2-user@ip-172-31-10-28 ~]$ ansible-playbook -i inventory copy.yml
  104.  
  105. PLAY [Copying files] ***************************************************************************************************
  106.  
  107. TASK [Gathering Facts] *************************************************************************************************
  108. ok: [172.31.5.81]
  109.  
  110. TASK [Copying file using the copy module] ******************************************************************************
  111. ok: [172.31.5.81]
  112.  
  113. PLAY RECAP *************************************************************************************************************
  114. 172.31.5.81 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  115.  
  116.  
  117.  
  118. [ec2-user@ip-172-31-10-28 ~]$ ansible -i inventory amazon -m command -a "cat /tmp/os-release"
  119. 172.31.5.81 | CHANGED | rc=0 >>
  120. NAME="Amazon Linux"
  121. VERSION="2023"
  122. ID="amzn"
  123. ID_LIKE="fedora"
  124. VERSION_ID="2023"
  125. PLATFORM_ID="platform:al2023"
  126. PRETTY_NAME="Amazon Linux 2023"
  127. ANSI_COLOR="0;33"
  128. CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2023"
  129. HOME_URL="https://aws.amazon.com/linux/"
  130. BUG_REPORT_URL="https://github.com/amazonlinux/amazon-linux-2023"
  131. SUPPORT_END="2028-03-01"
  132.  
  133.  
  134.  
  135.  
  136. -------------------------------------------------------------- Task 2 -------------------------------------------
  137.  
  138.  
  139. [ec2-user@ip-172-31-10-28 ~]$ echo "<h1><center>Hello World! This is running in version 1 </h1></center>">index.html
  140. [ec2-user@ip-172-31-10-28 ~]$
  141. [ec2-user@ip-172-31-10-28 ~]$
  142. [ec2-user@ip-172-31-10-28 ~]$ cat index.html
  143. <h1><center>Hello World! This is running in version 1 </h1></center>
  144. [ec2-user@ip-172-31-10-28 ~]$
  145.  
  146.  
  147.  
  148.  
  149. #### without condition ###
  150.  
  151.  
  152. [ec2-user@ip-172-31-10-28 ~]$
  153. ---
  154.  
  155. - name: "Apache installation"
  156. hosts: amazon
  157. become: true
  158. vars:
  159. httpd_owner: apache
  160. httpd_group: apache
  161. tasks:
  162. - name: "Apache installation"
  163. yum:
  164. name: httpd
  165. state: present
  166. - name: "Copying files to the document root"
  167. copy:
  168. src: ./index.html
  169. dest: /var/www/html/
  170. owner: "{{ httpd_owner }}"
  171. group: "{{ httpd_group }}"
  172. - name: "Servic restart with condition"
  173. service:
  174. name: httpd
  175. state: restarted
  176. enabled: true
  177.  
  178.  
  179. [ec2-user@ip-172-31-10-28 ~]$ ansible-playbook -i inventory register.yml
  180.  
  181. PLAY [Apache installation] *********************************************************************************************
  182.  
  183. TASK [Gathering Facts] *************************************************************************************************
  184. ok: [172.31.5.81]
  185.  
  186. TASK [Apache installation] *********************************************************************************************
  187. changed: [172.31.5.81]
  188.  
  189. TASK [Copying files to the document root] ******************************************************************************
  190. changed: [172.31.5.81]
  191.  
  192. TASK [Servic restart with condition] ***********************************************************************************
  193. changed: [172.31.5.81]
  194.  
  195. PLAY RECAP *************************************************************************************************************
  196. 172.31.5.81 : ok=4 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  197.  
  198.  
  199.  
  200. #### with condition ###
  201.  
  202.  
  203. - name: "Apache installation"
  204. hosts: amazon
  205. become: true
  206. vars:
  207. httpd_owner: apache
  208. httpd_group: apache
  209. tasks:
  210. - name: "Apache installation"
  211. yum:
  212. name: httpd
  213. state: present
  214. - name: "Copying files to the document root"
  215. copy:
  216. src: ./index.html
  217. dest: /var/www/html/
  218. owner: "{{ httpd_owner }}"
  219. group: "{{ httpd_group }}"
  220. register: copy_status
  221. - name: "Servic restart with condition"
  222. when: copy_status.changed == true
  223. service:
  224. name: httpd
  225. state: restarted
  226. enabled: true
  227.  
  228.  
  229.  
  230. [ec2-user@ip-172-31-10-28 ~]$ echo "<h1><center>Hello World! This is running in version 2 </h1></center>">index.html
  231.  
  232.  
  233. #### Condition triggers when register status changed ####
  234.  
  235. [ec2-user@ip-172-31-10-28 ~]$ ansible-playbook -i inventory register.yml
  236.  
  237. PLAY [Apache installation] *********************************************************************************************
  238.  
  239. TASK [Gathering Facts] *************************************************************************************************
  240. ok: [172.31.5.81]
  241.  
  242. TASK [Apache installation] *********************************************************************************************
  243. ok: [172.31.5.81]
  244.  
  245. TASK [Copying files to the document root] ******************************************************************************
  246. changed: [172.31.5.81]
  247.  
  248. TASK [Servic restart with condition] ***********************************************************************************
  249. changed: [172.31.5.81]
  250.  
  251. PLAY RECAP *************************************************************************************************************
  252. 172.31.5.81 : ok=4 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  253.  
  254.  
  255.  
  256. #### Condition triggers when register status= false and service tasked skipping ####
  257.  
  258. [ec2-user@ip-172-31-10-28 ~]$ ansible-playbook -i inventory register.yml
  259.  
  260. PLAY [Apache installation] *********************************************************************************************
  261.  
  262. TASK [Gathering Facts] *************************************************************************************************
  263. ok: [172.31.5.81]
  264.  
  265. TASK [Apache installation] *********************************************************************************************
  266. ok: [172.31.5.81]
  267.  
  268. TASK [Copying files to the document root] ******************************************************************************
  269. ok: [172.31.5.81]
  270.  
  271. TASK [Servic restart with condition] ***********************************************************************************
  272. skipping: [172.31.5.81]
  273.  
  274. PLAY RECAP *************************************************************************************************************
  275. 172.31.5.81 : ok=3 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
  276.  
  277.  
  278.  
  279.  
  280.  
  281. -------------------------------------------------------------- Task 3 -------------------------------------------
  282.  
  283.  
  284. [[ec2-user@ip-172-31-10-28 ~]$ cat <<'EOF' > index.php
  285. <?php
  286. $hostname = shell_exec('echo $HOSTNAME');
  287. echo "<h1><center> $hostname </h1></center>";
  288. echo "<h1><center> Zomato App - Version 1 </h1></center>";
  289. ?>
  290. EOF
  291.  
  292.  
  293.  
  294. #### fetch module ###
  295.  
  296. [ec2-user@ip-172-31-10-28 ~]$ ansible -i inventory amazon -m fetch -a "src=/etc/httpd/conf/httpd.conf dest=./ flat=true"
  297. 172.31.5.81 | CHANGED => {
  298. "changed": true,
  299. "checksum": "fcfc3261dbe43ccf2a521213571849cf36e277c0",
  300. "dest": "/home/ec2-user/httpd.conf",
  301. "md5sum": "abc2d98f55602c033626f046ce72fffc",
  302. "remote_checksum": "fcfc3261dbe43ccf2a521213571849cf36e277c0",
  303. "remote_md5sum": null
  304. }
  305.  
  306.  
  307.  
  308.  
  309. #### Playbook ###
  310.  
  311. ---
  312.  
  313. - name: "Apache and php installation"
  314. hosts: amazon
  315. become: true
  316. vars:
  317. packages:
  318. - httpd
  319. - php
  320. httpd_owner: apache
  321. httpd_group: apache
  322. httpd_port: 80
  323. Docphp: index.php
  324. Dochtml: index.html
  325.  
  326.  
  327. tasks:
  328. - name: "Apache and php installation"
  329. yum:
  330. name: "{{ packages }}"
  331. state: present
  332. register: packages_status
  333. - name: "httpd conf file copy"
  334. template:
  335. src: ./httpd.conf.tmpl
  336. dest: /etc/httpd/conf/httpd.conf
  337. register: conf_status
  338. - name: "Copying index.php"
  339. copy:
  340. src: ./index.php
  341. dest: /var/www/html
  342. register: index_status
  343. - name: "Retsarting and enabling php-fpm if condition equals to true"
  344. when: packages_status.changed == true or index_status.changed == true
  345. service:
  346. name: php-fpm
  347. state: restarted
  348. enabled: true
  349. - name: "Retsarting and enabling httpd if condition equals to true"
  350. when: packages_status.changed == true or conf_status.changed == true
  351. service:
  352. name: httpd
  353. state: restarted
  354. enabled: true
  355.  
  356.  
  357. ### made changes to httpd.conf via addind document root index with the variables ###
  358.  
  359. #Listen 12.34.56.78:80
  360. Listen {{ httpd_port }}
  361.  
  362.  
  363. <IfModule dir_module>
  364. DirectoryIndex {{ Docphp }} {{ Dochtml }}
  365. </IfModule>
  366.  
  367.  
  368.  
  369.  
  370. #### update the port to 8080 and index.php to version2 ###
  371.  
  372. [ec2-user@ip-172-31-10-28 ~]$ ansible-playbook -i inventory httpd.yml
  373.  
  374. PLAY [Apache and php installation] *************************************************************************************
  375. TASK [Gathering Facts] *************************************************************************************************
  376. ok: [172.31.5.81]
  377.  
  378. TASK [Apache and php installation] *************************************************************************************
  379. ok: [172.31.5.81]
  380.  
  381. TASK [httpd conf file copy] ********************************************************************************************
  382. changed: [172.31.5.81]
  383.  
  384. TASK [Copying index.php] ***********************************************************************************************
  385. changed: [172.31.5.81]
  386.  
  387. TASK [Retsarting and enabling php-fpm if condition equals to true] *****************************************************
  388. changed: [172.31.5.81]
  389.  
  390. TASK [Retsarting and enabling httpd if condition equals to true] *******************************************************
  391. changed: [172.31.5.81]
  392.  
  393. PLAY RECAP *************************************************************************************************************
  394. 172.31.5.81 : ok=6 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  395.  
  396.  
  397.  
  398.  
  399.  
  400. -------------------------------------------------------------- Task 4 -------------------------------------------
  401.  
  402.  
  403. [ec2-user@ip-172-31-10-28 ~]$ ansible -i inventory ubuntu -m fetch -a "src=/etc/nginx/sites-enabled/default dest=./default.conf flat=true"
  404. 172.31.0.96 | CHANGED => {
  405. "changed": true,
  406. "checksum": "e042f85b2efaf32b539247298d66d9bd5a40068f",
  407. "dest": "/home/ec2-user/default.conf",
  408. "md5sum": "f1f26aef86f90a484f3a2f46ccc46ff6",
  409. "remote_checksum": "e042f85b2efaf32b539247298d66d9bd5a40068f",
  410. "remote_md5sum": null
  411. }
  412.  
  413. [ec2-user@ip-172-31-10-28 ~]$
  414. [ec2-user@ip-172-31-10-28 ~]$
  415. [ec2-user@ip-172-31-10-28 ~]$ ls -l default.conf
  416. -rw-r--r--. 1 ec2-user ec2-user 2412 Jun 22 11:44 default.conf
  417.  
  418.  
  419.  
  420. [ec2-user@ip-172-31-10-28 ~]$ ansible -i inventory ubuntu -b -m apt -a "name=nginx,nginx-common,nginx-core state=absent purge=true"
  421. 172.31.0.96 | CHANGED => {
  422. "ansible_facts": {
  423. "discovered_interpreter_python": "/usr/bin/python3"
  424. },
  425. "changed": true,
  426. "stderr": "",
  427. "stderr_lines": [],
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434. ### Playbook ###
  435.  
  436.  
  437.  
  438. ---
  439.  
  440. - name: "Nginx installation in ubuntu OS"
  441. hosts: ubuntu
  442. become: true
  443. vars:
  444. nginx_port: 8080
  445. nginx_user: www-data
  446. nginx_group: www-data
  447. tasks:
  448. - name: "Nginx installation"
  449. apt:
  450. name: nginx
  451. state: present
  452. update_cache: true
  453. - name: "Copying conf to remote hosts"
  454. template:
  455. src: ./default.conf
  456. dest: /etc/nginx/sites-enabled/default
  457. register: nginxconf_status
  458. - name: "Copying index.html to DOC root"
  459. copy:
  460. src: ./index.html
  461. dest: /var/www/html/
  462. owner: "{{ nginx_user }}"
  463. group: "{{ nginx_group }}"
  464. - name: "Nginx service restart when condition meets"
  465. when: nginxconf_status.changed == true
  466. service:
  467. name: nginx
  468. state: restarted
  469. enabled: true
  470.  
  471.  
  472.  
  473. #### Output ####
  474.  
  475. [ec2-user@ip-172-31-10-28 ~]$ ansible-playbook -i inventory nginx.yml
  476.  
  477. PLAY [Nginx installation in ubuntu OS] *********************************************************************************************************************
  478.  
  479. TASK [Gathering Facts] *************************************************************************************************************************************
  480. ok: [172.31.0.96]
  481.  
  482. TASK [Nginx installation] **********************************************************************************************************************************
  483. changed: [172.31.0.96]
  484.  
  485. TASK [Copying conf to remote hosts] ************************************************************************************************************************
  486. changed: [172.31.0.96]
  487.  
  488. TASK [Copying index.html to DOC root] **********************************************************************************************************************
  489. changed: [172.31.0.96]
  490.  
  491. TASK [Nginx service restart when condition meets] **********************************************************************************************************
  492. changed: [172.31.0.96]
  493.  
  494. PLAY RECAP *************************************************************************************************************************************************
  495. 172.31.0.96 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  496.  
  497.  
  498.  
Advertisement
Add Comment
Please, Sign In to add comment