Create a playbook called /home/student/ansible/packages.yml that:
Installs the php and mariadb packages on hosts in the dev, test, 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
沒有留言:
張貼留言