ansible变量练习
作业:
在ansible节点中新建/home/student/ansible/hwreport.empty的文件,文件内容为
hostname: inventoryhostname
mem: memory_in_MB
bios: BIOS_version
sda: disk_sda_size
创建一个名为 /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,实现需求
[student@ansible ansible]$ vim hwreport.empty
hostname: inventoryhostname
mem: memory_in_MB
bios: BIOS_version
sda: disk_sda_size
[student@ansible ansible]$ vim hwreport.yml
---
- name: hwreport
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: men
replace:
path: /root/hwreport.txt
regexp: memory_in_MB
replace: "{{ ansible_memtotal_mb }}"
- name: bios
replace:
path: /root/hwreport.txt
regexp: BIOS_version
replace: "{{ ansible_bios_version }}"
- name: sda
replace:
path: /root/hwreport.txt
regexp: disk_sda_size
replace: "{{ ansible_devices.sda.size }}"
[student@ansible ansible]$ ansible-vault encrypt hwreport.yml
New Vault password:
Confirm New Vault password:
Encryption successful
[student@ansible ansible]$ echo abcdefg > /home/student/ansible/pass
[student@ansible ansible]$ ansible-playbook --vault-id /home/student/ansible/pass hwreport.yml
[student@ansible ansible]$ ansible-playbook --vault-id /home/student/ansible/pass hwreport.yml
PLAY [hwreport] *************************************************************************
TASK [Gathering Facts] ******************************************************************
ok: [node1]
ok: [node3]
ok: [node2]
TASK [copy hwreport.empty] **************************************************************
changed: [node2]
changed: [node1]
changed: [node3]
TASK [inventoryhostname] ****************************************************************
changed: [node1]
changed: [node2]
changed: [node3]
TASK [bios] *****************************************************************************
changed: [node2]
changed: [node3]
changed: [node1]
TASK [sda] ******************************************************************************
changed: [node2]
changed: [node1]
changed: [node3]
PLAY RECAP ******************************************************************************
node1 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node3 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律