chrisluiz16

Ansible Day 1

Jun 19th, 2023
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.02 KB | None | 0 0
  1.  
  2.  
  3. -------------------------------------------------------------- Task -------------------------------------------
  4.  
  5.  
  6.  
  7.  
  8. -----------------------Adhoc method ------------------------------
  9.  
  10.  
  11. ------- Inventory --------------
  12.  
  13.  
  14. [ec2-user@ip-172-31-11-206 ~]$ cat inventory
  15. [amazon]
  16. 172.31.7.158 ansible_port=22 ansible_ssh_user="ec2-user" ansible_ssh_private_key_file="aws.pem"
  17.  
  18. [ubuntu]
  19. 172.31.3.223 ansible_port=22 ansible_ssh_user="ubuntu" ansible_ssh_private_key_file="aws.pem"
  20. [ec2-user@ip-172-31-11-206 ~]$
  21.  
  22.  
  23.  
  24. ------- Ping module --------------
  25.  
  26. [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory -m ping amazon
  27. The authenticity of host '172.31.7.158 (172.31.7.158)' can't be established.
  28. ED25519 key fingerprint is SHA256:iIE3RBdA5K9GXiqiuGSrsjl8Yy+YnRSizu5wUySLitY.
  29. This key is not known by any other names
  30. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
  31. 172.31.7.158 | SUCCESS => {
  32. "ansible_facts": {
  33. "discovered_interpreter_python": "/usr/bin/python3.9"
  34. },
  35. "changed": false,
  36. "ping": "pong"
  37. }
  38. [ec2-user@ip-172-31-11-206 ~]$
  39. [ec2-user@ip-172-31-11-206 ~]$
  40. [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory -m ping ubuntu
  41. The authenticity of host '172.31.3.223 (172.31.3.223)' can't be established.
  42. ED25519 key fingerprint is SHA256:pg1/KnS/g2w+2b2EaoZmMHS6V4Zyi8jCTFmPeOh8Xt0.
  43. This key is not known by any other names
  44. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
  45. 172.31.3.223 | SUCCESS => {
  46. "ansible_facts": {
  47. "discovered_interpreter_python": "/usr/bin/python3"
  48. },
  49. "changed": false,
  50. "ping": "pong"
  51. }
  52.  
  53.  
  54.  
  55.  
  56.  
  57. --------- YUM module -----------
  58.  
  59. --- error -----
  60.  
  61. [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory amazon -m yum -a "name=httpd state=present"
  62. 172.31.7.158 | FAILED! => {
  63. "ansible_facts": {
  64. "discovered_interpreter_python": "/usr/bin/python3.9"
  65. },
  66. "changed": false,
  67. "msg": "This command has to be run under the root user.",
  68. "results": []
  69. }
  70.  
  71.  
  72. ----Fix with become b flag------
  73.  
  74. [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory amazon -b -m yum -a "name=httpd state=present"
  75. 172.31.7.158 | CHANGED => {
  76. "ansible_facts": {
  77. "discovered_interpreter_python": "/usr/bin/python3.9"
  78. },
  79. "changed": true,
  80. "msg": "",
  81. "rc": 0,
  82.  
  83.  
  84. ------------ apt module ------------
  85.  
  86. [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory ubuntu -b -m apt -a "name=apache2 state=present"
  87. 172.31.3.223 | CHANGED => {
  88. "ansible_facts": {
  89. "discovered_interpreter_python": "/usr/bin/python3"
  90. },
  91. "cache_update_time": 1684202990,
  92. "cache_updated": false,
  93. "changed": true,
  94. "stderr": "",
  95.  
  96.  
  97. ----- state=absent ---------
  98.  
  99. [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory ubuntu -b -m apt -a "name=apache2 state=absent"
  100. 172.31.3.223 | CHANGED => {
  101. "ansible_facts": {
  102. "discovered_interpreter_python": "/usr/bin/python3"
  103. },
  104. "changed": true,
  105. "stderr": "",
  106.  
  107.  
  108. ----- back to present ------
  109.  
  110. [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory ubuntu -b -m apt -a "name=apache2 state=present"
  111. 172.31.3.223 | CHANGED => {
  112. "ansible_facts": {
  113. "discovered_interpreter_python": "/usr/bin/python3"
  114. },
  115. "cache_update_time": 1684202990,
  116. "cache_updated": false,
  117. "changed": true,
  118.  
  119.  
  120. ----- state=started -----
  121. [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory ubuntu -b -m service -a "name=apache2 state=started enabled=true"
  122. 172.31.3.223 | SUCCESS => {
  123. "ansible_facts": {
  124. "discovered_interpreter_python": "/usr/bin/python3"
  125. },
  126. "changed": false,
  127. "enabled": true,
  128. "name": "apache2",
  129. "state": "started",
  130.  
  131.  
  132. ------- state=restarted ------
  133. [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory ubuntu -b -m service -a "name=apache2 state=restarted enabled=true"
  134. 172.31.3.223 | CHANGED => {
  135. "ansible_facts": {
  136. "discovered_interpreter_python": "/usr/bin/python3"
  137. },
  138. "changed": true,
  139. "enabled": true,
  140. "name": "apache2",
  141. "state": "started",
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149. ---------------- Service module ----------------
  150.  
  151. [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory amazon -b -m service -a "name=httpd state=started enabled=true"
  152. [WARNING]: Platform linux on host 172.31.7.158 is using the discovered Python interpreter at /usr/bin/python3.9, but future
  153. installation of another Python interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-
  154. core/2.15/reference_appendices/interpreter_discovery.html for more information.
  155. 172.31.7.158 | CHANGED => {
  156. "ansible_facts": {
  157. "discovered_interpreter_python": "/usr/bin/python3.9"
  158. },
  159. "changed": true,
  160. "enabled": true,
  161. "name": "httpd",
  162. "state": "started",
  163.  
  164.  
  165.  
  166. [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory ubuntu -b -m service -a "name=apache2 state=started enabled=true"
  167. 172.31.3.223 | CHANGED => {
  168. "ansible_facts": {
  169. "discovered_interpreter_python": "/usr/bin/python3"
  170. },
  171. "changed": true,
  172. "enabled": true,
  173. "name": "apache2",
  174. "state": "started",
  175.  
  176.  
  177.  
  178.  
  179.  
  180. -------------- Copy module -------------
  181.  
  182. [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory amazon -b -m copy -a "src=./website/ dest=/var/www/html owner=apache group=apache"
  183. 172.31.7.158 | CHANGED => {
  184. "changed": true,
  185. "dest": "/var/www/html/",
  186. "src": "/home/ec2-user/./website/"
  187. }
  188.  
  189. [ec2-user@ip-172-31-11-206 ~]$ ansible -i inventory ubuntu -b -m copy -a "src=./website/ dest=/var/www/html/ owner=www-data group=www-data"
  190. 172.31.3.223 | CHANGED => {
  191. "changed": true,
  192. "dest": "/var/www/html/",
  193. "src": "/home/ec2-user/./website/"
  194. }
  195.  
  196.  
  197.  
  198.  
  199. ------------------------------------------------- Playbook ----------------------------------------------
  200.  
  201.  
  202. [ec2-user@ip-172-31-11-206 ~]$ cat amazon.yml
  203. ---
  204.  
  205. - name: "httpd and php installation"
  206. hosts: amazon
  207. vars:
  208. httpd_owner: apache
  209. httpd_group: apache
  210. become: true
  211. tasks:
  212. - name: "httpd install for amazon linux 2023"
  213. yum:
  214. name: httpd,php
  215. state: latest
  216. - name: "httpd enable"
  217. service:
  218. name: httpd
  219. state: started
  220. enabled: true
  221. - name: "php-fpm enable"
  222. service:
  223. name: php-fpm
  224. state: started
  225. enabled: true
  226. - name: "Copying website"
  227. copy:
  228. src: ./website/
  229. dest: /var/www/html
  230. owner: "{{ httpd_owner }}"
  231. group: "{{ httpd_group }}"
  232.  
  233.  
  234.  
  235.  
  236. [ec2-user@ip-172-31-11-206 ~]$ ansible-playbook -i inventory amazon.yml
  237.  
  238. PLAY [httpd and php installation] ******************************************************************************************
  239.  
  240. TASK [Gathering Facts] *****************************************************************************************************
  241. [WARNING]: Platform linux on host 172.31.7.158 is using the discovered Python interpreter at /usr/bin/python3.9, but future
  242. installation of another Python interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-
  243. core/2.15/reference_appendices/interpreter_discovery.html for more information.
  244. ok: [172.31.7.158]
  245.  
  246. TASK [httpd install for amazon linux 2023] *********************************************************************************
  247. changed: [172.31.7.158]
  248.  
  249. TASK [httpd enable] ********************************************************************************************************
  250. changed: [172.31.7.158]
  251.  
  252. TASK [php-fpm enable] ******************************************************************************************************
  253. changed: [172.31.7.158]
  254.  
  255. TASK [Copying website] *****************************************************************************************************
  256. changed: [172.31.7.158]
  257.  
  258. PLAY RECAP *****************************************************************************************************************
  259. 172.31.7.158 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  260.  
  261.  
  262.  
  263.  
  264. ---------------- ubuntu.yml ---------------
  265.  
  266. ---
  267.  
  268. - name: "apache and php installation"
  269. hosts: ubuntu
  270. vars:
  271. apache_owner: www-data
  272. apache_group: www-data
  273. become: true
  274. tasks:
  275. - name: "Apache and php install for ubuntu 22.04 LTS"
  276. apt:
  277. name: apache2,php8.1-fpm
  278. state: present
  279. - name: "apache2 enable"
  280. service:
  281. name: apache2
  282. state: started
  283. enabled: true
  284. - name: "php-fpm enable"
  285. service:
  286. name: php8.1-fpm
  287. state: started
  288. enabled: true
  289. - name: "Copying website"
  290. copy:
  291. src: ./website/
  292. dest: /var/www/html
  293. owner: "{{ apache_owner }}"
  294. group: "{{ apache_group }}"
  295.  
  296.  
  297.  
  298. [ec2-user@ip-172-31-11-206 ~]$ ansible-playbook -i inventory ubuntu.yml
  299.  
  300. PLAY [apache and php installation] *****************************************************************************************
  301. TASK [Gathering Facts] *****************************************************************************************************ok: [172.31.3.223]
  302.  
  303. TASK [Apache and php install for ubuntu 22.04 LTS] *************************************************************************changed: [172.31.3.223]
  304.  
  305. TASK [apache2 enable] ******************************************************************************************************ok: [172.31.3.223]
  306.  
  307. TASK [php-fpm enable] ******************************************************************************************************ok: [172.31.3.223]
  308.  
  309. TASK [Copying website] *****************************************************************************************************changed: [172.31.3.223]
  310.  
  311. PLAY RECAP *****************************************************************************************************************
  312. 172.31.3.223 : ok=5 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  313.  
  314.  
  315.  
  316. ---- Version 5 upgrade -----
  317.  
  318. [ec2-user@ip-172-31-11-206 ~]$ ansible-playbook -i inventory ubuntu.yml
  319.  
  320. PLAY [apache and php installation] ****************************************************************************************
  321.  
  322. TASK [Gathering Facts] ****************************************************************************************************
  323. ok: [172.31.3.223]
  324.  
  325. TASK [Apache and php install for ubuntu 22.04 LTS] ************************************************************************
  326. ok: [172.31.3.223]
  327.  
  328. TASK [apache2 enable] *****************************************************************************************************
  329. ok: [172.31.3.223]
  330.  
  331. TASK [php-fpm enable] *****************************************************************************************************
  332. ok: [172.31.3.223]
  333.  
  334. TASK [Copying website] ****************************************************************************************************
  335. changed: [172.31.3.223]
  336.  
  337. PLAY RECAP ****************************************************************************************************************
  338. 172.31.3.223 : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
  339.  
  340.  
  341.  
Advertisement
Add Comment
Please, Sign In to add comment