*ansible变量*
定义变量规则:由字母/数字/下划线组成,变量需要以字母开头,ansible内置的关键字不能作为变量名。
ansible中,可以将变量简化为三个范围Global范围(高):从命令行和ansible配置设置的变量play范围(中):在play和相关结构中设置的变量Host范围(低):inventory、facts或register的变量,在主机组和个别主机上设置的变量三个范围的变量优先级依次降低,如果变量重复定义,则以优先级高的为准
*注册和定义变量的各种方式*
ansible中定义变量的方式有很多种,大致有:
(1) 将模块的执⾏结果注册为变量;
(2) 直接定义字典类型的变量;
(3) role中⽂件内定义变量;
(4) 命令⾏传递变量;
(5) 借助with_items迭代将多个task的结果赋值给⼀个变量;
(6) inventory中的主机或主机组变量;
(7) 内置变量。
在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]$ 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 }}"
[student@ansible ansible]$ vim pass
[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: [node2]
ok: [node1]
ok: [node3]
TASK [copy hwreport.empty]
********************************************************************************
*************
changed: [node3]
changed: [node1]
changed: [node2]
TASK [inventoryhostname]
********************************************************************************
***************
changed: [node2]
changed: [node1]
changed: [node3]
TASK [memory]
********************************************************************************
**************************
changed: [node2]
changed: [node1]
changed: [node3]
TASK [BIOS]
********************************************************************************
****************************
changed: [node1]
changed: [node3]
changed: [node2]
TASK [disk]
********************************************************************************
****************************
changed: [node1]
changed: [node3]
changed: [node2]
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
[root@node1 ~]#cat hwreport.txt
hostname:node1
mem:1789
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