主頁 > 作業系統 > RHCE認證(RedHat8)

RHCE認證(RedHat8)

2023-05-12 10:45:04 作業系統

考前說明:所有專案運行程序中出現紅色字體的報錯資訊是正常的,運行完成后看 “failed=0” 就代表執行成功,如果在執行任務期間暫停并且報錯那么代表專案內部書寫格式或者命令輸入錯誤,請認真檢查
此檔案為考前模擬不代表真實考試環境及內容,
環境說明:
system IP Address Role
workstation.lab.example.com 172.25.250.9 Ansible control node
servera.lab.example.com 172.25.250.10 Ansible managed node
serverb.lab.example.com 172.25.250.11 Ansible managed node
serverc.lab.example.com 172.25.250.12 Ansible managed node
serverd.lab.example.com 172.25.250.13 Ansible managed node
bastion.lab.example.com 172.25.250.254 Ansible managed node
帳戶資訊:

這些系統的 IP 地址采用靜態設定,主機名稱決議已配置為決議上方列出的主機名, 請勿更改這些 設定,

foundation0 主機(以下簡稱 f0)的 root 密碼為 Asimov ,f0 上其他用戶的密碼均為 redhat

f0 里面所有虛擬系統的 root 密碼是 redhat ,請勿更改 root 密碼,

所有系統上已預裝了 SSH 密 鑰,允許在不輸?密碼的前提下通過 SSH 進? root 訪問,請勿對系 統上的 root SSH 組態檔進? 任何修改,

Ansible 控制節點上已創建了用戶 student ,此帳戶預裝了 SSH 密鑰,允許在 Ansible 控制節點 和 各個 Ansible 受管節點之間進行 SSH 登錄,請勿對系統上的 student SSH 組態檔進行任何修改, Ansible 被管理節點上已創建了用戶 devops ,用于控制節點連接使用,考試時 ssh 免密和 sudo 提權已 經全部配置好,請勿修改,

初始化虛擬機:

[root@foundation0 ~]# rht-vmctl all -y
[root@foundation0 ~]# rht-vmctl classroom -y

說明:考試需要通過圖形界面對虛擬機進行開機(start),關機(poweroff),重啟(reboot)和重置(rebuilt)操 作,重置虛擬機后,虛擬機所有的配置將會清空,

一、安裝和配置 ansible

按照下方所述,在控制節點 workstation.lab.example.com 上安裝和配置 Ansible:
1.安裝所需的軟體包
2.創建名為/home/student/ansible/inventory 的靜態清單檔案, 以滿足以下需求:
servera 是 dev 主機組的成員
serverb 是 test 主機組的成員
serverc 和 serverd 是 prod 主機組的成員
bastion 是 balancers 主機組的成員
prod 組是 webservers 主機組的成員
3.創建名為/home/student/ansible/ansible.cfg 的組態檔, 以滿足以下要求:
主機清單檔案為/home/student/ansible/inventory
playbook 中使用的角色的位置包括/home/student/ansible/roles

準備作業:
[root@foundation0 ~]# ssh root@workstation
Activate the web console with: systemctl enable --now cockpit.socket

[root@workstation ~]# ssh root@bastion "useradd devops; echo redhat |passwd --stdin 
devops"
Warning: Permanently added 'bastion,172.25.250.254' (ECDSA) to the list of known hosts.
Changing password for user devops.
passwd: all authentication tokens updated successfully.

[root@workstation ~]# for i in server{a..d} bastion;do ssh root@$i "echo 'devops 
ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/devops";done
Warning: Permanently added 'servera,172.25.250.10' (ECDSA) to the list of known hosts.
Warning: Permanently added 'serverb,172.25.250.11' (ECDSA) to the list of known hosts.
Warning: Permanently added 'serverc,172.25.250.12' (ECDSA) to the list of known hosts.
Warning: Permanently added 'serverd,172.25.250.13' (ECDSA) to the list of known hosts.

[root@workstation ~]# yum -y install ansible #若考試已經安裝好了,則不需要安裝了
[root@workstation ~]# su - student #考試要求所有的配置都??個普通?戶進?配置
開始:
[student@workstation ~]$ mkdir ansible
[student@workstation ~]$ cd ansible/
[student@workstation ansible]$ vim inventory

[dev]
servera

[test]
serverb

[prod]
serverc
serverd

[balancers]
bastion

[webservers:children]
prod

[student@workstation ansible]$ cp /etc/ansible/ansible.cfg .
[student@workstation ansible]$ vim ansible.cfg

inventory      = /home/student/ansible/inventory	//取消注釋并更改路徑
roles_path    = /home/student/ansible/roles			//取消注釋并更改路徑
remote_user = devops								//取消注釋并更改用戶
[privilege_escalation]
become=True											//取消注釋即可
become_method=sudo									//取消注釋即可
become_user=root									//取消注釋即可
become_ask_pass=False								//取消注釋即可

[student@workstation ansible]$ mkdir -p /home/student/ansible/roles
[student@workstation ansible]$ ansible all -m ping	//執行后呈現綠色的“ping  pong”即代表成功

二、創建和運行Ansible 臨時命令

請按照下方所述, 創建?個名為/home/student/ansible/adhoc.sh 的 shell 腳本, 該腳將使用
Ansible 臨時命令在各個受管節點上安裝 yum 存盤庫:
存盤庫 1:
存盤庫的名稱為:rh294_BASE
描述為:rh294 base software
基礎 URL 為 http://content.example.com/rhel8.0/x86_64/dvd/BaseOS
GPG 簽名檢查為啟用狀態
GPG 密鑰 URL 為 http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
存盤庫為開啟狀態
存盤庫 2:
存盤庫的名稱為:rh294_STREAM
描述為:rh294 stream software
基礎 URL 為 http://content.example.com/rhel8.0/x86_64/dvd/AppStream
GPG 簽名檢查為啟?狀態
GPG 密鑰 URL 為 http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release
存盤庫為開啟狀態

