2024年4月3日 星期三

【IT Notes】RHCE測驗第四題 INSTALL PACKAGES

Create a playbook called /home/student/ansible/packages.yml that:

  • Installs the php and mariadb packages on hosts in the devtest, and prod host groups

  • Installs the RPM Development Tools package group on hosts in the dev host group

  • Updates all packages to the latest version on hosts in the dev host group

【題前說明】
 這是很入門的考題,目的只是要安裝php和mariadb在node1、node2、node3和node4上;然後node1上再多裝dev tools,並且更新每一台node上的套件至最新版本,全部動作都用ansible來部署。

一.解題過程:

1.建立package.yml,並直接編輯

[student@workstation ansible]$ touch packages.yml
[student@workstation ansible]$ vim packages.yml 
---
  hosts: dev,test,prod
  tasks:
    - name: install php and mariadb
      dnf:
        name: "{{ item }}"
        state: present
      loop:
        - php
        - mariadb
    - name: install devtools on host dev
      dnf:
        name: rpmdevtools       
        state: present
      when: ansible_hostname in groups.dev
    - name: upgrade all packages on host dev
      dnf:
        name: '*'
        state: latest
      when: ansible_hostname in groups.dev
- name: install packages
\\考題這裡要下載的是'@RPM Development Tools',不過我沒有這包套件,就改成官方的rpmdevtools,應該也是類似的東西

二.驗證結果

[student@workstation ansible]$ ansible-playbook -C packages.yml   \\試跑看看是否正常



[student@workstation ansible]$ ansible-playbook packages.yml   \\沒問題就正式
[student@workstation ansible]$ ansible dev -m shell -a 'rpm -qa | grep mariadb'
[student@workstation ansible]$ ansible dev -m shell -a 'rpm -qa  | grep php'
\\驗證dev節點是否有安裝完畢

回傳訊息是這樣就表示安裝成功

三.恢復解題前的環境

回復變更前環境,把剛剛所有安裝的軟體都移除,也順便把本機上的package.yml也刪掉。

[student@workstation ansible]$ vim 04-lab-collection-stop.yml

---
- name: remove packages
  hosts: dev,test,prod
  tasks:
    - name: remove php mariadb
      yum:
        name: "{{ item }}"
        state: absent
      loop:
      - php
      - mariadb
    - name: remove packages groups
      yum:
        name: "rpmdevtools"
        state: absent
      when: inventory_hostname in groups.dev

- name: remove package.yml
  hosts: 127.0.0.1
  tasks:
    - name: remove package.yml
      file:
        path: /home/student/ansible/packages.yml
        state: absent


[student@workstation ansible]$ ansible-playbook 04-lab-collection-stop.yml

沒有留言:

張貼留言

【當兵回憶】八里連

 在我心目中新兵日記是天花板等級的軍教片,並不是說沒有比它更好的作品,只是因為它的在拍攝的背景和時間,也正是我在服兵役的同時,老婆很多次好奇問我,為什麼我總是放在YouTube的新兵日記直播,我都說我只放在有個聲音在,我沒有真正在看,但這行為仔細想想也好多年了吧!台詞聽到我都會背...