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

推薦閱讀