準備作業:
[student@workstation ansible]$ for i in server{a..d} bastion; do ssh root@$i "rm -rf/etc/yum.repos.d/*"; done				//因虛擬機自帶 yum 源, 需要先洗掉, 考試時不需要操作

Warning: Permanently added 'servera,172.25.250.10' (ECDSA) to the list of known hosts.
Warning: Permanently added 'serverb,172.25.250.11' (ECDSA) to the list of known hosts.
Warning: Permanently added 'serverc,172.25.250.12' (ECDSA) to the list of known hosts.
Warning: Permanently added 'serverd,172.25.250.13' (ECDSA) to the list of known hosts.
Warning: Permanently added 'bastion,172.25.250.254' (ECDSA) to the list of known hosts.

開始:
[student@workstation ansible]$ vim adhoc.sh

#!/bin/bash

ansible all -m yum_repository -a "name=rh294_BASE description='rh294 base software' file=rhed_dvd gpgcheck=yes gpgkey=http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release baseurl=http://content.example.com/rhel8.0/x86_64/dvd/BaseOS/ enabled=yes"

ansible all -m yum_repository -a "name=rh294_STREAM description='rh294 stream software' file=rhed_dvd gpgcheck=yes gpgkey=http://content.example.com/rhel8.0/x86_64/dvd/RPM-GPG-KEY-redhat-release baseurl=http://content.example.com/rhel8.0/x86_64/dvd/AppStream/ enabled=yes"

[student@workstation ansible]$ chmod a+x adhoc.sh
[student@workstation ansible]$ ./adhoc.sh

三、安裝軟體包

創建?個名為 /home/student/ansible/packages.yml 的 playbook:
1.將 php 和 mariadb 軟體包安裝到 dev、test 和 prod 主機組中的主機上
2.將 Development Tools 軟體包組安裝到 dev 主機組中的主機上
3.將 dev 主機組中主機上的所有軟體包更新為最新版本

開始:
[student@workstation ansible]$ vim packages.yml

---
- hosts: dev,test,prod
  tasks:
    - name: install mariadb php
      yum:
        name:
          - php
          - mariadb
        state: present
- hosts: dev
  tasks:
    - name: install Development Tools
      yum:
        name: "@Development Tools"
        state: present
    - name: update pkgs
      yum:
        name: '*'
        state: latest
        
[student@workstation ansible]$ ansible-playbook packages.yml

四、使用 RHEL 系統角色

安裝 RHEL 系統角色軟體包,并創建符合以下條件的 playbook /home/student/ansible/timesync.yml:
1.在所有受管節點上運行
2.使用 timesync 角色
3.配置該角色,以使用當前有效的 NTP 提供
4.配置該角色,以使用時間服務器 classroom.example.com

準備作業:
[root@foundation0 ~]# ssh root@workstation
Activate the web console with: systemctl enable --now cockpit.socket
Last login: Wed Sep 21 07:51:44 2022 from 172.25.250.250

[root@workstation ~]# yum -y install rhel-system-roles
[root@workstation ~]# su - student
[student@workstation ~]$ cd ansible/
[student@workstation ansible]$ cp -r /usr/share/ansible/roles/rhel-system-roles.timesync roles/timesync
開始:
[student@workstation ansible]$ vim timesync.yml

---
- hosts: all
  vars:
    timesync_ntp_servers:
      - hostname: classroom.example.com
        iburst: yes
    timesync_ntp_provider: chrony
  roles:
    - timesync
  post_tasks:
    - name: set timezone
      timezone:
        name: Asia/Shanghai
      notify: restart crond
  handlers:
    - name: restart crond
      service:
        name: crond
        state: restarted
        
[student@workstation ansible]$ ansible-playbook timesync.yml

//說明:若考試沒有要求設定時區,post_tasks 和 handlers 部分可以不?配置;如果重新設定了時區,建議重啟?下
crond 定時器,確保計劃任務運?的時間是對的,

五、使用 RHEL 系統角色

安裝 RHEL 系統角色軟體包,并使用 SeLinux 角色,要求在所有節點運行,將 SELINUX 設定為強制模式,

準備作業:
[student@workstation ansible]$ sudo yum -y install rhel-system-roles
[sudo] password for student: student
[student@workstation ansible]$ ls
adhoc.sh ansible.cfg inventory packages.yml roles timesync.yml

[student@workstation ansible]$ cp -r /usr/share/ansible/roles/rhel-system-roles.selinux
roles/selinux
開始:
[student@workstation ansible]$ vim selinux.yml 		//看題目要求的檔案名是什么

---
- hosts: all
  vars:
    selinux_policy: targeted
    selinux_state: enforcing
  roles:
    - role: selinux
      become: true
      
[student@workstation ansible]$ ansible-playbook selinux.yml

六、使用 Ansible Galaxy 安裝角色

使用 Ansible Galaxy 和要求檔案 /home/student/ansible/roles/requirements.yml,從以下 URL
下載角色并安裝到 /home/student/ansible/roles:
1.http://classroom.example.com/content/haproxy.tar.gz 此角色的名稱應當為 balancer
2.http://classroom.example.com/content/phpinfo.tar.gz 此角色的名稱應當為 phpinfo

準備作業:
將 haproxy.tar.gz 和 phpinfo.tar.gz 下載下來放入 foundation0 下面的/content 目錄下,
起一個新的終端:
[root@foundation0 ~]# cd /content/
[root@foundation0 content]# wget
http://classroom.example.com/content/ansible2.8/haproxy.tar.gz

