chrisluiz16

Ansible Day 1 edited

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