iT邦幫忙

0

ansible 學習筆記5

  • 分享至 

  • xImage
  •  

說明:
傳參數學完了,接下來便是循環語句了,跟學各種程式語言類似

ansible 單循環語句:

- hosts: web5
  name: play-test

  vars:
    test:
      - test1
      - test2
      - test3  
      - test4
  
  tasks: 
  - name: debug test
    debug:
      msg: "{{ item }}" #item 是關鍵字 
    with_items: "{{ test }}"   #with_items 單循環 

單循環跑出來如下:

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}}"        #單循環   
    msg: "{{ item[0] }},{{ item[1] }}" #多循環
# with_items: "{{ test }}" #單循環  
  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"
}


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言