[root@foundation0 content]# wget
http://classroom.example.com/content/ansible2.8/phpinfo.tar.gz
開始:
[root@foundation0 content]# ssh workstation
[student@workstation ~]$ cd ansible/
[student@workstation ansible]$ ls
adhoc.sh ansible.cfg inventory packages.yml roles selinux.yml timesync.yml

[student@workstation ansible]$ vim roles/requirements.yml

- name: balancer
  src: http://content.example.com/haproxy.tar.gz
- name: phpinfo
  src: http://content.example.com/phpinfo.tar.gz
  
[student@workstation ansible]$ ansible-galaxy install -r roles/requirements.yml -p
roles/

七、創建和使用角色

根據下列要求,在 /home/student/ansible/roles 中創建名為 apache 的角色:
1.httpd 軟體包已安裝,設為在系統啟動時啟用
2.防?墻已啟用并正在運行,并使用允許訪問 Web 服務器的規則
3.模板檔案 index.html.j2 已存在,用于創建具有以下輸出的檔案 /var/www/html/index.html:
Welcome to HOSTNAME on IPADDRESS
其中,HOSTNAME 是受管節點的完全限定域名,IPADDRESS 則是受管節點的 IP 地址,
4.按照下方所述,創建?個使用此角色的 playbook /home/student/ansible/newrole.yml:
該 playbook 在 webservers 主機組中的主機上運行

開始:
[student@workstation ansible]$ ls
adhoc.sh ansible.cfg inventory packages.yml roles selinux.yml timesync.yml

[student@workstation ansible]$ cd roles/
[student@workstation roles]$ ansible-galaxy init apache
- apache was created successfully

[student@workstation roles]$ vim apache/tasks/main.yml

---
# tasks file for apache
- name: install http
  yum:
    name: "{{ item }}"
    state: present
  loop:
    - httpd
    - firewalld
- name: system service
  service:
    name: "{{ item }}"
    state: started
    enabled: yes
  loop:
    - httpd
    - firewalld
- name: firewalld service
  firewalld:
    service: http
    zone: public
    permanent: yes
    immediate: yes
    state: enabled
- name: user templates
  template:
    src: index.html.j2
    dest: /var/www/html/index.html

[student@workstation roles]$ vim apache/templates/index.html.j2

Welcome to {{ ansible_facts['fqdn'] }} on {{ ansible_facts['default_ipv4']['address'] }}

[student@workstation roles]$ cd ..
[student@workstation ansible]$ vim newrole.yml

---
- hosts: webservers
  roles:
    - apache
    
[student@workstation ansible]$ ansible-playbook newrole.yml

[student@workstation ansible]$ curl serverc			//驗證
Welcome to serverc.lab.example.com on 172.25.250.12

[student@workstation ansible]$ curl serverd			//驗證
Welcome to serverd.lab.example.com on 172.25.250.13

八、從 Ansible Galaxy 使用角色

根據下列要求,創建?個名為 /home/student/ansible/roles.yml 的 playbook:
1.playbook 中包含?個 play,該 play 在 balancers 主機組中的主機上運?并將使用 balancer
角色,
此角色配置?項服務,以在 webservers 主機組中的主機之間平衡 Web 服務器請求的負載,
瀏覽到 balancers 主機組中的主機(例如 http:/bastion.lab.example.com/ )將生成以下輸
出:
Welcome to serverc.example.com on 172.25.250.12
重新加載瀏覽器將從另? Web 服務器生成輸出:
Welcome to serverd.example.com on 172.25.250.13
2.playbook 中包含?個 play,該 play 在 webservers 主機組中的主機上運?并將使用 phpinfo
角色,
通過 URL /hello.php 瀏覽到 webservers 主機組中的主機將生成以下輸出:
Hello PHP World from FQDN
其中,FQDN 是主機的完全限定名稱,
例如,瀏覽到 http://serverc.lab.example.com/hello.php 會生成以下輸出:
Hello PHP World from serverc.lab.example.com
另外還有 PHP 配置的各種詳細資訊,如安裝的 PHP 版本等,
同樣,瀏覽到 http://serverd.lab.example.com/hello.php 會生成以下輸出:
Hello PHP World from serverd.lab.example.com
另外還有 PHP 配置的各種詳細資訊,如安裝的 PHP 版本等,

準備作業:
[student@workstation ansible]$ ssh root@bastion 'systemctl stop httpd && systemctl
disable httpd'			//關閉 bastion 主機上的 httpd 服務,以免沖突,考試不需要做
Removed /etc/systemd/system/multi-user.target.wants/httpd.service.
開始:
[student@workstation ansible]$ vim roles.yml

---
- hosts: webservers
  gather_facts: false
  tasks:
    - name: test facts
      setup:

- hosts: balancers
  roles:
    - balancer
- hosts: webservers
  roles:
    - phpinfo

[student@workstation ansible]$ ansible-playbook roles.yml

[student@workstation ansible]$ curl http://bastion.lab.example.com/		//驗證
Welcome to serverc.lab.example.com on 172.25.250.12

[student@workstation ansible]$ curl http://bastion.lab.example.com/		//驗證
Welcome to serverd.lab.example.com on 172.25.250.13

[student@workstation ansible]$ curl http://serverc.lab.example.com/hello.php	//驗證
Hello PHP World form serverc.lab.example.com

[student@workstation ansible]$ curl http://serverd.lab.example.com/hello.php	//驗證
Hello PHP World form serverd.lab.example.com

九、 創建和使用邏輯卷

將創建一個名為/home/student/ansible/lv.yml 的 playbook,它將在所有受管節點上運行以執行下
列任務
1.創建符合以下要求的邏輯卷:
邏輯卷創建在 research 卷組中
邏輯卷名稱為 data
邏輯卷大小為 1500MiB
2.使用 ext4 檔案系統格式化邏輯卷
3.如果無法創建請求的邏輯卷大小,應顯示錯誤訊息
Could not create logical volume of that size,并且應改為使用大小 800MiB,
4.如果卷組 research 不存在 ,應顯示錯誤訊息
Volume group does not exist,
5.不要以任何方式掛載邏輯卷,

