RHCE习题( 三 )


解答:[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无法正常运行 。9、生成主机文件将一个初始模板文件从http://content.example.com/hosts.j2下载到/home/student/ansible完成该模板 , 以便用它生成以下文件:针对每个清单主机包含一行内容 , 其格式与 /etc/hosts 相同创建名为 /home/student/ansible/hosts.yml 的playbook , 它将使用此模板在 dev 主机组中的主机上生成文件 /etc/myhosts 。该 playbook 运行后,dev 主机组中主机上的文件/etc/myhosts 应针对每个受管主机包含一行内容:127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6172.24.1.6 servera.lab1.example.com servera172.24.1.7 serverb.lab1.example.com serverb172.24.1.8 serverc.lab1.example.com serverc172.24.1.9 serverd.lab1.example.com serverd172.24.1.10 bastion.lab1.example.com bastion
解答:[student@workstation ansible]$ wget http://content.example.com/hosts.j2[student@workstation ansible]$ vim hosts.j2127.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 %}
[student@workstation ansible]$ vim hosts.yml

  • name: get all factshosts: all
  • name: cp to myhostshosts: devtasks:
    • name: cp filetemplate:src: /home/student/ansible/hosts.j2dest: /etc/myhosts
验证:[root@servera ~]# cat /etc/myhosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6172.25.250.10servera.lab.example.comservera172.25.250.11serverb.lab.example.comserverb172.25.250.254bastion.lab.example.combastion172.25.250.12serverc.lab.example.comserverc172.25.250.13serverd.lab.example.comserverd

推荐阅读