ansible练习题:
创建一个名为
/home/skk/ansible/hwreport.yml
的 playbook
它将在所有受管节点上生成含有以下信息的输出文件/root/hwreport.txt
清单主机名称
以 MB 表示的总内存大小
BIOS 版本
磁盘设备 vda 的大小
磁盘设备 vdb 的大小
输出文件中的每一行含有一个 key=value 对
您的 playbook 应当:
从http://172.25.36.250/hwreport.empty
下载文件,并将它保存为/root/hwreport.txt
;
使用正确的值改为/root/hwreport.txt
;
如果硬件项不存在,相关的值应设为NONE
。
1、下载报告模板查看
wget http://172.25.36.250/hwreport.empty
cat hwreport.empty
2、编写playbook
vim /home/skk/ansible/hwreport.yml
---
- name: create hwreport
hosts: all
tasks:
- name: get hwreport
get_url: ##使用get_url下载报告模板
url: http://172.25.36.250/hwreport.empty
dest: /root/hwreport.txt
force: yes
- name: vim hwreport
lineinfile: ##使用lineinfile模块替换文件内容
path: /root/hwreport.txt
regexp: "{{item.name}}"
line: "{{item.name}}={{item.value}}"
loop: ##使用loop循环获取变量
- name: HOST
value: "{{inventory_hostname}}"
- name: MEMERY
value: "{{ansible_facts['memtotal_mb']}}MB"
- name: BIOS
value: "{{ansible_facts['bios_version']}}"
- name: DISK_SIZE_VDA
value: "{{ansible_facts['devices']['vda']['size'] | default('NONE')}}" ##注意{{}}的使用
- name: DISK_SIZE_VDB
value: "{{ansible_facts['devices']['vdb']['size'] | default('NONE')}}" ##当不存在时输出NONE
3、执行playbook
ansible-playbook --syntax-check hwreport.yml ##语法检测
ansible-playbook hwreport.yml
4、远程连接查看
ansible all -m shell -a "cat /root/hwreport.txt"