準備作業:
[student@workstation ansible]$ vim lvm_pre.yml

---
- hosts: dev,test
  tasks:
    - name: crteam 2G
      parted:
        device: /dev/vdb
        number: 1
        flags: [ lvm ]
        state: present
        part_start: 1MiB
        part_end: 2GiB
    - name: create vg
      lvg:
       vg: research
       pvs: /dev/vdb1

- hosts: prod
  tasks:
    - name: crteam 1G
      parted:
        device: /dev/vdb
        number: 1
        flags: [ lvm ]
        state: present
        part_start: 1MiB
        part_end: 1GiB
    - name: create vg
      lvg:
       vg: research
       pvs: /dev/vdb1

[student@workstation ansible]$ ansible-playbook lvm_pre.yml

[student@workstation ansible]$ for i in server{a..d};do ssh root@$i 'vgs';done	//驗證
 
 VG #PV #LV #SN Attr VSize VFree
 research 1 0 0 wz--n- <2.00g <2.00g
 VG #PV #LV #SN Attr VSize VFree
 research 1 0 0 wz--n- <2.00g <2.00g
 VG #PV #LV #SN Attr VSize VFree 
 research 1 0 0 wz--n- 1020.00m 1020.00m
 VG #PV #LV #SN Attr VSize VFree 
 research 1 0 0 wz--n- 1020.00m 1020.00m
開始:
[student@workstation ansible]$ vim lv.yml

---
- hosts: all
  tasks:
    - name: create logical volume
      block:
        - name: create lvm 1500m
          lvol:
            vg: research
            lv: data
            size: 1500m
      rescue:
        - debug:
            msg: Could not create logical volume of that size
        - name: create lvm 800m
          lvol:
            vg: research
            lv: data
            size: 800m
      always:
        - name: format lvm
          filesystem:
            fstype: ext4
            dev: /dev/research/data
          when: "'research' in ansible_facts['lvm']['vgs']"
        - name: serche not
          debug:
            msg: Volume group does not exist
          when: "'research' not in ansible_facts['lvm']['vgs']"

[student@workstation ansible]$ ansible-playbook lv.yml

[student@workstation ansible]$ for i in server{a..d}; do ssh root@$i 'lvs'; done	//驗證

 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
 data research -wi-a----- 1.46g 
 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
 data research -wi-a----- 1.46g 
 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
 data research -wi-a----- 800.00m 
 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
 data research -wi-a----- 800.00m

十、創建磁區

說明:由于只有一塊可用磁盤,與上一道題沖突,需要重置磁盤后再做
創建一個名為/home/student/ansible/parted.yml 的 playbook,它將在 dev 主機組上運行下列任務
1.如果磁盤/dev/vdd 存在,則創建 1500m 磁區
2.如果無法創建請求的磁區大小,應顯示錯誤訊息
Could not create partition of that size,并且應改為使用大小 800m,
3.如果磁盤/dev/vdd 不存在 ,應顯示錯誤訊息
disk /dev/vdd does not exist,
4.如果磁盤/dev/vdb 存在,則創建 1500m 磁區
5.如果無法創建請求的磁區大小,應顯示錯誤訊息
Could not create partition of that size,并且應改為使用大小 800m,
6.最后磁區都要格式化為 ext4 檔案系統,并掛載在/mnt/fs01 上

準備作業:

//先將 dev 環境主機 servera 的/dev/vdb 硬碟磁區洗掉,即題目所說重置磁盤,

