#!/bin/bash

# 引用函数
source /usr/local/lib/exam.lib

function check_apache_role() {
  echo "Check role apache :"

  pad "  Check package httpd installed"
  ansible $1 -a 'dnf list httpd' &>/dev/null && print_PASS && let grade++ || print_FAIL

  pad "  Check service httpd enabled and started"
  ansible $1 -m shell -a 'systemctl is-enabled httpd && systemctl is-active httpd' &>/dev/null && print_PASS && let grade++ || print_FAIL

  pad "  Check package firewalld installed"
  ansible $1 -a 'dnf list firewalld' &>/dev/null && print_PASS && let grade++ || print_FAIL

  pad "  Check service firewalld enabled and started"
  ansible $1 -m shell -a 'systemctl is-enabled firewalld && systemctl is-active firewalld' &>/dev/null && print_PASS && let grade++ || print_FAIL

  pad "  Check firewall allow http service"
  ansible $1 -m shell -a "firewall-cmd --list-all|egrep 'services:.* http ' && firewall-cmd --list-all --permanent |egrep 'services:.* http '" &>/dev/null && print_PASS && let grade++ || print_FAIL

  pad "  Check template index.html.j2"
  [[ "$(cat roles/apache/templates/index.html.j2 2>/dev/null )" == "Welcome to {{ ansible_fqdn }} on {{ ansible_default_ipv4.address }}" ]] && print_PASS && let grade++ || print_FAIL

  pad "  Check website content"
  (curl http://serverc |grep  'Welcome to serverc.lab.example.com on 172.25.250.12' && curl http://serverd |grep  'Welcome to serverd.lab.example.com on 172.25.250.13') &>/dev/null && print_PASS && let grade++ || print_FAIL
}

function check_password() {
  user=$1
  passwd=$2
  shadow_file=$3
  
  FULLHASH=$(grep "${user}:" ${shadow_file} | cut -d: -f 2)
  SALT=$(grep "^${user}:" ${shadow_file} | cut -d'$' -f3)
  PERLCOMMAND="print crypt(\"${passwd}\", \"\\\$6\\\$${SALT}\");"
  NEWHASH=$(perl -e "${PERLCOMMAND}")
  [[ "${FULLHASH}" == "${NEWHASH}" ]] && echo 0	
}

# 评分部分
project_path=/home/devops/ansible

# 切换到对应目录
cd ${project_path} &>/dev/null || (echo "${project_path} is not exist" && exit 1)

# 定义grade统计成绩
export grade=0


echo -e "\n############################# Question 1 ##############################"
echo "Install and config ansible:"

pad "Check package ansible-core and ansible-navigator installed"
if dnf list ansible-core ansible-navigator &>/dev/null ;then
  print_PASS && let grade++
else
  print_FAIL
  exit 2
fi

pad "Check file ansible.cfg exist"
[[ -e ${project_path}/ansible.cfg ]] && ansible --version |grep -q "config file = ${project_path}/ansible.cfg" && print_PASS && let grade++ || print_FAIL

# 临时导出ansible配置
ansible-config dump > /tmp/ansible-config

pad "Check ansible.cfg-roles_path"
[[ -d ${project_path}/roles ]] && cat /tmp/ansible-config|grep -q "DEFAULT_ROLES_PATH.*${project_path}/roles" && print_PASS && let grade++ || print_FAIL

pad "Check ansible.cfg-collections_path"
[[ -d ${project_path}/mycollections ]] && cat /tmp/ansible-config|grep -q "COLLECTIONS_PATHS.*${project_path}/mycollections" && print_PASS && let grade++ || print_FAIL

pad "Check ansible.cfg-become"
[[ $(ansible all -a id|grep -c root) -eq 5 ]] && print_PASS && let grade++ || print_FAIL

pad "Check ansible.cfg-inventory"
[[ -e ${project_path}/inventory ]] && cat /tmp/ansible-config | grep -q "DEFAULT_HOST_LIST.*${project_path}/inventory" && print_PASS && let grade++ || print_FAIL

# 清理临时导出的ansible配置
rm -f /tmp/ansible-config

# 临时导出inventory配置
ansible-inventory --graph > /tmp/inventory

echo "Check inventory configure:"
pad "  Check workstation in group balancers"
cat /tmp/inventory | grep -A1 balancers | grep workstation &>/dev/null && print_PASS && let grade++ || print_FAIL

pad "  Check servera in group dev"
cat /tmp/inventory | grep -A1 dev | grep servera &>/dev/null && print_PASS && let grade++ || print_FAIL

pad "  Check serverb in group test"
cat /tmp/inventory | grep -A1 test | grep serverb &>/dev/null && print_PASS && let grade++ || print_FAIL

pad "  Check serverc and serverd in group prod"
(cat /tmp/inventory | grep -A2 prod | grep serverc && cat /tmp/inventory | grep -A2 prod | grep serverd ) &>/dev/null && print_PASS && let grade++ || print_FAIL

pad "  Check prod in group webservers"
cat /tmp/inventory | grep -A1 webservers| grep prod &>/dev/null && print_PASS && let grade++ || print_FAIL

#清理临时导出的inventory配置
rm -f /tmp/inventory


echo -e "\n############################# Question 2 ##############################"
echo "Config default yum_repository:"
playbook=yum_repo.yml

pad "Check file ${playbook} exist" 
if [[ -e ${project_path}/${playbook} ]];then
  print_PASS && let grade++

  pad "Apply playbook ${playbook}"
  ansible-navigator run -m stdout ${playbook} &>/dev/null && print_DONE || print_FAIL

  pad "Check repository EX294_BASE for all nodes"
  [[ $(ansible all -a 'cat /etc/yum.repos.d/EX294_BASE.repo' | grep '\[EX294_BASE\]' |wc -l) -eq 5 ]] && [[ $(ansible all -a 'cat /etc/yum.repos.d/EX294_BASE.repo' | grep 'baseurl = http://content.example.com/rhel9.0/x86_64/dvd/BaseOS' |wc -l) -eq 5 ]] && [[ $(ansible all -a 'cat /etc/yum.repos.d/EX294_BASE.repo' | grep 'enabled = 1' |wc -l) -eq 5 ]] && [[ $(ansible all -a 'cat /etc/yum.repos.d/EX294_BASE.repo' | grep 'gpgcheck = 1' |wc -l) -eq 5 ]] && [[ $(ansible all -a 'cat /etc/yum.repos.d/EX294_BASE.repo' | grep 'gpgkey = http://content.example.com/rhel9.0/x86_64/dvd/RPM-GPG-KEY-redhat-release' |wc -l) -eq 5 ]] && [[ $(ansible all -a 'cat /etc/yum.repos.d/EX294_BASE.repo' | grep 'name = EX294 base software' |wc -l) -eq 5 ]] && print_PASS && let grade++ || print_FAIL

  pad "Check repository EX294_STREAM for all nodes"
  [[ $(ansible all -a 'cat /etc/yum.repos.d/EX294_STREAM.repo' | grep '\[EX294_STREAM\]' |wc -l) -eq 5 ]] && [[ $(ansible all -a 'cat /etc/yum.repos.d/EX294_STREAM.repo' | grep 'baseurl = http://content.example.com/rhel9.0/x86_64/dvd/AppStream' |wc -l) -eq 5 ]] && [[ $(ansible all -a 'cat /etc/yum.repos.d/EX294_STREAM.repo' | grep 'enabled = 1' |wc -l) -eq 5 ]] && [[ $(ansible all -a 'cat /etc/yum.repos.d/EX294_STREAM.repo' | grep 'gpgcheck = 1' |wc -l) -eq 5 ]] && [[ $(ansible all -a 'cat /etc/yum.repos.d/EX294_STREAM.repo' | grep 'gpgkey = http://content.example.com/rhel9.0/x86_64/dvd/RPM-GPG-KEY-redhat-release' |wc -l) -eq 5 ]] && [[ $(ansible all -a 'cat /etc/yum.repos.d/EX294_STREAM.repo' | grep 'name = EX294 stream software' |wc -l) -eq 5 ]] && print_PASS && let grade++ || print_FAIL

else
  print_FAIL
fi


echo -e "\n############################# Question 3 ##############################"
echo "Install packages with Ansible:"
playbook=packages.yml

pad "Check file packages.yml exist" 
if [[ -e ${project_path}/${playbook} ]];then
  print_PASS && let grade++ 

  pad "Apply playbook ${playbook}"
  ansible-navigator run -m stdout ${playbook} &>/dev/null && print_DONE || print_FAIL
 
 pad "Check package php and mariadb installed"  
  (ansible dev,test,prod -a 'dnf list php' && ansible dev,test,prod -a 'dnf list mariadb') &>/dev/null && print_PASS && let grade++ || print_FAIL

  pad "Check package group-RPM Development Tools installed" 
  ansible dev -a 'dnf grouplist --installed "RPM Development Tools"' &>/dev/null && print_PASS && let grade++ || print_FAIL

  pad "Check group dev has latest packages" 
  [[ $(ansible dev -a 'dnf list updates' 2>/dev/null |wc -l) -le 2 ]] && print_PASS && let grade++ || print_FAIL

else
  print_FAIL
fi


echo -e "\n############################# Question 4 ##############################"
echo "Use RHEL system roles:"

pad "Check package rhel-system-roles installed"  
dnf list rhel-system-roles &>/dev/null && print_PASS && let grade++ || print_FAIL

pad "Check role timesync exist" 
ansible-galaxy list|grep '\- timesync,' &>/dev/null && print_PASS && let grade++ || print_FAIL

# 清理默认配置
ansible all -m shell -a "systemctl disable chronyd --now && rm -f /etc/chrony.conf" &>/dev/null

playbook=timesync.yml

pad "Check file ${playbook} exist"
if [[ -e ${project_path}/${playbook} ]];then
  print_PASS && let grade++ 

  pad "Apply playbook ${playbook}"
  ansible-navigator run -m stdout ${playbook} &>/dev/null && print_DONE || print_FAIL

  pad "Check service chronyd enabled and started"
  [[ $(ansible all -a 'systemctl is-enabled chronyd' | grep '^enabled$' |wc -l) -eq 5 ]] && [[ $(ansible all -a 'systemctl is-active chronyd' | grep '^active$' |wc -l) -eq 5 ]] && print_PASS && let grade++ || print_FAIL

  pad "Check service chronyd configure"
  [[ $(ansible all -a "grep '^server 172.25.254.254 iburst$' /etc/chrony.conf"| grep '^server 172.25.254.254 iburst$'|wc -l) -eq 5 ]] && print_PASS && let grade++ || print_FAIL

else
  print_FAIL
fi


echo -e "\n############################# Question 5 ##############################"
echo "Install collections with ansible-galaxy:"
pad "Check collection-ansible.posix exist"
ls mycollections/ansible_collections/ansible/posix &>/dev/null && print_PASS && let grade++ || print_FAIL 

pad "Check collection-community.general exist"
ls mycollections/ansible_collections/community/general/ &>/dev/null && print_PASS && let grade++ || print_FAIL 

pad "Check collection-redhat.rhel_system_roles exist"
ls mycollections/ansible_collections/redhat/rhel_system_roles/ &>/dev/null && print_PASS && let grade++ || print_FAIL 


echo -e "\n############################# Question 6 ##############################"
echo "Install roles with ansible-galaxy:"

pad "Check file requirements.yml exist"
if [[ -e ${project_path}/roles/requirements.yml ]];then
  print_PASS && let grade++ 

  pad "Apply role requirements.yml"
  ansible-galaxy install -r roles/requirements.yml &>/dev/null && print_DONE || print_FAIL

  pad "Check role-balancer exist"
  ls roles/balancer/ &>/dev/null && print_PASS && let grade++ || print_FAIL 

  pad "Check role-phpinfo exist"
  ls roles/phpinfo/ &>/dev/null && print_PASS && let grade++ || print_FAIL 

  #根据tar包中file 数量判定role content是是否一致
  pad "Check roles content "
  rm -f haproxy.tar phpinfo.tar && ( wget http://materials.example.com/laoma/haproxy.tar && wget http://materials.example.com/laoma/phpinfo.tar) &>/dev/null
  [[ $(find roles/balancer/ |wc -l) -eq $(tar tf haproxy.tar |wc -l) ]] && [[ $(find roles/phpinfo/ |wc -l) -eq $(tar tf phpinfo.tar |wc -l) ]] && print_PASS && let grade++ || print_FAIL
  #清理file 
  rm -f haproxy.tar phpinfo.tar

else
  print_FAIL
fi


echo -e "\n############################# Question 7 ##############################"
echo "Create and Use role:"
playbook=newrole.yml

pad "Check file newrole.yml exist"
[[ -e ${project_path}/${playbook} ]] && print_PASS && let grade++ || print_FAIL

pad "Check role apache exist"
[[ -d ${project_path}/roles/apache ]] && print_PASS && let grade++ || print_FAIL

if [[ -e ${project_path}/${playbook} ]] && [[ -d ${project_path}/roles/apache ]];then
  pad "Apply playbook ${playbook}"
  ansible-navigator run -m stdout ${playbook} &>/dev/null && print_DONE || print_FAIL

  #根据是否只有serverc and serverd 生成 /var/www/html/index.htmlfile 判断
  pad "Check playbook only apply to webservers"
  [[ $(ansible all -a 'ls /var/www/html/index.html'|grep '^/var/www/html/index.html$' |wc -l) -eq 2 ]] && (ansible serverc -a 'ls /var/www/html/index.html' && ansible serverd -a 'ls /var/www/html/index.html' ) &>/dev/null && print_PASS && let grade++ || print_FAIL
  
  # Check Apacherole 配置
  check_apache_role webservers

else
  pad "Deploy grade environment"
  # 生成 newrole-tmp.yml 用于验证 apacherole
  cat << EOF > newrole-tmp.yml
---
- name: use apache roles
  hosts: webservers 
  roles:
    - apache
EOF

  #Apply newrole-tmp.yml
  ansible-navigator run -m stdout newrole-tmp.yml &>/dev/null && print_DONE || print_FAIL

  #Check Apacherole配置
  check_apache_role webservers

  #清理newrole-tmp.yml
  rm -f newrole-tmp.yml
fi


echo -e "\n############################# Question 8 ##############################"
echo "Use roles from ansible-galaxy installation:"
playbook=roles.yml

pad "Check file ${playbook} exist"
if [[ -e ${project_path}/${playbook} ]];then
  print_PASS && let grade++ 
  
  pad "Apply playbook ${playbook}"
  ansible-navigator run -m stdout ${playbook} &>/dev/null && print_DONE || print_FAIL

  echo "Check play for balancers role:"
  ssh root@workstation systemctl stop firewalld
  pad "  Check balancers role only apply to balancers"
  ([[ $(ansible all -a 'dnf list --installed haproxy'|grep haproxy|wc -l) -eq 1 ]] && ansible balancers -a 'dnf list --installed haproxy') &>/dev/null && print_PASS && let grade++ || print_FAIL

  pad "  Check balancers role apply result"
  [[ "$(curl http://workstation 2>/dev/null)" != "$(curl http://workstation 2>/dev/null)" ]] && print_PASS && let grade++ || print_FAIL

  echo "Check play for phpinfo role:"
  pad "  Check phpinfo role only apply to webservers"
  ([[ $(ansible all -a 'ls /var/www/html/hello.php' |grep '^/var/www/html/hello.php$'|wc -l) -eq 2 ]] && ansible webservers -a 'ls /var/www/html/hello.php') &>/dev/null && print_PASS && let grade++ || print_FAIL

  pad "  Check phpinfo role apply result"
  [[ "$(curl http://workstation/hello.php 2>/dev/null|grep 'Hello PHP World')" != "$(curl http://workstation/hello.php 2>/dev/null|grep 'Hello PHP World')" ]] && [[ "$(curl http://workstation/hello.php 2>/dev/null|grep 'PHP Version')" == "$(curl http://workstation/hello.php 2>/dev/null|grep 'PHP Version')" ]] && print_PASS && let grade++ || print_FAIL

else
  print_FAIL
fi


echo -e "\n############################# Question 9 ##############################"
echo "Create and Use LV:"
playbook=lv.yml

pad "Check file ${playbook} exist"
if [[ -e ${project_path}/${playbook} ]];then
  print_PASS && let grade++
  
  pad "Apply playbook ${playbook}"
  ansible-navigator run -m stdout ${playbook} >/tmp/lv.log 2>/dev/null && print_DONE || print_FAIL

  echo "Check servera lvm:"
  pad "  Check lv name"
  ssh root@servera ls /dev/research/data &>/dev/null && print_PASS && let grade++ || print_FAIL

  pad "  Check lv size"
  ssh root@servera lvdisplay /dev/research/data|grep -q 'Current LE.*1500$' && print_PASS && let grade++ || print_FAIL

  pad "  Check lv filesystem format"
  ssh root@servera blkid /dev/research/data | grep -q ext4 && print_PASS && let grade++ || print_FAIL

  echo "Check serverb lvm:"
  pad "  Check lv name"
  ssh root@serverb ls /dev/research/data &>/dev/null && print_PASS && let grade++ || print_FAIL

  pad "  Check lv size"
  ssh root@serverb lvdisplay /dev/research/data|grep -q 'Current LE.*200$' && print_PASS && let grade++ || print_FAIL

  pad "  Check lv filesystem format"
  ssh root@serverb blkid /dev/research/data | grep -q ext4 && print_PASS && let grade++ || print_FAIL

  pad "  Check msg for Could not create logical volume of that size"
  grep -B1 "Could not create logical volume of that size" /tmp/lv.log |grep -q serverb && print_PASS && let grade++ || print_FAIL

  pad "Check msg for Volume group does not exist"
  [[ $(grep -B1 "Volume group does not exist" /tmp/lv.log |egrep '(serverc|serverd|workstation)' |wc -l) -eq 3 ]] && print_PASS && let grade++ || print_FAIL

  # 清理 /tmp/lv.log
  rm -f /tmp/lv.log
else
  print_FAIL
fi


echo -e "\n############################# Question 10 ##############################"
echo "Generate host file:"
playbook=hosts.yml

pad "Check file hosts.j2 exist"
[[ -e ${project_path}/hosts.j2 ]] && print_PASS && let grade++ || print_FAIL

pad "Check file hosts.yml exist"
[[ -e ${project_path}/${playbook} ]] && print_PASS && let grade++ || print_FAIL

if [[ -e ${project_path}/hosts.j2 ]] && [[ -e ${project_path}/${playbook} ]] ;then
  pad "Apply playbook ${playbook}"
  ansible-navigator run -m stdout ${playbook} &>/dev/null && print_DONE || print_FAIL

  pad "Check hosts.j2 format"
  (egrep '^{% +for.*%}$' hosts.j2 && egrep '^{% +endfor +%}$' hosts.j2) &>/dev/null && print_PASS && let grade++ || print_FAIL

  pad "Check file /etc/myhosts Content"

  #准备比对file 
  cat <<EOF > /tmp/myhosts
172.25.250.10 servera.lab.example.com servera
172.25.250.11 serverb.lab.example.com serverb
172.25.250.12 serverc.lab.example.com serverc
172.25.250.13 serverd.lab.example.com serverd
172.25.250.9 workstation.lab.example.com workstation
EOF

  #比对 file content
  [[ "$(ansible dev -a 'cat /etc/myhosts'|grep '^172'|sort -n)" == "$(cat /tmp/myhosts|grep '^172')" ]] && print_PASS && let grade++ || print_FAIL

fi


echo -e "\n############################# Question 11 #############################"
echo "Modity file content:"
playbook=issue.yml

pad "Check file ${playbook} exist"
if [[ -e ${project_path}/${playbook} ]];then
  print_PASS && let grade++ 
  pad "Apply playbook ${playbook}"
  ansible-navigator run -m stdout ${playbook} &>/dev/null && print_DONE || print_FAIL

  pad "Check file content on group dev"
  [[ "$(ansible dev -a 'cat /etc/issue' | grep Development)" == "Development" ]] && print_PASS && let grade++ || print_FAIL

  pad "Check file content on group test"
  [[ "$(ansible test -a 'cat /etc/issue' | grep Test)" == "Test" ]] && print_PASS && let grade++ || print_FAIL

  pad "Check file content on group prod"
  [[ "$(ansible serverc -a 'cat /etc/issue' | grep Production)" == "Production" ]] && [[ "$(ansible serverd -a 'cat /etc/issue' | grep Production)" == "Production" ]] && print_PASS && let grade++ || print_FAIL

else
  print_FAIL
fi


echo -e "\n############################# Question 12 #############################"
echo "Create webcontent directory:"
playbook=webcontent.yml

pad "Check file ${playbook} exist"
if [[ -e ${project_path}/${playbook} ]];then
  print_PASS && let grade++ 
  
  pad "Apply playbook ${playbook}"
  ansible-navigator run -m stdout ${playbook} &>/dev/null && print_DONE || print_FAIL

  pad "Check playbook only apply to group dev"
  [[ $(ansible all -a 'ls -d /webdev' |grep -B1 '^/webdev$'|egrep '(server|workstation)'|wc -l) -eq 1 ]]&& ansible all -a 'ls -d /webdev' |grep -B1 '^/webdev$'|grep -q servera && print_PASS && let grade++ || print_FAIL

  pad "Check /webdev permission"
  [[ "$(ansible dev -a 'ls -ld /webdev' | grep webdev | awk '{print $4}')" == "webdev" ]] && [[ "$(ansible dev -a 'stat -c %a /webdev' |grep -v servera)" == "2775" ]] && print_PASS && let grade++ || print_FAIL

  pad "Check /var/www/html/webdev link to /webdev"
  ansible dev -a 'ls -ld /var/www/html/webdev' |grep -q '/var/www/html/webdev -> /webdev' && print_PASS && let grade++ || print_FAIL

  pad "Check file /webdev/index.html content"
  [[ "$(ansible dev -a 'cat /webdev/index.html' |grep -v servera )" == "Development" ]] && print_PASS && let grade++ || print_FAIL

  pad "Check website http://servera.lab.example.com/webdev/"
  [[ "$(curl http://servera.lab.example.com/webdev/ 2>/dev/null)" == "Development" ]] && print_PASS && let grade++ || print_FAIL

else
  print_FAIL
fi


echo -e "\n############################# Question 13 #############################"
echo "Generate hardware reports:"
playbook=hwreport.yml

pad "Check file ${playbook} exist"
if [[ -e ${project_path}/${playbook} ]];then
  print_PASS && let grade++ 
  pad "Apply playbook ${playbook}"
  ansible-navigator run -m stdout ${playbook} &>/dev/null && print_DONE || print_FAIL

  pad "Deploy grade environment"
  # 准备正确的playbook
  cat << EOF >  /tmp/hwreport-new.yml
---
- name: Create hardware report
  hosts: all
  tasks:
    - name: download hwreport.txt
      get_url:
        url: http://materials.example.com/laoma/hwreport.empty
        dest: /root/hwreport-new.txt
        force: yes
    - name: hostname
      lineinfile:
        path: /root/hwreport-new.txt
        regexp: '^HOST='
        line: 'HOST={{ inventory_hostname }}'
    - name: memory
      lineinfile:
        path: /root/hwreport-new.txt
        regexp: '^MEMORY='
        line: 'MEMORY={{ ansible_memtotal_mb }}'
    - name: BIOS version
      lineinfile:
        path: /root/hwreport-new.txt
        regexp: '^BIOS='
        line: 'BIOS={{ ansible_bios_version }}'
    - name: vda size
      lineinfile:
        path: /root/hwreport-new.txt
        regexp: '^DISK_SIZE_VDA='
        line: 'DISK_SIZE_VDA={{ ansible_devices.vda.size | default("NONE")}}'
    - name: vdb size 
      lineinfile:
        path: /root/hwreport-new.txt
        regexp: '^DISK_SIZE_VDB='
        line: 'DISK_SIZE_VDB={{ ansible_devices.vdb.size | default("NONE")}}'
EOF

  #Use 正确的playbook，生成正确的比对file 
  ansible-navigator run -m stdout /tmp/hwreport-new.yml &>/dev/null && print_DONE || print_FAIL
  rm -f /tmp/hwreport-new.yml
  
  #Check 主机apply效果
  for host in server{a..d} workstation
  do
    echo "Check hwreport.txt content on $host:"
    pad "  Check inventory_name"
    [[ "$(ssh root@$host cat /root/hwreport.txt | grep 'HOST=')" == "$(ssh root@$host cat /root/hwreport-new.txt | grep 'HOST=')" ]] && print_PASS && let grade++ || print_FAIL

    pad "  Check total memory size"
    [[ "$(ssh root@$host cat /root/hwreport.txt | grep 'MEMORY=')" == "$(ssh root@$host cat /root/hwreport-new.txt | grep 'MEMORY=')" ]] && print_PASS && let grade++ || print_FAIL

    pad "  Check BIOS version"
    [[ "$(ssh root@$host cat /root/hwreport.txt | grep 'BIOS=')" == "$(ssh root@$host cat /root/hwreport-new.txt | grep 'BIOS=')" ]] && print_PASS && let grade++ || print_FAIL

    pad "  Check vda size"
    [[ "$(ssh root@$host cat /root/hwreport.txt | grep 'DISK_SIZE_VDA=')" == "$(ssh root@$host cat /root/hwreport-new.txt | grep 'DISK_SIZE_VDA=')" ]] && print_PASS && let grade++ || print_FAIL

    pad "  Check vdb size"
    [[ "$(ssh root@$host cat /root/hwreport.txt | grep 'DISK_SIZE_VDB=')" == "$(ssh root@$host cat /root/hwreport-new.txt | grep 'DISK_SIZE_VDB=')" ]] && print_PASS && let grade++ || print_FAIL
    
  done

else
  print_FAIL
fi


echo -e "\n############################# Question 14 #############################"
echo "Create Ansible Vault file:"

pad "Check file secret.txt exist"
[[ -e ${project_path}/secret.txt ]] && print_PASS && let grade++ || print_FAIL

pad "Check file secret.txt content"
[[ "$( cat ${project_path}/secret.txt)" == "laoma" ]] && print_PASS && let grade++ || print_FAIL

pad "Check file locker.yml exist"
[[ -e ${project_path}/locker.yml ]] && print_PASS && let grade++ || print_FAIL

pad "Check file locker.yml content"
ansible-vault view ${project_path}/locker.yml --vault-password-file=${project_path}/secret.txt |egrep -q 'pw_developer: +Imadev' && ansible-vault view ${project_path}/locker.yml --vault-password-file=${project_path}/secret.txt |egrep -q 'pw_manager: +Imamgr' && print_PASS && let grade++ || print_FAIL


echo -e "\n############################# Question 15 #############################"
echo "Create user:"
playbook=users.yml

pad "Check file user_list.yml exist"
[[ -e ${project_path}/user_list.yml ]] && print_PASS && let grade++ || print_FAIL

pad "Check file ${playbook} exist"
[[ -e ${project_path}/${playbook} ]] && print_PASS && let grade++ || print_FAIL

if [[ -e ${project_path}/user_list.yml ]] && [[ -e ${project_path}/${playbook} ]];then
  pad "Apply playbook ${playbook}"
  ansible-navigator run -m stdout ${playbook} --vault-password-file=${project_path}/secret.txt &>/dev/null && print_DONE || print_FAIL

  for SERVER in servera serverb serverc serverd
  do
    if [[ "${SERVER}" == "servera" || "${SERVER}" == "serverb" ]];then
      user=john
      uid=3001
      passwd=Imadev
      group=devops
	  
      echo "Check user and group configure on ${SERVER}:"
      pad "  Check group ${group} exist"
      ansible ${SERVER} -a 'cat /etc/group'|grep -q "^${group}:" && print_PASS && let grade++ || print_FAIL
	  
      pad "  Check user ${user} exist"
      ansible ${SERVER} -a "id ${user}" &>/dev/null && print_PASS && let grade++ || print_FAIL
	  
      pad "  Check user ${user}-supplementary group ${group}"
      [[ "$(ansible ${SERVER} -a 'cat /etc/group'|grep ${user}|egrep -v "^[[:alnum:]]*${user}[[:alnum:]]*:"|awk -F ':' '{print $1}')" == "${group}" ]] && print_PASS && let grade++ || print_FAIL

      pad "  Check user ${user}-uid ${uid}"
      [[ $(ssh ${SERVER} -a "id -u ${user}") -eq ${uid} ]] && print_PASS && let grade++ || print_FAIL

      pad "  Check user ${user}-password"
      ansible ${SERVER} -m fetch -a 'src=/etc/shadow dest=/tmp' &>/dev/null
	  [[ "$(check_password ${user} ${passwd} /tmp/${SERVER}/etc/shadow)" -eq 0 ]] && print_PASS && let grade++ || print_FAIL
    
	else
      user="james mary"
      passwd=Imamgr
      group=opsmgr
	  
	  echo "Check ${SERVER} group and user configure:"
      pad "  Check group ${group} exist"
      ansible ${SERVER} -a 'cat /etc/group'|grep -q "^${group}:" && print_PASS && let grade++ || print_FAIL
	  
	  for username in ${user}
	  do
        # 根据user名称设置uid
        if [ "$username" == "james" ];then
          uid=2001
        else
          uid=2002
        fi
        
        pad "  Check user ${user} exist"
        ansible ${SERVER} -a "id ${username}" &>/dev/null && print_PASS && let grade++ || print_FAIL
	    
        pad "  Check user ${username}-supplementary group ${group}"
        [[ "$(ansible ${SERVER} -a 'cat /etc/group'|grep ${username}|egrep -v "^[[:alnum:]]*${username}[[:alnum:]]*:"|awk -F ':' '{print $1}')" == "${group}" ]] && print_PASS && let grade++ || print_FAIL

        pad "  Check user ${user}-uid $uid"
        [[ $(ssh ${SERVER} -a "id -u ${username}") -eq $uid ]] && print_PASS && let grade++ || print_FAIL

        pad "  Check user ${username}-password"
        ansible ${SERVER} -m fetch -a 'src=/etc/shadow dest=/tmp' &>/dev/null
	    [[ "$(check_password ${username} ${passwd} /tmp/${SERVER}/etc/shadow)" -eq 0 ]] && print_PASS && let grade++ || print_FAIL
	  done
	fi
  done

else
  print_FAIL
fi


echo -e "\n############################# Question 16 #############################"
echo "Update ansible vault password:"

pad "Check file salaries.yml exist"
[[ -e ${project_path}/salaries.yml ]] && print_PASS && let grade++ || print_FAIL

# 准备新的密码file 
echo redhat > /tmp/secret-new.txt

pad "Check vault password for salaries.yml"
[[ "$(ansible-vault view salaries.yml --vault-password-file=/tmp/secret-new.txt)" == "hello laoma" ]] && print_PASS && let grade++ || print_FAIL


echo -e "\n############################# Question 17 #############################"
echo "Config cron job:"
playbook=cron.yml

pad "Check file ${playbook} exist"
if [[ -e ${project_path}/${playbook} ]];then
  print_DONE && let grade++ 
  
  pad "Apply playbook ${playbook}"
  ansible-navigator run -m stdout ${playbook} &>/dev/null && print_DONE || print_FAIL

  pad "Check cron job for all nodes"
  [[ $(ansible all -a 'crontab -u devops -l' | egrep -c '\*/2 \* \* \* \*.*logger.*EX294 in progress') -eq 5 ]] && &>/dev/null && print_PASS && let grade++

else
  print_FAIL
fi

#定义考点数量
all=132
echo -e "\n***********************************************************************"
echo "scale=2;${grade}/${all}*300 " | bc |awk -F'.' '{print $1}' > /tmp/grade
echo -e "*****************************  GRADE: \033[1;31m$(cat /tmp/grade)\033[0;39m *****************************"
echo "***********************************************************************"