RHCE習題( 四 )


10、修改文件內容按照下方所述,創建一個名為 /home/student/ansible/issue.yml 的 playbook:該 playbook 將在所有清單主機上運行該 playbook 會將 /etc/issue 的內容替換為下方所示的一行文本:在 dev 主機組中的主機上,這行文本顯示為:Development在 test 主機組中的主機上,這行文本顯示為:Test在 prod 主機組中的主機上,這行文本顯示為:Production
解答:[student@workstation ansible]$ vim issue.yml---- name: modify issuehosts: alltasks:- name: input to issuecopy:content: |{% if 'dev' in group_names %}Development{% elif 'test' in group_names %}Test{% elif 'prod' in group_names %}Production{% endif %}dest: /etc/issue[student@workstation ansible]$ ansible-playbook issue.yml 驗證:[root@servera ~]# cat /etc/issueDevelopment[root@serverb ~]# cat /etc/issueTest[root@serverc ~]# cat /etc/issueProduction[root@serverd ~]# cat /etc/issueProduction11、創建Web內容目錄按照下方所述,創建一個名為 /home/student/ansible/webcontent.yml 的 playbook:該 playbook 在 dev 主機組中的受管節點上運行創建符合下列要求的目錄 /webdev:所有者為 devops 組具有常規權限:owner=read+write+execute,group=read+write+execute,other=read+execute具有特殊權限: set group ID用符號鏈接將 /var/www/html/webdev 鏈接到 /webdev創建文件 /webdev/index.html,其中包含如下所示的單行文本:Development在 dev 主機組中主機上瀏覽此目錄(例如 http://servera.lab.example.com/webdev/ )將生成以下輸出:Development
解答:[student@workstation ansible]$ vim webcontent.yml---- name: web stationhosts: devtasks:- name: install httpd firewalldyum:name:- httpd- firewalldstate: present- name: create groupgroup:name: devopsstate: present- name: create /webdevfile:path: /webdevstate: directorygroup: devopsmode: 2775- name: cpcopy:content: Developmentdest: /webdev/index.html- name: set selinux contextsefcontext:target: /webdev(/.*)?setype: httpd_sys_content_t- name: shellshell:cmd: restorecon -Rv /webdev- name: create link to /var/www/html/webdevfile:src: /webdevdest: /var/www/html/webdevstate: link- name: restart httpdservice:name: httpdstate: restartedenabled: yes- name: restart firewalldservice:name: firewalldstate: restartedenabled: yes- name: firewall for httpfirewalld:service: httpstate: enabledpermanent: yesimmediate: yes[student@workstation ansible]$ ansible-playbook webcontent.yml 驗證:[student@workstation ansible]$ curl http://servera.lab.example.com/webdev/Development12、生成硬件報告創建一個名為 /home/student/ansible/hwreport.yml的 playbook , 它將在所有受管節點上生成含有以下信息的輸出文件 /root/hwreport.txt:
輸出文件中的每一行含有一個 key=value 對 。您的 playbook 應當:從 http://content.example.com/hwreport.empty 下載文件,并將它保存為/root/hwreport.txt使用正確的值修改 /root/hwreport.txt如果硬件項不存在,相關的值應設為NONE
解答:[student@workstation ansible]$ vim hwreport.yml---- name: get hwreporthosts: alltasks:- name: Create report fileget_url:url: http://content.example.com/hwreport.emptydest: /root/hwreport.txt- name: get inventory_hostnamereplace:path: /root/hwreport.txtregexp: 'inventoryhostname'replace: "{{ inventory_hostname }}"- name: get memreplace:path: /root/hwreport.txtregexp: 'memory_in_MB'replace: "{{ ansible_memtotal_mb }}"- name: get biosreplace:path: /root/hwreport.txtregexp: 'BIOS_version'replace: "{{ ansible_bios_version }}"- name: get vdareplace:path: /root/hwreport.txtregexp: 'disk_vda_size'replace: "{{ ansible_devices.vda.size if ansible_devices.vda is defined else 'NONE'}}"- name: get vdbreplace:path: /root/hwreport.txtregexp: 'disk_vdb_size'replace: "{{ ansible_devices.vdb.size if ansible_devices.vdb is defined else 'NONE'}}"[student@workstation ansible]$ ansible-playbook hwreport.yml13、創建密碼庫按照下方所述,創建一個 Ansible 庫來存儲用戶密碼:庫名稱為 /home/student/ansible/locker.yml庫中含有兩個變量 , 名稱如下:pw_developer,值為 Imadevpw_manager,值為 Imamgr用于加密和解密該庫的密碼為whenyouwishuponastar密碼存儲在文件 /home/student/ansible/secret.txt中
解答:[student@workstation ansible]$ vim locker.yml---pw_developer: lmadevpw_manager: lmamgr[student@workstation ansible]$ echo whenyouwishuponastar > secret.txt[student@workstation ansible]$ chmod 600 secret.txt[student@workstation ansible]$ ansible-vault encrypt locker.yml --vault-id=/home/student/ansible/secret.txt 14、創建用戶賬戶從 http://content.example.com/user_list.yml 下載要創建的用戶的列表 , 并將它保存到 /home/student/ansible在本次考試中使用在其他位置創建的密碼庫 /home/student/ansible/locker.yml,創建名為/home/student/ansible/users.yml 的playbook,從而按以下所述創建用戶帳戶:職位描述為 developer 的用戶應當:在 dev 和 test 主機組中的受管節點上創建從 pw_developer 變量分配密碼,密碼有效期為30天是附加組 student 的成員職位描述為 manager 的用戶應當:在 prod 主機組中的受管節點上創建從 pw_manager 變量分配密碼,密碼有效期為30天是附加組 opsmgr 的成員密碼應采用 SHA512 哈希格式 。您的 playbook 應能夠在本次考試中使用在其他位置創建的庫密碼文件/home/student/ansible/secret.txt 正常運行

推薦閱讀