RHCE習題( 二 )

【RHCE習題】[student@workstation ansible]$ vim packages.yml---- name: install pkgshosts: dev, test, prodtasks:- name: install mariadb phpyum:name:- php- mariadbstate: present- name: install group pkgshosts: devtasks:- name: install Development Toolsyum:name: "@Development Tools"state: present- name: update all pkgshosts: devtasks:- name: update pkgsyum:name: '*'state: latest[student@workstation ansible]$ ansible-playbook packages.yml4、使用RHEL系統角色安裝 RHEL 系統角色軟件包,并創建符合以下條件的playbook /home/student/ansible/timesync.yml:在所有受管節點上運行使用 timesync 角色配置該角色,以使用當前有效的 NTP 提供商配置該角色,以使用時間服務器 classroom.example.com配置該角色,以啟用 iburst 參數
解答:[student@workstation ansible]$ sudo yum -y install rhel-system-roles[student@workstation ansible]$ mkdir roles[student@workstation ansible]$ cp -r /usr/share/ansible/roles/rhel-system-roles.timesync//home/student/ansible/roles/timesync[student@workstation ansible]$ vim timesync.yml---- name: set time synchosts: allvars:timesync_ntp_servers:- hostname: classroom.example.comiburst: yesroles:- timesync[student@workstation ansible]$ ansible-playbook timesync.yml使用selinux角色配置該角色,開啟所有受控節點的selinux[student@workstation ansible]$ cp -r /usr/share/ansible/roles/rhel-system-roles.selinux/home/student/ansible/roles/selinuxvim selinux.yml---- name: set selinuxhosts: allvars:selinux_state: enforcingroles:- role: selinuxbecome: true[student@workstation ansible]$ ansible-playbook selinux.yml5、使用Ansible Galaxy安裝角色使用 Ansible Galaxy 和要求文件 /home/student/ansible/roles/requirements.yml,從以下 URL 下載角色并安裝到 /home/student/ansible/roles:http://content.example.com/haproxy.tar.gz 此角色的名稱應當為 balancerhttp://content.example.com/phpinfo.tar.gz 此角色的名稱應當為 phpinfo
解答:[student@workstation ansible]$ vim roles/requirements.yml---- name: balancersrc: http://content.example.com/ansible2.8/haproxy.tar.gz- name: phpinfosrc: http://content.example.com/ansible2.8/phpinfo.tar.gz[student@workstation ansible]$ ansible-galaxy install -r /home/student/asnible/roles/requirements.yml -p /home/student/ansible/roles/6、創建和使用角色根據下列要求,在/home/student/ansible/roles中創建名為apache的角色:httpd軟件包已安裝,設為在系統啟動時啟用并啟動防火墻已啟用并正在運行,并使用允許訪問Web服務器的規則模板文件 index.html.j2 已存在,用于創建具有以下輸出的文件/var/www/html/index.html:Welcome to HOSTNAME on IPADDRESS其中,HOSTNAME是受管節點的完全限定域名 , IPADDRESS則是受管節點的IP地址 。按照下方所述,創建一個使用此角色的playbook /home/student/ansible/newrole.yml:該playbook在webservers主機組中的主機上運行
解答:[student@workstation ansible]$ cd roles/[student@workstation roles]$ ansible-galaxy init apache[student@workstation roles]$ vim http/tasks/main.yml---# tasks file for http- name: install httpd firewalldyum:name:- httpd- firewalldstate: present- name: cp filetemplate:src: index.html.j2dest: /var/www/html/index.html- name: start httpdservice:name: httpdstate: startedenabled: yes- name: restart firewalldservice:name: firewalldstate: restartedenabled: yes - name: firewalld for httpfirewalld:service: httpstate: enabledpermanent: yesimmediate: yes[student@workstation roles]$ vim http/templates/index.html.j2Welcome to {{ansible_fqdn}} on {{ansible_enp1s0.ipv4.address}} [student@workstation ansible]$ vim newrole.yml---- name: use http rolehosts: webserversroles:- apache[student@workstation ansible]$ ansible-playbook newrole.yml驗證結果:[student@workstation ansible]$ curl http://servercWelcome to serverc.lab.example.com on 172.25.250.12[student@workstation ansible]$ curl http://serverdWelcome to serverd.lab.example.com on 172.25.250.137、從Ansible Galaxy使用角色根據下列要求 , 創建一個名為 /home/student/ansible/roles.yml的playbook:playbook中包含一個play,該play在balancers主機組中的主機上運行并將使用balancer角色 。此角色配置一項服務,以在webservers主機組中的主機之間平衡Web服務器請求的負載 。瀏覽到balancers主機組中的主機(例如http://bastion.lab.example.com/ )將生成以下輸出:Welcome to serverc.example.com on 172.25.1.12重新加載瀏覽器將從另一Web服務器生成輸出:Welcome to serverd.example.com on 172.25.1.13playbook 中包含一個 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 版本等 。

推薦閱讀