1、使用动态清单模板,修改其内容,要求如下:
(1)node1是test主机组的成员,其中test主机组可以使用变量: aa=11 bb=22
(2)node2和node3是prod主机组的成员,其中prod主机组可以使用的变量: cc=33 dd=44
(3)node1还可以使用的变量:nodevar=liuxiang1
(4)node2还可以使用的变量:nodevar=liuxiang2
(5)node3还可以使用的变量:nodevar=liuxiang3
(6)撰写一个test.yml的playbook,要求所有的受控主机输出变量nodevar的值
[student@ansible ~/ansible]$vim inventory.py
......
return {
'test': {
'hosts': ['node1'],
'vars': {
'aa': '11',
'bb': '22'
}
},
'prod': {
'hosts': ['node2', 'node3'],
'vars': {
'cc': '33',
'dd': '44'
}
},
'_meta': {
'hostvars': {
'node1': {
'nodevar': 'liuxiang1'
},
'node2': {
'nodevar': 'liuxiang2'
},
'node3': {
'nodevar': 'liuxiang3'
}
}
}
}
......
[student@ansible ~/ansible]$cd playbook/
[student@ansible ~/ansible/playbook]$vim test1.yaml
---
- hosts: all
tasks:
- name: debug test 1
debug:
msg: "just for test ansible dynamic inventory"
- name: debug test 2
debug:
msg: "{{ nodevar }}"
[student@ansible ~/ansible]$ansible-playbook
/home/student/ansible/playbook/test1.yaml -i inventory.py
PLAY [all]
********************************************************************************
*****************************
TASK [Gathering Facts]
********************************************************************************
*****************
ok: [node3]
ok: [node1]
ok: [node2]
TASK [debug test 1]
********************************************************************************
********************
ok: [node1] => {
"msg": "just for test ansible dynamic inventory"
}
ok: [node2] => {
"msg": "just for test ansible dynamic inventory"
}
ok: [node3] => {
"msg": "just for test ansible dynamic inventory"
}
TASK [debug test 2]
********************************************************************************
********************
ok: [node2] => {
"msg": "liuxiang2"
}
ok: [node1] => {
"msg": "liuxiang1"
}
ok: [node3] => {
"msg": "liuxiang3"
}
PLAY RECAP
********************************************************************************
*****************************
node1 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node3 :ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
标签:ok,练习,nodevar,ansible,test,node1,msg
From: https://www.cnblogs.com/Archer-x/p/16853008.html