[student@workstation ansible]$ ssh root@servera
[root@servera ~]# lvremove /dev/research/data
[root@servera ~]# vgremove research
[root@servera ~]# pvremove /dev/vdb1
[root@servera ~]# pvs
[root@servera ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0 10G 0 disk
└─vda1 252:1 0 10G 0 part /
vdb 252:16 0 5G 0 disk
└─vdb1 252:17 0 2G 0 part
 └─research-data 253:0 0 1.5G 0 lvm 
 
[root@servera ~]# fdisk /dev/vdb
輸入: d
輸入: w

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

如果以上命令執行后提示需要重啟則先執行 reboot 命令,然后再次進行洗掉磁區動作,待 servera 主機重啟完成
后執行以下命令

[root@servera ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0 10G 0 disk
└─vda1 252:1 0 10G 0 part /
vdb 252:16 0 5G 0 disk

[root@servera ~]# exit
開始:
[student@workstation ansible]$ vim parted.yml

---
- hosts: dev
  tasks:
    - name: create partition
      block:
        - name: create 1500m vdd
          parted:
            device: /dev/vdd
            number: 1
            state: present
            part_end: 1501MiB
      rescue:
        - debug:
            msg: Could not create partition of that size
        - name: create 800m vdd
          parted:
            device: /dev/vdd
            number: 1
            state: present
            part_end: 801MiB
      always:
        - name: format partition
          filesystem:
            fstype: ext4
            dev: /dev/vdd1
        - name: mount device
          mount:
            path: /mnt/fs01
            src: /dev/vdd1
            fstype: ext4
            opts: defaults
            state: mounted
      when: "'vdd' in ansible_facts['devices']"

    - debug:
        msg: disk /dev/vdd does not exist
      when: "'vdd' not in ansible_facts['devices']"

    - name: create partition
      block:
        - name: create 1500m vdb
          parted:
            device: /dev/vdb
            number: 1
            state: present
            part_end: 1501MiB
      rescue:
        - debug:
            msg: Could not create partition of that size
        - name: create 800m vdb
          parted:
            device: /dev/vdb
            number: 1
            state: present
            part_end: 801MiB
      always: 
        - name: format partition
          filesystem:
            fstype: ext4
            dev: /dev/vdb1
        - name: mount device
          mount:
            path: /mnt/fs01
            src: /dev/vdb1
            fstype: ext4
            opts: defaults
            state: mounted
      when:
        - "'vdb' in ansible_facts['devices']"
        - "'vdd' not in ansible_facts['devices']"

    - debug:
        msg: disk /dev/vdb does not exist
      when: "'vdb' not in ansible_facts['devices']"

[student@workstation ansible]$ ansible-playbook parted.yml

十一、生成主機檔案

將?個初始模板檔案從 http://172.25.254.254/content/hosts.j2 下載到/home/student/ansible
1.完成該模板,以便用它生成以下檔案:針對每個清單主機包含??內容,其格式與 /etc/hosts
相同,
2.創建名為 /home/student/ansible/hosts.yml 的 playbook,它將使用此模板在 dev 主機組中的
主機上生成檔案 /etc/myhosts,
3.該 playbook 運行后,dev 主機組中主機上的檔案/etc/myhosts 應針對每個受管主機包含一行內
容,

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.10 servera.lab.example.com servera
172.25.254.11 serverb.lab.example.com serverb
172.25.254.12 serverc.lab.example.com serverc
172.25.254.13 serverd.lab.example.com serverd
172.25.250.254 bastion.lab.example.com bastion

注意:清單主機名稱的顯示順序不重要,

準備作業:
[student@workstation ~]$ exit
[root@foundation0 ~]# cd /content/
[root@foundation0 content]# vim hosts.j2

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
{% for host in groups.all %}
{{ hostvars[host].ansible_enp1s0.ipv4.address }} {{ hostvars[host].ansible_fqdn }}
{{ hostvars[host].ansible_hostname }}
{% endfor %}

[root@foundation0 ~]# ssh workstation
[student@workstation ~]$ cd ansible/
開始:
[student@workstation ansible]$ wget http://172.25.254.254/content/hosts.j2

[student@workstation ansible]$ vim hosts.yml

---
- hosts: all

- hosts: dev
  tasks:
    - name: copy hosts.j2 to dev
      template:
        src: hosts.j2
        dest: /etc/myhosts

[student@workstation ansible]$ ansible-playbook hosts.yml

[student@workstation ansible]$ ssh root@servera 'cat /etc/myhosts'		//驗證

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.250.10 servera.lab.example.com servera
172.25.250.11 serverb.lab.example.com serverb
172.25.250.254 bastion.lab.example.com bastion
172.25.250.12 serverc.lab.example.com serverc
172.25.250.13 serverd.lab.example.com serverd

//注意:清單主機名稱的顯示順序不重要,

十二、修改檔案內容

按照下方所述,創建一個名為 /home/student/ansible/issue.yml 的 playbook:
1.該 playbook 將在所有清單主機上運行
2.該 playbook 會將 /etc/issue 的內容替換為下方所示的一行文本:
在 dev 主機組中的主機上,這行文本顯示為:Development
在 test 主機組中的主機上,這行文本顯示為:Test
在 prod 主機組中的主機上,這行文本顯示為:Production

開始:
[student@workstation ansible]$ vim issue.yml

---
- hosts: all
  tasks:
    - name: write something to /etc/issue
      copy:
        content: |
          {% if 'dev' in group_names %}
          Development
          {% elif 'test' in group_names %}
          Test
          {% elif 'prod' in group_names %}
          Production
          {% endif %}
        dest: /etc/issue

[student@workstation ansible]$ ansible-playbook issue.yml
[student@workstation ansible]$ for i in server{a..d} bastion ; do ssh root@$i "cat
/etc/issue" ; done			//驗證

Development
Test
Production
Production

十三、創建 web 內容目錄

按照下方所述,創建一個名為 /home/student/ansible/webcontent.yml 的 playbook:
1.該 playbook 在 dev 主機組中的受管節點上運行
2.創建符合下列要求的目錄 /webdev:
擁有組為 devops 組
具有常規權限:owner=read+write+execute,group=read+write+execute,other=read+execute
具有特殊權限: set group ID
3.用符號鏈接將 /var/www/html/webdev 鏈接到 /webdev
4.創建檔案 /webdev/index.html,其中包含如下所示的單行文本:Development
5.在 dev 主機組中主機上瀏覽此目錄(例如 http://servera.lab.example.com/webdev/ )將生成
以 下輸出:
Development

開始:
[student@workstation ansible]$ vim webcontent.yml

---
- hosts: dev
  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: present
    - name: enabled httpd
      service:
        name: httpd
        enabled: yes
        state: started
    - name: enabled 80/tcp
      firewalld:
        service: http
        zone: public
        permanent: yes
        immediate: yes
        state: enabled
    - name: create /webdev
      file:
        path: /webdev
        state: directory
        owner: root
        group: devops
        mode: '2775'
    - name: create file
      copy:
        content: "Devlopment\n"
        dest: /webdev/index.html
    - name: create soft link
      file:
        src: /webdev
        dest: /var/www/html/webdev
        state: link
    - name: modify sefcontext
      sefcontext:
        target: '/webdev(/.*)?'
        setype: httpd_sys_content_t
        state: present

    - name: Apply new SELinux file context to filesystem
      command: restorecon -irv /webdev

[student@workstation ansible]$ ansible-playbook webcontent.yml
[student@workstation ansible]$ curl http://servera.lab.example.com/webdev/		//驗證

Devlopment

十四、生成硬體報告

創建一個名為 /home/student/ansible/hwreport.yml 的 playbook,它將在所有受管節點上生成含
有以 下資訊的輸出檔案 /root/hwreport.txt:
清單主機名稱
以 MB 表示的總記憶體大小
BIOS 版本
磁盤設備 vda 的大小
磁盤設備 vdb 的大小
輸出檔案中的每一行含有一個 key=value 對,
您的 playbook 應當:
1.從 http://172.25.254.254/content/hwreport.empty 下載檔案,并將它保存為
/root/hwreport.txt
2.使用正確的值修改 /root/hwreport.txt
3.如果硬體項不存在,相關的值應設為 NONE

準備作業:
[student@workstation ansible]$ exit
[root@foundation0 ~]# cd /content/
[root@foundation0 content]# vim hwreport.empty

hostname = inventoryhostname
memory = memory_in_MB
bios_version = BIOS_version
vda_size = disk_vda_size
vdb_size = disk_vdb_size
開始:
[root@foundation0 ~]# ssh workstation
[student@workstation ~]$ cd ansible/
[student@workstation ansible]$ vim hwreport.yml

---
- hosts: all
  tasks:
    - name: create report file
      get_url:
        url: http://content.example.com/hwreport.empty
        dest: /root/hwreport.txt
    - name: get inventory
      replace:
        path: /root/hwreport.txt
        regexp: 'inventoryhostname'
        replace: '{{ inventory_hostname }}'
    - name: get memory
      replace:
        path: /root/hwreport.txt
        regexp: 'memory_in_MB'
        replace: '{{ ansible_facts["memtotal_mb"] | string }}'
    - name: get bios
      replace:
        path: /root/hwreport.txt
        regexp: 'BIOS_version'
        replace: '{{ ansible_facts["bios_version"] }}'

    - name: get vda
      replace:
        path: /root/hwreport.txt
        regexp: 'disk_vda_size'
        replace: '{{ ansible_facts["devices"]["vda"]["size"] | default("NONE") }}'
    - name: get vdb
      replace:
        path: /root/hwreport.txt
        regexp: 'disk_vdb_size'
        replace: '{{ ansible_facts["devices"]["vdb"]["size"] | default("NONE") }}'

[student@workstation ansible]$ ansible-playbook hwreport.yml

[student@workstation ansible]$ for i in server{a..d} bastion;do ssh root@$i 'cat
/root/hwreport.txt';done		//驗證

hostname = servera
memory = 821
bios_version = 1.11.1-4.module+el8.1.0+4066+0f1aadab
vda_size = 10.00 GB
vdb_size = 5.00 GB
hostname = serverb
memory = 821
bios_version = 1.11.1-4.module+el8.1.0+4066+0f1aadab
vda_size = 10.00 GB
vdb_size = 5.00 GB
hostname = serverc
memory = 821
bios_version = 1.11.1-4.module+el8.1.0+4066+0f1aadab
vda_size = 10.00 GB
vdb_size = 5.00 GB
hostname = serverd
memory = 821
bios_version = 1.11.1-4.module+el8.1.0+4066+0f1aadab
vda_size = 10.00 GB
vdb_size = 5.00 GB
hostname = bastion
memory = 821
bios_version = 1.11.1-4.module+el8.1.0+4066+0f1aadab
vda_size = 10.00 GB
vdb_size = NONE

十五、創建密碼庫

按照下方所述,創建一個 Ansible 庫來存盤用戶密碼:
1.庫名稱為 /home/student/ansible/locker.yml
2.庫中含有兩個變數,名稱如下:
pw_developer,值為 Imadev
pw_manager,值為 Imamgr
3.用于加密和解密該庫的密碼為 whenyouwishuponastar
4.密碼存盤在檔案 /home/student/ansible/secret.txt 中

開始:
[student@workstation ansible]$ vim locker.yml

pw_developer: Imadev
pw_manager: Imamgr

[student@workstation ansible]$ echo whenyouwishuponastar > secret.txt
[student@workstation ansible]$ ansible-vault --vault-password-file=secret.txt encrypt
locker.yml

[student@workstation ansible]$ ansible-vault view locker.yml
Vault password: # 輸入密碼
pw_developer: Imadev
pw_manager: Imamgr

十六、創建用戶帳戶

1.從 http://172.25.254.254/content/user_list.yml 下載要創建的用戶的串列,并將它保存到
/home/student/ansible,用戶密碼來自于/home/student/ansible/locker.yml 檔案,
2.創建名為/home/student/ansible/users.yml 的 playbook,從而按以下所述創建用戶帳戶:
職位描述為 developer 的用戶應當:
在 dev 和 test 主機組中的受管節點上創建
從 pw_developer 變數分配密碼
是附加組 student 的成員
職位描述為 manager 的用戶應當:
在 prod 主機組中的受管節點上創建
從 pw_manager 變數分配密碼
是附加組 opsmgr 的成員
3.密碼應采用 SHA512 哈希格式,
4.您的 playbook 應能夠在本次考試中使用在其他位置創建的庫密碼檔案
/home/student/ansible/secret.txt 正常運行,

準備作業:
[student@workstation ansible]$ exit
[root@foundation0 ~]# cd /content/
[root@foundation0 content]# vim user_list.yml

users:
 - name: bob
   job: developer
 - name: sally
   job: manager
 - name: fred 
   job: developer

[root@foundation0 content]# ssh workstation
[student@workstation ~]$ cd ansible/
開始:
[student@workstation ansible]$ wget http://172.25.254.254/content/user_list.yml
[student@workstation ansible]$ vim users.yml

---
- hosts: dev,test
  vars_files:
    - locker.yml
    - user_list.yml
  tasks:
    - name: student group
      group:
        name: student
        state: present
    - name: create user in developer
      user:
        name: "{{ item.name }}"
        groups: student
        password: "{{ pw_developer | password_hash('sha512') }}"
      loop: "{{ users }}"
      when: item.job == "developer"
- hosts: prod
  vars_files:
    - locker.yml
    - user_list.yml
  tasks:
    - name: group
      group:
        name: opsmgr
        state: present
    - name: create user
      user:
        name: "{{ item.name }}"
        groups: opsmgr
        password: "{{ pw_manager | password_hash('sha512') }}"
      loop: "{{ users }}"
      when: item.job == "manager"

[student@workstation ansible]$ ansible-playbook --vault-password-file=secret.txt users.yml

十七、更新 ansible 庫的密鑰

按照下方所述,更新現有 Ansible 庫的密鑰:
1.從 http://172.25.254.254/content/salaries.yml 下載 Ansible 庫到 /home/student/ansible
2.當前的庫密碼為 insecure4sure
3.新的庫密碼為 bbe2de98389b
4.庫使用新密碼保持加密狀態

準備作業:
[student@workstation ansible]$ ansible-vault create salaries.yml

New Vault password: # 輸入密碼 insecure4sure

Confirm New Vault password: # 輸入密碼 insecure4sure

this is a test file # 內容任意

[student@workstation ansible]$ exit
[root@foundation0 ~]# cd /content/
[root@foundation0 content]# scp workstation:/home/student/ansible/salaries.yml salaries.yml

[root@foundation0 content]# chmod 644 salaries.yml
[root@foundation0 content]# ssh workstation
[student@workstation ~]$ cd ansible/
[student@workstation ansible]$ rm -f salaries.yml
開始:
[student@workstation ansible]$ wget http://172.25.254.254/content/salaries.yml

[student@workstation ansible]$ ansible-vault rekey salaries.yml

Vault password: # 輸入舊密碼 insecure4sure

New Vault password: # 輸入新密碼 bbe2de98389b

Confirm New Vault password: # 輸入新密碼 bbe2de98389b
Rekey successful

十八、創建計劃任務

為 natasha 創建一個計劃任務,要求每隔 2 分鐘執行一次 echo hello,playbook 檔案名為
cron.yml,該 playbook 在 dev 主機組上運行,

開始:
[student@workstation ansible]$ vim cron.yml

---
- hosts: dev
  tasks:
    - name: create user
      user:
        name: natasha
        state: present
    - name: create cron
      cron:
        minute: "*/2"
        user: natasha
        job: "echo hello"		//復制題目給的任務即可
        
[student@workstation ansible]$ ansible-playbook cron.yml

[student@workstation ansible]$ ssh root@servera		//驗證
[root@servera ~]# crontab -l -u natasha
#Ansible: exec tasks every 2 minute
*/2 * * * * echo hello

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/552303.html

標籤:Linux

上一篇:shell自動化腳本,啟動、停止應用程式

下一篇:返回列表

標籤雲
其他(158918) Python(38128) JavaScript(25420) Java(18033) C(15226) 區塊鏈(8265) C#(7972) AI(7469) 爪哇(7425) MySQL(7179) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5871) 数组(5741) R(5409) Linux(5339) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4572) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2433) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) .NET技术(1972) 功能(1967) Web開發(1951) HtmlCss(1936) python-3.x(1918) C++(1915) 弹簧靴(1913) xml(1889) PostgreSQL(1875) .NETCore(1860) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • CA和證書

    1、在 CentOS7 中使用 gpg 創建 RSA 非對稱密鑰對 gpg --gen-key #Centos上生成公鑰/密鑰對(存放在家目錄.gnupg/) 2、將 CentOS7 匯出的公鑰,拷貝到 CentOS8 中,在 CentOS8 中使用 CentOS7 的公鑰加密一個檔案 gpg -a ......

    uj5u.com 2020-09-10 00:09:53 more
  • Kubernetes K8S之資源控制器Job和CronJob詳解

    Kubernetes的資源控制器Job和CronJob詳解與示例 ......

    uj5u.com 2020-09-10 00:10:45 more
  • VMware下安裝CentOS

    VMware下安裝CentOS 一、軟硬體準備 1 Centos鏡像準備 1.1 CentOS鏡像下載地址 下載地址 1.2 CentOS鏡像下載程序 點擊下載地址進入如下圖的網站,選擇需要下載的版本,這里選擇的是Centos8,點擊如圖所示。 決定選擇Centos8后,選擇想要的鏡像源進行下載,此 ......

    uj5u.com 2020-09-10 00:12:10 more
  • 如何使用Grep命令查找多個字串

    如何使用Grep 命令查找多個字串 大家好,我是良許! 今天向大家介紹一個非常有用的技巧,那就是使用 grep 命令查找多個字串。 簡單介紹一下,grep 命令可以理解為是一個功能強大的命令列工具,可以用它在一個或多個輸入檔案中搜索與正則運算式相匹配的文本,然后再將每個匹配的文本用標準輸出的格式 ......

    uj5u.com 2020-09-10 00:12:28 more
  • git配置http代理

    git配置http代理 經常遇到克隆 github 慢的問題,這里記錄一下幾種配置 git 代理的方法,解決 clone github 過慢。 目錄 git配置代理 git單獨配置github代理 git配置全域代理 配置終端環境變數 git配置代理 主要使用 git config 命令 git單獨 ......

    uj5u.com 2020-09-10 00:12:33 more
  • Linux npm install 裝包時提示Error EACCES permission denied解

    npm install 裝包時提示Error EACCES permission denied解決辦法 ......

    uj5u.com 2020-09-10 00:12:53 more
  • Centos 7下安裝nginx,使用yum install nginx,提示沒有可用的軟體包

    Centos 7下安裝nginx,使用yum install nginx,提示沒有可用的軟體包。 18 (flaskApi) [root@67 flaskDemo]# yum -y install nginx 19 已加載插件:fastestmirror, langpacks 20 Loading ......

    uj5u.com 2020-09-10 00:13:13 more
  • Linux查看服務器暴力破解ssh IP

    在公網的服務器上經常遇到別人爆破你服務器的22埠,用來挖礦或者干其他嘿嘿嘿的事情~ 這種情況下正確的做法是: 修改默認ssh的22埠 使用設定密鑰登錄或者白名單ip登錄 建議服務器密碼為復雜密碼 創建普通用戶登錄服務器(root權限過大) 建立堡壘機,實作統一管理服務器 統計爆破IP [root ......

    uj5u.com 2020-09-10 00:13:17 more
  • CentOS 7系統常見快捷鍵操作方式

    Linux系統中一些常見的快捷方式,可有效提高操作效率,在某些時刻也能避免操作失誤帶來的問題。 ......

    uj5u.com 2020-09-10 00:13:31 more
  • CentOS 7作業系統目錄結構介紹

    作業系統存在著大量的資料檔案資訊,相應檔案資訊會存在于系統相應目錄中,為了更好的管理資料資訊,會將系統進行一些目錄規劃,不同目錄存放不同的資源。 ......

    uj5u.com 2020-09-10 00:13:35 more
最新发布
  • RHCE認證(RedHat8)

    考前說明:所有專案運行程序中出現紅色字體的報錯資訊是正常的,運行完成后看 “failed=0” 就代表執行成功,如果在執行任務期間暫停并且報錯那么代表專案內部書寫格式或者命令輸入錯誤,請認真檢查 此檔案為考前模擬不代表真實考試環境及內容。 環境說明: | system | IP Address | ......

    uj5u.com 2023-05-12 10:45:04 more
  • shell自動化腳本,啟動、停止應用程式

    #!/usr/bin/env bash # 常量初始化 set_runtime_vars(){ # 日期時間 Now_Date=`date +"%Y-%m-%d %H:%M:%S"` # 服務狀態 Service_Status=( "服務已開啟" "服務已停止" ) # 動作狀態 Action_St ......

    uj5u.com 2023-05-10 10:22:47 more
  • 服務器卡死,ssh連接不上-finalshell導致

    問題:客戶反饋服務器ssh連接不上 設備資訊:Asianux Server release 7.3.1512(Lotus) 定位:通過虛擬機控制臺重啟服務器,發現能登錄虛擬機,查看服務器各種狀態都正常。 客戶使用一段時間后又卡死,ssh連接不上,登錄發現行程數有一萬多個,導致記憶體不足,服務器卡斷 查 ......

    uj5u.com 2023-05-10 10:22:42 more
  • 筆記本通過HDMI介面擴展顯示幕,微信/Outlook等界面模糊變清晰的解

    1、筆記本擴展顯示幕,微信界面顯示字體模糊如何解決? 解決方案: 第一步:滑鼠右鍵打開微信快捷方式,選擇‘屬性’,找到‘兼容性’,選擇‘ 更改高DPI設定’ 第二步:高DPI縮放替代:勾選? ‘替代高DPI縮放行為’ 第三步:點擊“確定”。 第四步:重新啟動微信,微信界面的字體顯示清晰了 2、問題描 ......

    uj5u.com 2023-05-10 10:22:37 more
  • 筆記本通過HDMI介面擴展顯示幕,微信/Outlook等界面模糊變清晰的解

    1、筆記本擴展顯示幕,微信界面顯示字體模糊如何解決? 解決方案: 第一步:滑鼠右鍵打開微信快捷方式,選擇‘屬性’,找到‘兼容性’,選擇‘ 更改高DPI設定’ 第二步:高DPI縮放替代:勾選? ‘替代高DPI縮放行為’ 第三步:點擊“確定”。 第四步:重新啟動微信,微信界面的字體顯示清晰了 2、問題描 ......

    uj5u.com 2023-05-10 10:22:23 more
  • shell自動化腳本,啟動、停止應用程式

    #!/usr/bin/env bash # 常量初始化 set_runtime_vars(){ # 日期時間 Now_Date=`date +"%Y-%m-%d %H:%M:%S"` # 服務狀態 Service_Status=( "服務已開啟" "服務已停止" ) # 動作狀態 Action_St ......

    uj5u.com 2023-05-10 10:21:45 more
  • 服務器卡死,ssh連接不上-finalshell導致

    問題:客戶反饋服務器ssh連接不上 設備資訊:Asianux Server release 7.3.1512(Lotus) 定位:通過虛擬機控制臺重啟服務器,發現能登錄虛擬機,查看服務器各種狀態都正常。 客戶使用一段時間后又卡死,ssh連接不上,登錄發現行程數有一萬多個,導致記憶體不足,服務器卡斷 查 ......

    uj5u.com 2023-05-10 10:21:42 more
  • 快速上手Linux核心命令(十一):Linux用戶相關命令

    (Linux用戶相關命令) 前言 上一篇中已經預告,我們這篇主要說一說Linux中用戶相關的命令。 可能各位小伙伴用的Windows會比較多一點,在Windows作業系統中,我們知道有Administrator用戶,Administrator 是Windows系統中的管理員用戶,有著最高的權限。 用 ......

    uj5u.com 2023-05-07 09:35:05 more
  • 快速上手Linux核心命令(十一):Linux用戶相關命令

    (Linux用戶相關命令) 前言 上一篇中已經預告,我們這篇主要說一說Linux中用戶相關的命令。 可能各位小伙伴用的Windows會比較多一點,在Windows作業系統中,我們知道有Administrator用戶,Administrator 是Windows系統中的管理員用戶,有著最高的權限。 用 ......

    uj5u.com 2023-05-07 09:34:12 more
  • Linux 記憶體管理 pt.2

    哈嘍大家好我是咸魚,在《Linux 記憶體管理 pt.1》中我們學習了什么是物理記憶體、虛擬記憶體,了解了記憶體映射、缺頁例外等內容 那么今天我們來接著學習 Linux 記憶體管理中的多級頁表和大頁 多級頁表&大頁 在《Linux 記憶體管理 pt.1》中我們知道了內核為每個行程都維護了一張頁表,這張頁表用來記 ......

    uj5u.com 2023-05-05 09:49:41 more