簡體   English   中英

Ansible 劇本:在變量中循環

[英]Ansible playbook : loop in variable

我想創建一個循環來檢查文件是否存在於特定位置。

這是我的ansible劇本:

- name: Launch purge script
  hosts: roi-asb-01p
  vars_files:
    - instance_test.yml

  tasks:
    - name: Check if file test.php exists
      stat:
        path: "/home/alekso/instance_test/prod/{{ instance_all }}/test.php"
      register: result
        #  loop: "{{ instance_all }}"
    - name : DEBUG FILE EXISTS
      debug:
        msg: "The file test.php exist"
      when: result.stat.exists
      loop: "{{ instance_all }}"
    - name: DEBUG FILE NOT EXISTS
      debug:
        msg: "The file test.php doesn't exist"
      when: not result.stat.exists
      loop: "{{ instance_all }}"

問題是對於路徑,它在我的 instance_test.yml 文件中包含我的“instance_all”變量的每個實例,因此它檢測到不存在文件,因為路徑不好

路徑突然看起來像這樣: /home/test/instance_test/prod/[one, two, tree]/test.php而不是/home/test/instance_test/prod/one/test.php等...

這是我的不同變量的文件:

instance_all:
  - one
  - two
  - tree

instance_1:
  - test1

instance_2:
  - test2

這是命令輸出:

test@srv-test-1:~/ansible/test$ ansible-playbook -i inventaire.ini launch-purge-script-test.yml
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
[WARNING]: An error occurred while calling ansible.utils.display.initialize_locale (unsupported locale setting). This may result in incorrectly calculated text widths
that can cause Display to print incorrect line lengths

PLAY [Launch purge script] ***********************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************
ok: [roi-asb-01p]

TASK [Check if file test.php exists] *************************************************************************************************************************************
ok: [roi-asb-01p]

TASK [debug] *************************************************************************************************************************************************************
skipping: [roi-asb-01p] => (item=one) 
skipping: [roi-asb-01p] => (item=two) 
skipping: [roi-asb-01p] => (item=tree) 
skipping: [roi-asb-01p]

TASK [debug] *************************************************************************************************************************************************************
ok: [roi-asb-01p] => (item=one) => {
    "msg": "The file test.php doesn't exist"
}
ok: [roi-asb-01p] => (item=two) => {
    "msg": "The file test.php doesn't exist"
}
ok: [roi-asb-01p] => (item=tree) => {
    "msg": "The file test.php doesn't exist"
}

PLAY RECAP ***************************************************************************************************************************************************************
roi-asb-01p                : ok=3    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0  

感謝幫助

變量instance_all包含您要循環的所有元素。 但是每次迭代都需要一個元素。 這包含在 loop_var 中(默認情況下它的名稱是item )。 這將在你的情況下做到這一點:

path: /home/test/instance_test/prod/{{ item }}/test.php

當然,您還應該從帶有# loop:的行中刪除注釋符號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM