Anible-playbook作业

作业:
在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]$ cat hwreport.empty
hostname:inventoryhostname
mem:memory_in_MB
bios:BIOS_version
sda:disk_sda_size
[student@ansible ansible]$ vim hwreport.yml
[student@ansible ansible]$ vim hwreport.yml
[student@ansible 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: "NODE"
[student@ansible ansible]$ cat pass 
alone
[student@ansible ansible]$ ansible-vault encrypt hwreport.yml --vault-id pass
Encryption successful
[student@ansible ansible]$ ansible-playbook hwreport.yml --vault-id pass

PLAY [exercise2] ******************************************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************************************
ok: [192.168.253.134]
ok: [node1]
ok: [node2]
ok: [node3]

TASK [copy hwreport.empty] ********************************************************************************************************************************
changed: [node1]
changed: [node2]
ok: [192.168.253.134]
changed: [node3]

TASK [inventoryhostname] **********************************************************************************************************************************
changed: [192.168.253.134]
ok: [node1]
changed: [node2]
changed: [node3]

TASK [memory] *********************************************************************************************************************************************
changed: [node1]
ok: [192.168.253.134]
changed: [node2]
changed: [node3]

TASK [BIOS] ***********************************************************************************************************************************************
changed: [node1]
ok: [192.168.253.134]
changed: [node2]
changed: [node3]

TASK [disk] ***********************************************************************************************************************************************
changed: [192.168.253.134]
ok: [node1]
changed: [node2]
changed: [node3]

PLAY RECAP ************************************************************************************************************************************************
192.168.253.134            : ok=6    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node1                      : ok=6    changed=3    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   

验证

[root@node1 ~]# cat hwreport.txt
hostname:192.168.253.134
mem:1800
bios:6.00
sda:NODE

[root@node2 ~]# cat hwreport.txt
hostname:node2
mem:3752
bios:6.00
sda:NODE

[root@node3 ~]# cat hwreport.txt 
hostname:node3
mem:1800
bios:6.00
sda:NODE

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值