Ansible练习三
练习:
1、在ansible节点中新建/home/student/ansible/hwreport.empty的文件,文件内容为
hostname: inventoryhostname
mem: memory_in_MB
bios: BIOS_version
sda: disk_sda_size
2、创建一个名为 /home/student/ansible/hwreport.yml的 playbook,它将在所有受管节点上生成含有以下信息的输出文件 /root/hwreport.txt:
输出文件中的每一行含有一个 key=value 对。
您的 playbook 应当:
从 ansible节点中复制hwreport.empty文件到每台受控主机,并将它保存为/root/hwreport.txt
使用正确的值修改 /root/hwreport.txt
然后将/home/student/ansible/hwreport.yml这个playbook进行加密,加密的密码保存在/home/student/ansible/pass文件中,密码为abcdefg
执行该playbook,实现需求。
创建hwreport.empty
[root@server ~]# su - student
Last login: Thu Oct 27 03:33:21 CST 2022 on pts/1
[student@server ~]$ cd ansible/
[student@server ansible]$ vim hwreport.empty
[student@server ansible]$ cat hwreport.empty
hostname:inventoryhostname
mem:memory_in_MB
bios:BIOS_version
sda:disk_sda_size
创建 hwreport.yml
[student@server ansible]$ vim hwreport.yml
[student@server ansible]$ cat hwreport.yml
---
- name: exercise2
hosts: all
tasks:
- name: copy hwreport.empty
copy:
src: /home/student/ansible/hwreport.empty
dest: /root/hwreport.txt
- name: inventoryhostname
replace:
path: /root/hwreport.txt
regexp: 'inventoryhostname'
replace: "{{ inventory_hostname }}"
- name: memory
replace:
path: /root/hwreport.txt
regexp: 'memory_in_MB'
replace: "{{ ansible_memtotal_mb | string }}"
- name: BIOS
replace:
path: /root/hwreport.txt
regexp: 'BIOS_version'
replace: "{{ ansible_bios_version }}"
- name: disk
replace:
path: /root/hwreport.txt
regexp: 'disk_sda_size'
replace: "{{ ansible_devices.sda.size }}"
加密 playbook
[student@server ansible]$ vim pass
[student@server ansible]$ cat pass
abcdefg
[student@server ansible]$ ansible-vault encrypt hwreport.yml --vault-id pass
Encryption successful
执行 playbook
[student@server ansible]$ ansible-playbook hwreport.yml --vault-id pass
PLAY [exercise2] *******************************************************************************
TASK [Gathering Facts] *************************************************************************
ok: [node2]
ok: [node3]
ok: [node1]
TASK [copy hwreport.empty] *********************************************************************
changed: [node2]
changed: [node3]
changed: [node1]
TASK [inventoryhostname] ***********************************************************************
changed: [node3]
changed: [node1]
changed: [node2]
TASK [memory] **********************************************************************************
changed: [node1]
changed: [node2]
changed: [node3]
TASK [BIOS] ************************************************************************************
changed: [node3]
changed: [node2]
changed: [node1]
TASK [disk] ************************************************************************************
changed: [node2]
changed: [node1]
changed: [node3]
PLAY RECAP *************************************************************************************
node1 : ok=6 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=6 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node3 : ok=6 changed=5 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
验证
[student@server ansible]$ ansible-vault view hwreport.yml
Vault password:
---
- name: exercise2
hosts: all
tasks:
- name: copy hwreport.empty
copy:
src: /home/student/ansible/hwreport.empty
dest: /root/hwreport.txt
- name: inventoryhostname
replace:
path: /root/hwreport.txt
regexp: 'inventoryhostname'
replace: "{{ inventory_hostname }}"
- name: memory
replace:
path: /root/hwreport.txt
regexp: 'memory_in_MB'
replace: "{{ ansible_memtotal_mb | string }}"
- name: BIOS
replace:
path: /root/hwreport.txt
regexp: 'BIOS_version'
replace: "{{ ansible_bios_version }}"
- name: disk
replace:
path: /root/hwreport.txt
regexp: 'disk_sda_size'
replace: "{{ ansible_devices.sda.size }}"
(END)
[root@node1 ~]# cat hwreport.txt
hostname:node1
mem:791
bios:6.00
sda:20.00 GB
[root@node2 ~]# cat hwreport.txt
hostname:node2
mem:1789
bios:6.00
sda:20.00 GB
[root@node3 ~]# cat hwreport.txt
hostname:node3
mem:1789
bios:6.00
sda:20.00 GB