RHCE习题( 三 )

7、从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 版本等 。
解答:[student@workstation ansible]$ vim roles.yml---- name: gather facts for webservershosts: webservers//获取webservers的事实变量,因为你要在webservers主机组上平衡WEB服务器的负载 。- name: balancer rolehosts: balancersroles:- balancer- name: php rolehosts: webserversroles:- phpinfo再来执行该playbook[student@workstation ansible]$ ansible-playbook roles.yml 验证:[student@workstation ansible]$ curl http://bastion.lab.example.comWelcome to serverc.lab.example.com on 172.25.250.12[student@workstation ansible]$ curl http://bastion.lab.example.comWelcome to serverd.lab.example.com on 172.25.250.13[student@workstation ansible]$ curl http://serverc.lab.example.com/hello.phpHello PHP World form serverc.lab.example.com[student@workstation ansible]$ curl http://serverd.lab.example.com/hello.phpHello PHP World form serverd.lab.example.com8、创建和使用逻辑卷创建一个名为/home/student/ansible/lv.yml 的playbook,它将在所有受管节点上运行以执行下列任务:创建符合以下要求的逻辑卷:逻辑卷创建在research卷组中逻辑卷名称为data逻辑卷大小为1500MiB使用ext4文件系统格式化逻辑卷如果无法创建请求的逻辑卷大小,应显示错误消息Could not create logical volume of that size,并且应改为使用大小 800MiB 。如果卷组research 不存在,应显示错误消息Volume group does not exist 。不要以任何方式挂载逻辑卷
前期环境首先执行lvm_pre.yml[student@workstation ansible]$ ansible-playbook lvm_pre.yml
答题:[student@workstation ansible]$ vim lv.yml---- name: create lvmhosts: alltasks:- name: create lv datablock:- name: create lv 1500Mlvol:lv: datavg: researchsize: 1500Mrescue:- name: output fail messagedebug:msg: Could not create logical volume of that size- name: create lv 800Mlvol:lv: datavg: researchsize: 800Malways:- name: format lvfilesystem:dev: /dev/research/datafstype: ext4when: "'research' in ansible_lvm.vgs"- name: search not existsdebug:msg: Volume group does not existwhen: "'research' not in ansible_lvm.vgs"[student@workstation ansible]$ ansible-playbook lv.yml创建和使用分区创建名为partition.yml的playbook,对所有节点进行操作:在vdb上创建一个主分区1500MiB使用ext4文件系统进行格式化将文件系统挂载到/newpart如果分区大小不满足,产生报错信息 could not create partition os that size则创建分区大小变成800MiB如果磁盘不存在,产生报错信息:diskdoes not exist
[student@workstation ansible]$ vim partition.yml---- name: create partitionhosts: alltasks:- name: create part1block:- name: create part 1500parted:device: /dev/vdbnumber: 1part_type: primarypart_start: 10MiBpart_end: 1510MiBstate: presentrescue:- name: output fail messagedebug:msg: could not create partition os that size- name: create part 800parted:device: /dev/vdbnumber: 1part_type: primarypart_start: 10MiBpart_end: 800MiBstate: presentalways:- name: format partfilesystem:dev: /dev/vdb1fstype: ext4- name: create mount pointfile:path: /newpartstate: directory- name: mountmount:src: /dev/vdb1path: /newpartfstype: ext4state: mountedwhen: "ansible_devices.vdb is defined"- name: vdb not existdebug:msg: diskdoes not existwhen: "ansible_devices.vdb is not defined"[student@workstation ansible]$ ansible-playbook partition.yml由于练习环境原因,此playbook无法正常运行 。

经验总结扩展阅读