chrisluiz16

node Js container + SMTP

Jul 4th, 2023
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 28.98 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. ####---------------------------------- Tasks  --------------------####
  6.  
  7.  
  8. #### playbook with normal variable declaration ####
  9.  
  10.  
  11. ---
  12.  
  13. - name: "Variable checking"
  14.   hosts: all
  15.   become: true
  16.   vars:
  17.     amazon_package:
  18.      - httpd
  19.       - php-fpm
  20.     amazon_service:
  21.      - httpd
  22.       - php-fpm
  23.     httpd_owner: apache
  24.     httpd_group: apache
  25.     ubuntu_package:
  26.      - apache2
  27.       - php-fpm
  28.     ubuntu_service:
  29.      - apache2
  30.       - php8.1-fpm
  31.     ubuntu_owner: www-data
  32.     ubuntu_group: www-data
  33.   tasks:
  34.     - name: "Install httpd package for Amazon OS"
  35.       when: ansible_distribution == "Amazon" or ansible_os_family == "Redhat"
  36.       yum:
  37.         name: "{{ amazon_package }}"
  38.         state: present
  39.     - name: "Copy the content for index.html"
  40.       when: ansible_distribution == "Amazon" or ansible_os_family == "Redhat"
  41.       copy:
  42.         content: "<h1><center>You are seeing a httpd server running in Amazon OS </center></h1>"
  43.         dest: /var/www/html/index.html
  44.         owner: "{{  httpd_owner }}"
  45.         group: "{{ httpd_group }}"
  46.     - name: "{{ ansible_distribution }} OS service restart and enable"
  47.       when: ansible_distribution == "Amazon" or ansible_os_family == "Redhat"
  48.       service:
  49.         name: "{{ item }}"
  50.         state: restarted
  51.         enabled: true
  52.       with_items:
  53.        - "{{ amazon_service }}"
  54.     - name: "Install httpd package for ubuntu OS"
  55.       when: ansible_distribution == "Ubuntu" or ansible_os_family == "Debian"
  56.       apt:
  57.         name: "{{ ubuntu_package }}"
  58.         state: present
  59.         update_cache: true
  60.     - name: "Copy the content for index.html"
  61.       when: ansible_distribution == "Ubuntu" or ansible_os_family == "Debian"
  62.       copy:
  63.         content: "<h1><center>You are seeing a httpd server running in {{ ansible_distribution }} OS </center></h1>"
  64.         dest: /var/www/html/index.html
  65.         owner: "{{  ubuntu_owner }}"
  66.         group: "{{ ubuntu_group }}"
  67.     - name: "Ubuntu  OS service restart and enable"
  68.       when: ansible_distribution == "Ubuntu" or ansible_os_family == "Debian"
  69.       service:
  70.         name: "{{ item }}"
  71.         state: restarted
  72.         enabled: true
  73.       with_items:
  74.        - "{{ ubuntu_service }}"
  75.        
  76.        
  77.        
  78.        
  79. ### Output ###
  80.  
  81.  
  82. [ec2-user@ip-172-31-10-148 ~]$ ansible-playbook -i inventory main.yml
  83.  
  84. PLAY [Variable checking] ***********************************************************************************************
  85.  
  86. TASK [Gathering Facts] *************************************************************************************************
  87. ok: [172.31.45.6]
  88. [WARNING]: Platform linux on host 172.31.10.148 is using the discovered Python interpreter at /usr/bin/python3.9, but
  89. future installation of another Python interpreter could change the meaning of that path. See
  90. https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information.
  91. ok: [172.31.10.148]
  92.  
  93. TASK [Install httpd package for Amazon OS] *****************************************************************************
  94. skipping: [172.31.45.6]
  95. changed: [172.31.10.148]
  96.  
  97. TASK [Copy the content for index.html] *********************************************************************************
  98. skipping: [172.31.45.6]
  99. changed: [172.31.10.148]
  100.  
  101. TASK [Amazon OS service restart and enable] ****************************************************************************
  102. skipping: [172.31.45.6] => (item=httpd)
  103. skipping: [172.31.45.6] => (item=php-fpm)
  104. skipping: [172.31.45.6]
  105. changed: [172.31.10.148] => (item=httpd)
  106. changed: [172.31.10.148] => (item=php-fpm)
  107.  
  108. TASK [Install httpd package for ubuntu OS] *****************************************************************************
  109. skipping: [172.31.10.148]
  110. changed: [172.31.45.6]
  111.  
  112. TASK [Copy the content for index.html] *********************************************************************************
  113. skipping: [172.31.10.148]
  114. changed: [172.31.45.6]
  115.  
  116. TASK [Ubuntu  OS service restart and enable] ***************************************************************************
  117. skipping: [172.31.10.148] => (item=apache2)
  118. skipping: [172.31.10.148] => (item=php8.1-fpm)
  119. skipping: [172.31.10.148]
  120. changed: [172.31.45.6] => (item=apache2)
  121. changed: [172.31.45.6] => (item=php8.1-fpm)
  122.  
  123. PLAY RECAP *************************************************************************************************************
  124. 172.31.10.148              : ok=4    changed=3    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0
  125. 172.31.45.6                : ok=4    changed=3    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0
  126.  
  127.  
  128.  
  129.  
  130. #### Set_facts playbook ####
  131.  
  132.  
  133. [ec2-user@ip-172-31-10-148 ~]$ cat main.yml
  134. ---
  135.  
  136. - name: "Variable checking"
  137.   hosts: all
  138.   become: true
  139.   tasks:
  140.     - name: "Setting dynamic variable for Amazon"
  141.       when: ansible_distribution == "Amazon" or ansible_os_family == "Redhat"
  142.       set_fact:
  143.         package:
  144.           - httpd
  145.            - php-fpm
  146.         service:
  147.          - httpd
  148.           - php-fpm
  149.         owner: apache
  150.         group: apache
  151.     - name: "Setting dynamic variable for Ubuntu"
  152.       when: ansible_distribution == "Ubuntu" or ansible_os_family == "Debian"
  153.       set_fact:
  154.         package:
  155.           - apache2
  156.            - php-fpm
  157.         service:
  158.          - apache2
  159.           - php8.1-fpm
  160.         owner: www-data
  161.         group: www-data
  162.     - name: "Install httpd package for Amazon OS"
  163.       when: ansible_distribution == "Amazon" or ansible_os_family == "Redhat"
  164.       yum:
  165.         name: "{{ package }}"
  166.         state: present
  167.     - name: "Copy the content for index.html"
  168.       when: ansible_distribution == "Amazon" or ansible_os_family == "Redhat"
  169.       copy:
  170.         content: "<h1><center>You are seeing a httpd server running in Amazon OS </center></h1>"
  171.         dest: /var/www/html/index.html
  172.         owner: "{{  owner }}"
  173.         group: "{{ group }}"
  174.     - name: "{{ ansible_distribution }} OS service restart and enable"
  175.       when: ansible_distribution == "Amazon" or ansible_os_family == "Redhat"
  176.       service:
  177.         name: "{{ item }}"
  178.         state: restarted
  179.         enabled: true
  180.       with_items:
  181.        - "{{ service }}"
  182.     - name: "Install httpd package for ubuntu OS"
  183.       when: ansible_distribution == "Ubuntu" or ansible_os_family == "Debian"
  184.       apt:
  185.         name: "{{ package }}"
  186.         state: present
  187.         update_cache: true
  188.     - name: "Copy the content for index.html"
  189.       when: ansible_distribution == "Ubuntu" or ansible_os_family == "Debian"
  190.       copy:
  191.         content: "<h1><center>You are seeing a httpd server running in {{ ansible_distribution }} OS </center></h1>"
  192.         dest: /var/www/html/index.html
  193.         owner: "{{  owner }}"
  194.         group: "{{ group }}"
  195.     - name: "Ubuntu  OS service restart and enable"
  196.       when: ansible_distribution == "Ubuntu" or ansible_os_family == "Debian"
  197.       service:
  198.         name: "{{ item }}"
  199.         state: restarted
  200.         enabled: true
  201.       with_items:
  202.        - "{{ service }}"
  203.        
  204.        
  205. ### Output ###
  206.  
  207. PLAY [Variable checking] ***********************************************************************************************
  208.  
  209. TASK [Gathering Facts] *************************************************************************************************
  210. ok: [172.31.45.6]
  211. ok: [172.31.10.148]
  212.  
  213. TASK [Setting dynamic variable for Amazon] *****************************************************************************
  214. ok: [172.31.10.148]
  215. skipping: [172.31.45.6]
  216.  
  217. TASK [Setting dynamic variable for Ubuntu] *****************************************************************************
  218. skipping: [172.31.10.148]
  219. ok: [172.31.45.6]
  220.  
  221. TASK [Install httpd package for Amazon OS] *****************************************************************************
  222. skipping: [172.31.45.6]
  223. ok: [172.31.10.148]
  224.  
  225. TASK [Copy the content for index.html] *********************************************************************************
  226. skipping: [172.31.45.6]
  227. ok: [172.31.10.148]
  228.  
  229. TASK [Amazon OS service restart and enable] ****************************************************************************
  230. skipping: [172.31.45.6] => (item=apache2)
  231. skipping: [172.31.45.6] => (item=php8.1-fpm)
  232. skipping: [172.31.45.6]
  233. changed: [172.31.10.148] => (item=httpd)
  234. changed: [172.31.10.148] => (item=php-fpm)
  235.  
  236. TASK [Install httpd package for ubuntu OS] *****************************************************************************
  237. skipping: [172.31.10.148]
  238. ok: [172.31.45.6]
  239.  
  240. TASK [Copy the content for index.html] *********************************************************************************
  241. skipping: [172.31.10.148]
  242. ok: [172.31.45.6]
  243.  
  244. TASK [Ubuntu  OS service restart and enable] ***************************************************************************
  245. skipping: [172.31.10.148] => (item=httpd)
  246. skipping: [172.31.10.148] => (item=php-fpm)
  247. skipping: [172.31.10.148]
  248. changed: [172.31.45.6] => (item=apache2)
  249. changed: [172.31.45.6] => (item=php8.1-fpm)
  250.  
  251. PLAY RECAP *************************************************************************************************************
  252. 172.31.10.148              : ok=5    changed=1    unreachable=0    failed=0    skipped=4    rescued=0    ignored=0
  253. 172.31.45.6                : ok=5    changed=1    unreachable=0    failed=0    skipped=4    rescued=0    ignored=0
  254.  
  255.  
  256.  
  257.  
  258. #### group_vars ####
  259.  
  260.  
  261.  
  262. [ec2-user@ip-172-31-10-148 ~]$ cat group_vars/amazon
  263. ---
  264.  
  265. package:
  266.  - httpd
  267.   - php-fpm
  268. service:
  269.  - httpd
  270.   - php-fpm
  271. owner: apache
  272. group: apache
  273. [ec2-user@ip-172-31-10-148 ~]$
  274. [ec2-user@ip-172-31-10-148 ~]$
  275. [ec2-user@ip-172-31-10-148 ~]$
  276. [ec2-user@ip-172-31-10-148 ~]$ cat group_vars/ubuntu
  277. ---
  278.  
  279.  
  280. package:
  281.  - apache2
  282.   - php-fpm
  283. service:
  284.  - apache2
  285.   - php8.1-fpm
  286. owner: www-data
  287. group: www-data
  288.  
  289.  
  290.  
  291. #### group_vars playbook ####
  292.  
  293.  
  294. [ec2-user@ip-172-31-10-148 ~]$ cat main.yml
  295. ---
  296.  
  297. - name: "Variable checking"
  298.   hosts: all
  299.   become: true
  300.   tasks:
  301.     - name: "Install httpd package for Amazon OS"
  302.       when: ansible_distribution == "Amazon" or ansible_os_family == "Redhat"
  303.       yum:
  304.         name: "{{ package }}"
  305.         state: present
  306.     - name: "Copy the content for index.html"
  307.       when: ansible_distribution == "Amazon" or ansible_os_family == "Redhat"
  308.       copy:
  309.         content: "<h1><center>You are seeing a httpd server running in Amazon OS </center></h1>"
  310.         dest: /var/www/html/index.html
  311.         owner: "{{  owner }}"
  312.         group: "{{ group }}"
  313.     - name: "{{ ansible_distribution }} OS service restart and enable"
  314.       when: ansible_distribution == "Amazon" or ansible_os_family == "Redhat"
  315.       service:
  316.         name: "{{ item }}"
  317.         state: restarted
  318.         enabled: true
  319.       with_items:
  320.        - "{{ service }}"
  321.     - name: "Install httpd package for ubuntu OS"
  322.       when: ansible_distribution == "Ubuntu" or ansible_os_family == "Debian"
  323.       apt:
  324.         name: "{{ package }}"
  325.         state: present
  326.         update_cache: true
  327.     - name: "Copy the content for index.html"
  328.       when: ansible_distribution == "Ubuntu" or ansible_os_family == "Debian"
  329.       copy:
  330.         content: "<h1><center>You are seeing a httpd server running in {{ ansible_distribution }} OS </center></h1>"
  331.         dest: /var/www/html/index.html
  332.         owner: "{{  owner }}"
  333.         group: "{{ group }}"
  334.     - name: "Ubuntu  OS service restart and enable"
  335.       when: ansible_distribution == "Ubuntu" or ansible_os_family == "Debian"
  336.       service:
  337.         name: "{{ item }}"
  338.         state: restarted
  339.         enabled: true
  340.       with_items:
  341.        - "{{ service }}"
  342.        
  343.  
  344.  
  345. #### Host vars ####
  346.  
  347.  
  348.  
  349. ### playbook ####
  350.  
  351. - name: "Variable checking"
  352.   hosts: all
  353.   become: true
  354.   tasks:
  355.     - name: "Install httpd package for Amazon OS"
  356.       when: ansible_distribution == "Amazon" or ansible_os_family == "Redhat"
  357.       yum:
  358.         name: "{{ package }}"
  359.         state: present
  360.     - name: "Copy the content for index.html"
  361.       when: ansible_distribution == "Amazon" or ansible_os_family == "Redhat"
  362.       copy:
  363.         content: "<h1><center>You are seeing a httpd server running in Amazon OS </center></h1>"
  364.         dest: /var/www/html/index.html
  365.         owner: "{{  owner }}"
  366.         group: "{{ group }}"
  367.     - name: "{{ ansible_distribution }} OS service restart and enable"
  368.       when: ansible_distribution == "Amazon" or ansible_os_family == "Redhat"
  369.       service:
  370.         name: "{{ item }}"
  371.         state: restarted
  372.         enabled: true
  373.       with_items:
  374.        - "{{ service }}"
  375.     - name: "Install httpd package for ubuntu OS"
  376.       when: ansible_distribution == "Ubuntu" or ansible_os_family == "Debian"
  377.       apt:
  378.         name: "{{ package }}"
  379.         state: present
  380.         update_cache: true
  381.     - name: "Copy the content for index.html"
  382.       when: ansible_distribution == "Ubuntu" or ansible_os_family == "Debian"
  383.       copy:
  384.         content: "<h1><center>You are seeing a httpd server running in {{ ansible_distribution }} OS </center></h1>"
  385.         dest: /var/www/html/index.html
  386.         owner: "{{  owner }}"
  387.         group: "{{ group }}"
  388.     - name: "Ubuntu  OS service restart and enable"
  389.       when: ansible_distribution == "Ubuntu" or ansible_os_family == "Debian"
  390.       service:
  391.         name: "{{ item }}"
  392.         state: restarted
  393.         enabled: true
  394.       with_items:
  395.        - "{{ service }}"
  396.     - name: "Printing the myvars result"
  397.       debug:
  398.         var: myvar
  399.        
  400.        
  401.        
  402. ### Added an instance in inventory ####
  403.  
  404.  
  405. [ec2-user@ip-172-31-10-148 ~]$ cat inventory
  406. [amazon]
  407. 172.31.10.148 ansible_ssh_user="ec2-user" ansible_ssh_private_key_file="aws.pem"
  408. 172.31.0.74 ansible_ssh_user="ec2-user" ansible_ssh_private_key_file="aws.pem"
  409.  
  410. [ubuntu]
  411. 172.31.45.6 ansible_ssh_user="ubuntu"  ansible_ssh_private_key_file="aws.pem"
  412.  
  413.  
  414. ### hostvars tree result ####
  415. [ec2-user@ip-172-31-10-148 ~]$ tree host_vars/
  416. host_vars/
  417. ├── 172.31.10.148
  418. └── 172.31.45.6
  419.  
  420.  
  421. #### Output failed ###
  422.  
  423.  
  424. ec2-user@ip-172-31-10-148 ~]$ ansible-playbook -i inventory main.yml
  425.  
  426. PLAY [Variable checking] ***********************************************************************************************
  427.  
  428. TASK [Gathering Facts] *************************************************************************************************
  429. ok: [172.31.45.6]
  430.  
  431. ok: [172.31.0.74]
  432.  
  433. ok: [172.31.10.148]
  434.  
  435. TASK [Install httpd package for Amazon OS] *****************************************************************************
  436. skipping: [172.31.45.6]
  437. ok: [172.31.10.148]
  438. changed: [172.31.0.74]
  439.  
  440. TASK [Copy the content for index.html] *********************************************************************************
  441. skipping: [172.31.45.6]
  442. ok: [172.31.10.148]
  443. changed: [172.31.0.74]
  444.  
  445. TASK [Amazon OS service restart and enable] ****************************************************************************
  446. skipping: [172.31.45.6] => (item=apache2)
  447. skipping: [172.31.45.6] => (item=php8.1-fpm)
  448. skipping: [172.31.45.6]
  449. changed: [172.31.0.74] => (item=httpd)
  450. changed: [172.31.10.148] => (item=httpd)
  451. changed: [172.31.10.148] => (item=php-fpm)
  452. changed: [172.31.0.74] => (item=php-fpm)
  453.  
  454. TASK [Install httpd package for ubuntu OS] *****************************************************************************
  455. skipping: [172.31.10.148]
  456. skipping: [172.31.0.74]
  457. ok: [172.31.45.6]
  458.  
  459. TASK [Copy the content for index.html] *********************************************************************************
  460. skipping: [172.31.10.148]
  461. skipping: [172.31.0.74]
  462. ok: [172.31.45.6]
  463.  
  464. TASK [Ubuntu  OS service restart and enable] ***************************************************************************
  465. skipping: [172.31.10.148] => (item=httpd)
  466. skipping: [172.31.10.148] => (item=php-fpm)
  467. skipping: [172.31.10.148]
  468. skipping: [172.31.0.74] => (item=httpd)
  469. skipping: [172.31.0.74] => (item=php-fpm)
  470. skipping: [172.31.0.74]
  471. changed: [172.31.45.6] => (item=apache2)
  472. changed: [172.31.45.6] => (item=php8.1-fpm)
  473.  
  474. TASK [Printing the myvars result] **************************************************************************************
  475. ok: [172.31.10.148] => {
  476.     "myvar": "You are seeing the amzon OS host_vars"
  477. }
  478. ok: [172.31.0.74] => {
  479.     "myvar": "VARIABLE IS NOT DEFINED!"
  480. }
  481. ok: [172.31.45.6] => {
  482.     "myvar": "You are seeing the ubuntu host_vars result"
  483. }
  484.  
  485. PLAY RECAP *************************************************************************************************************
  486. 172.31.0.74                : ok=5    changed=3    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0
  487. 172.31.10.148              : ok=5    changed=1    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0
  488. 172.31.45.6                : ok=5    changed=1    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0
  489.  
  490.  
  491.  
  492. #### Added the myvar in group_vars ####
  493.  
  494.  
  495. [ec2-user@ip-172-31-10-148 ~]$ cat group_vars/amazon
  496. ---
  497.  
  498. package:
  499.  - httpd
  500.   - php-fpm
  501. service:
  502.  - httpd
  503.   - php-fpm
  504. owner: apache
  505. group: apache
  506. myvar: "You are seeing the myvar result in group_vars in amazon OS"
  507.  
  508.  
  509.  
  510. #### Final Output showing the priority of host_vars and group_vars ####
  511.  
  512.  
  513. [ec2-user@ip-172-31-10-148 ~]$ ansible-playbook -i inventory main.yml
  514.  
  515. PLAY [Variable checking] ***********************************************************************************************
  516.  
  517. TASK [Gathering Facts] *************************************************************************************************
  518. ok: [172.31.0.74]
  519. ok: [172.31.45.6]
  520. ok: [172.31.10.148]
  521.  
  522. TASK [Install httpd package for Amazon OS] *****************************************************************************
  523. skipping: [172.31.45.6]
  524. ok: [172.31.0.74]
  525. ok: [172.31.10.148]
  526.  
  527. TASK [Copy the content for index.html] *********************************************************************************
  528. skipping: [172.31.45.6]
  529. ok: [172.31.0.74]
  530. ok: [172.31.10.148]
  531.  
  532. TASK [Amazon OS service restart and enable] ****************************************************************************
  533. skipping: [172.31.45.6] => (item=apache2)
  534. skipping: [172.31.45.6] => (item=php8.1-fpm)
  535. skipping: [172.31.45.6]
  536. changed: [172.31.0.74] => (item=httpd)
  537. changed: [172.31.10.148] => (item=httpd)
  538. changed: [172.31.0.74] => (item=php-fpm)
  539. changed: [172.31.10.148] => (item=php-fpm)
  540.  
  541. TASK [Install httpd package for ubuntu OS] *****************************************************************************
  542. skipping: [172.31.10.148]
  543. skipping: [172.31.0.74]
  544. ok: [172.31.45.6]
  545.  
  546. TASK [Copy the content for index.html] *********************************************************************************
  547. skipping: [172.31.10.148]
  548. skipping: [172.31.0.74]
  549. ok: [172.31.45.6]
  550.  
  551. TASK [Ubuntu  OS service restart and enable] ***************************************************************************
  552. skipping: [172.31.10.148] => (item=httpd)
  553. skipping: [172.31.10.148] => (item=php-fpm)
  554. skipping: [172.31.10.148]
  555. skipping: [172.31.0.74] => (item=httpd)
  556. skipping: [172.31.0.74] => (item=php-fpm)
  557. skipping: [172.31.0.74]
  558. changed: [172.31.45.6] => (item=apache2)
  559. changed: [172.31.45.6] => (item=php8.1-fpm)
  560.  
  561. TASK [Printing the myvars result] **************************************************************************************
  562. ok: [172.31.10.148] => {
  563.     "myvar": "You are seeing the amzon OS host_vars"
  564. }
  565. ok: [172.31.0.74] => {
  566.     "myvar": "You are seeing the myvar result in group_vars in amazon OS"
  567. }
  568. ok: [172.31.45.6] => {
  569.     "myvar": "You are seeing the ubuntu host_vars result"
  570. }
  571.  
  572. PLAY RECAP *************************************************************************************************************
  573. 172.31.0.74                : ok=5    changed=1    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0
  574. 172.31.10.148              : ok=5    changed=1    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0
  575. 172.31.45.6                : ok=5    changed=1    unreachable=0    failed=0    skipped=3    rescued=0    ignored=0
  576.  
  577.  
  578.  
  579.  
  580. ------------------------------ Task 2 --------------------------------
  581.  
  582.  
  583.  
  584.  
  585. [ec2-user@ip-172-31-10-148 smtp]$ tree
  586. .
  587. ├── aws.pem
  588. ├── group_vars
  589. │   └── all
  590. ├── hosts
  591. ├── LICENSE
  592. ├── nodejs.yml
  593. ├── passwd.vars
  594. └── README.md
  595.  
  596.  
  597.  
  598.  
  599. #### inventory file #####
  600.  
  601.  
  602. [ec2-user@ip-172-31-10-148 smtp]$ cat hosts
  603. [build]
  604. docker_build  ansible_ssh_host=172.31.10.148 ansible_ssh_user="ec2-user" ansible_ssh_private_key_file="aws.pem"
  605.  
  606. [test]
  607. 172.31.0.74 ansible_ssh_user="ec2-user" ansible_ssh_private_key_file="aws.pem"
  608.  
  609.  
  610.  
  611.  
  612. #### group_vars ####
  613.  
  614. [ec2-user@ip-172-31-10-148 smtp]$ cat group_vars/all
  615. ---
  616.  
  617. packages:
  618.  - docker
  619.   - git
  620. git_url: https://github.com/Chris-luiz-16/Simple-Nodejs-Hello-world-Docker-image-and-container-build.git
  621. clone_dest: /var/nodejs
  622. docker_name: chrisluiz16/nodejs
  623.  
  624.  
  625.  
  626. #### Playbook ####
  627.  
  628. [ec2-user@ip-172-31-10-148 smtp]$ cat nodejs.yml
  629. ---
  630.  
  631. [ec2-user@ip-172-31-10-148 smtp]$ cat nodejs.yml
  632. ---
  633.  
  634. - name: "Docker image build"
  635.   hosts: build
  636.   become: true
  637.   vars_files:
  638.    - passwd.vars
  639.   tasks:
  640.     - name: "Install required packages for docker build"
  641.       yum:
  642.         name: "{{ packages }}"
  643.         state: present
  644.     - name: "Adding a secondary user docker for the user ec2-user"
  645.       user:
  646.         name: ec2-user
  647.         groups: docker
  648.         append: true
  649.     - name: "Starng/enabibling the docker service"
  650.       service:
  651.         name: docker
  652.         state: started
  653.         enabled: true
  654.     - name: "git cloning the nodejs application"
  655.       git:
  656.         repo: "{{ git_url }}"
  657.         dest: "{{ clone_dest }}"
  658.       register: git_status
  659.     - name: "Printing the git commit id's"
  660.       debug:
  661.         var: git_status
  662.     - name: "Login to the dockerhub"
  663.       when: git_status.changed == true
  664.       docker_login:
  665.         username: "{{ docker_user }}"
  666.         password: "{{ docker_passwd }}"
  667.     - name: "Docker image build from Docker file"
  668.       when: git_status.changed == true
  669.       community.docker.docker_image:
  670.         name: "{{ docker_name }}"
  671.         source: build
  672.         build:
  673.           path: "{{ clone_dest }}"
  674.           pull: true
  675.         tag: "{{ item }}"
  676.         push: true
  677.         force_tag: true
  678.         force_source: true
  679.       with_items:
  680.        - latest
  681.         - "{{ git_status.after }}"
  682.       register: build_status
  683.  
  684.     - name: "Deleting the local image build"
  685.       when: git_status.changed == true
  686.       community.docker.docker_image:
  687.         name: "{{ docker_name }}"
  688.         tag: "{{ item }}"
  689.         state: absent
  690.       with_items:
  691.        - latest
  692.         - "{{ git_status.after }}"
  693.     - name: "Logout docker hub"
  694.       when: git_status.changed == true
  695.       docker_login:
  696.         username: "{{ docker_user }}"
  697.         password: "{{ docker_passwd }}"
  698.         state: absent
  699.  
  700. - name: "Running a nodejs container in test server"
  701.   hosts: test
  702.   become: true
  703.   vars_files:
  704.    - passwd.vars
  705.   tasks:
  706.     - name: "Installing required packages for the nodejs docker container"
  707.       yum:
  708.         name: "{{ packages }}"
  709.         state: present
  710.     - name: "Adding docker as a secondary user in ec2-user"
  711.       user:
  712.         name: ec2-user
  713.         groups: docker
  714.         append: true
  715.     - name: " Starting & enable docker service"
  716.       service:
  717.         name: docker
  718.         state: started
  719.         enabled: true
  720.     - name: "Pulling the docker image to the test server"
  721.       community.docker.docker_image:
  722.         name: "{{ docker_name }}"
  723.         source: pull
  724.         force_source: true
  725.       register: image_status
  726.     - name: "Creating a node js container in test server"
  727.       when: image_status.changed == true
  728.       docker_container:
  729.         name: nodejsctr
  730.         image: "{{ docker_name }}"
  731.         recreate: true
  732.         restart_policy: always
  733.         published_ports: 80:3000
  734.     - name: "Remove dangling image if any"
  735.       when: image_status.changed == true
  736.       community.docker.docker_prune:
  737.         images: true
  738.         images_filters:
  739.           dangling: true
  740.     - name: "Inform the client changes has made"
  741.       when: image_status.changed == true
  742.       community.general.mail:
  743.         host: smtp.gmail.com
  744.         port: 587
  745.         sender: "{{ username }}"
  746.         username: "{{ username }}"
  747.         password: "{{ password }}"
  748.         to: Chris Luiz <[email protected]>
  749.         subject: "Docker new container deployed and image pushed to dockerhub on {{ ansible_date_time.day }}/{{ ansible_date_time.month }}/{{ ansible_date_time.year }} at {{ ansible_date_time.time }}"
  750.         body: "A new change to the docker image {{ docker_name }} with tag {{ hostvars.docker_build.git_status.after }} has been successfully pushed to docker hub account and a new container has been deployed."ount and a new container has been deployed."
  751.  
  752.  
  753.  
  754.  
  755. #### Output ####
  756.  
  757.  
  758. [ec2-user@ip-172-31-10-148 smtp]$ ansible-playbook -i hosts nodejs.yml --ask-vault-password
  759. Vault password:
  760.  
  761. PLAY [Docker image build] **********************************************************************************************
  762.  
  763. TASK [Gathering Facts] *************************************************************************************************
  764. [WARNING]: Platform linux on host docker_build is using the discovered Python interpreter at /usr/bin/python3.9, but
  765. future installation of another Python interpreter could change the meaning of that path. See
  766. https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information.
  767. ok: [docker_build]
  768.  
  769. TASK [Install required packages for docker build] **********************************************************************
  770. ok: [docker_build]
  771.  
  772. TASK [Adding a secondary user docker for the user ec2-user] ************************************************************
  773. ok: [docker_build]
  774.  
  775. TASK [Starng/enabibling the docker service] ****************************************************************************
  776. ok: [docker_build]
  777.  
  778. TASK [git cloning the nodejs application] ******************************************************************************
  779. changed: [docker_build]
  780.  
  781. TASK [Printing the git commit id's] ************************************************************************************
  782. ok: [docker_build] => {
  783.    "git_status": {
  784.        "after": "788cc6417f1e8bf82024703773f23c1f369fa617",
  785.        "before": "fccdc6d9abc0922aef927cfb9601a827deeca78b",
  786.        "changed": true,
  787.        "failed": false,
  788.        "remote_url_changed": false
  789.    }
  790. }
  791.  
  792. TASK [Login to the dockerhub] ******************************************************************************************
  793. changed: [docker_build]
  794.  
  795. TASK [Docker image build from Docker file] *****************************************************************************
  796. changed: [docker_build] => (item=latest)
  797. ok: [docker_build] => (item=788cc6417f1e8bf82024703773f23c1f369fa617)
  798.  
  799. TASK [Deleting the local image build] **********************************************************************************
  800. changed: [docker_build] => (item=latest)
  801. changed: [docker_build] => (item=788cc6417f1e8bf82024703773f23c1f369fa617)
  802.  
  803. TASK [Logout docker hub] ***********************************************************************************************
  804. changed: [docker_build]
  805.  
  806. PLAY [Running a nodejs container in test server] ***********************************************************************
  807.  
  808. TASK [Gathering Facts] *************************************************************************************************
  809. [WARNING]: Platform linux on host 172.31.0.74 is using the discovered Python interpreter at /usr/bin/python3.9, but
  810. future installation of another Python interpreter could change the meaning of that path. See
  811. https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information.
  812. ok: [172.31.0.74]
  813.  
  814. TASK [Installing required packages for the nodejs docker container] ****************************************************
  815. ok: [172.31.0.74]
  816.  
  817. TASK [Adding docker as a secondary user in ec2-user] *******************************************************************
  818. ok: [172.31.0.74]
  819.  
  820. TASK [Starting & enable docker service] ********************************************************************************
  821. ok: [172.31.0.74]
  822.  
  823. TASK [Pulling the docker image to the test server] *********************************************************************
  824. changed: [172.31.0.74]
  825.  
  826. TASK [Creating a node js container in test server] *********************************************************************
  827. changed: [172.31.0.74]
  828.  
  829. TASK [Remove dangling image if any] ************************************************************************************
  830. changed: [172.31.0.74]
  831.  
  832. TASK [Inform the client changes has made] ******************************************************************************
  833. ok: [172.31.0.74]
  834.  
  835. PLAY RECAP *************************************************************************************************************
  836. 172.31.0.74                : ok=8    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
  837. docker_build               : ok=10   changed=5    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
Advertisement
Add Comment
Please, Sign In to add comment