ansible实用技巧演示(单循环/多循环)
ansible 单循环语句:
- hosts: web5
name: play-test
vars:
test:
- test1
- test2
- test3
- test4
tasks:
- name: debug test
debug:
msg: "{{ item }}"
with_items: "{{ test }}"
单循环跑出来如下:
joulong@joulong1:~/playbook$ ansible-playbook playbook4.yml -i inventory.ini
PLAY [play-test] *********************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
ok: [joulong2]
ok: [joulong3]
TASK [debug test] ********************************************************************************************************************************************************************************************
ok: [joulong2] => (item=test1) => {
"msg": "test1"
}
ok: [joulong2] => (item=test2) => {
"msg": "test2"
}
ok: [joulong2] => (item=test3) => {
"msg": "test3"
}
ok: [joulong2] => (item=test4) => {
"msg": "test4"
}
ok: [joulong3] => (item=test1) => {
"msg": "test1"
}
ok: [joulong3] => (item=test2) => {
"msg": "test2"
}
ok: [joulong3] => (item=test3) => {
"msg": "test3"
}
ok: [joulong3] => (item=test4) => {
"msg": "test4"
}
ansible 多循环语句:
- hosts: web5
name: play-test
vars:
test:
- test1
- test2
- test3
- test4
demo:
- demo1
- demo2
- demo3
- name: debug test
debug:
msg: "{{ item[0] }},{{ item[1] }}"
with_nested:
- "{{test}}"
- "{{demo}}"
多循环跑出来如下:
joulong@joulong1:~/playbook$ ansible-playbook playbook4.yml -i inventory.ini
PLAY [play-test] *********************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
ok: [joulong2]
ok: [joulong3]
TASK [debug test] ********************************************************************************************************************************************************************************************
ok: [joulong2] => (item=['test1', 'demo1']) => {
"msg": "test1,demo1"
}
ok: [joulong2] => (item=['test1', 'demo2']) => {
"msg": "test1,demo2"
}
ok: [joulong3] => (item=['test1', 'demo1']) => {
"msg": "test1,demo1"
}
ok: [joulong3] => (item=['test1', 'demo2']) => {
"msg": "test1,demo2"
}
ok: [joulong2] => (item=['test1', 'demo3']) => {
"msg": "test1,demo3"
}
ok: [joulong3] => (item=['test1', 'demo3']) => {
"msg": "test1,demo3"
}
ok: [joulong2] => (item=['test2', 'demo1']) => {
"msg": "test2,demo1"
}
ok: [joulong3] => (item=['test2', 'demo1']) => {
"msg": "test2,demo1"
}
ok: [joulong2] => (item=['test2', 'demo2']) => {
"msg": "test2,demo2"
}
ok: [joulong3] => (item=['test2', 'demo2']) => {
"msg": "test2,demo2"
}
ok: [joulong2] => (item=['test2', 'demo3']) => {
"msg": "test2,demo3"
}
ok: [joulong3] => (item=['test2', 'demo3']) => {
"msg": "test2,demo3"
}
ok: [joulong2] => (item=['test3', 'demo1']) => {
"msg": "test3,demo1"
}
ok: [joulong3] => (item=['test3', 'demo1']) => {
"msg": "test3,demo1"
}
ok: [joulong2] => (item=['test3', 'demo2']) => {
"msg": "test3,demo2"
}
ok: [joulong3] => (item=['test3', 'demo2']) => {
"msg": "test3,demo2"
}
ok: [joulong2] => (item=['test3', 'demo3']) => {
"msg": "test3,demo3"
}
ok: [joulong3] => (item=['test3', 'demo3']) => {
"msg": "test3,demo3"
}
ok: [joulong2] => (item=['test4', 'demo1']) => {
"msg": "test4,demo1"
}
ok: [joulong3] => (item=['test4', 'demo1']) => {
"msg": "test4,demo1"
}
ok: [joulong2] => (item=['test4', 'demo2']) => {
"msg": "test4,demo2"
}
ok: [joulong3] => (item=['test4', 'demo2']) => {
"msg": "test4,demo2"
}
ok: [joulong2] => (item=['test4', 'demo3']) => {
"msg": "test4,demo3"
}
ok: [joulong3] => (item=['test4', 'demo3']) => {
"msg": "test4,demo3"
}