我有 Ansible 用來部署資料的 delivery.yaml :
---
- name: Deploy to Filer on host
hosts: host
tasks:
- name: Copy files
copy: src={{ item.src }} dest={{ item.dest }} mode=0775
with_items:
- { src: 'data1.xml', dest: '' }
- { src: 'data2.so', dest: '' }
- { src: 'data3.exe', dest: '' }
- { src: 'data4.lib', dest: '' }
我需要根據資料擴展名完成“dest”值:
xml files => target1
so files => target2
exe files => target3
lib files => target4
怎么寫?我不習慣使用 groovy 語言。目前,我寫了這個,但它不起作用:
stage('YAML test'){
steps{
script{
yamlData = readYaml file: 'delivery.yaml'
yamlData.tasks.with_items.find{it.src.endsWith("xml")}.dest="target1"
writeYaml file: 'delivery_temp.yaml', data: yamlData
sh "cat delivery_temp.yaml"
}
}
}
我收到此錯誤:
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.endsWith() is applicable for argument types: (java.lang.String) values: [xml]
uj5u.com熱心網友回復:
這里的問題是,您最外層的資料型別已經是一個串列。所以 Groovy 將使用隱式擴展運算子。這里隱含的意思是:
yamlData*.tasks*.with_items.find{ it*.src.endWith ... }
所以你實際上想要對最外層的每個元素進行轉換:
yamlData.each{ it.tasks.each { task -> task.with_items.find{it.src.endsWith("xml")}.dest="target1" } }
不管find這里是正確的依賴于資料(這只會找到第一個,如果沒有,則它會失敗)。很可能是您想要的findAll(這會生成所有結果的串列,并且.dest由于隱式擴展運算子,
分配仍然有效)。
為了擺脫對所有四種型別(或將有多少種型別)執行此操作的復制和粘貼代碼,您最好將轉換提取為資料(例如從后綴到目標的映射)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/363196.html
上一篇:如何在Javascript中用地圖替換forEach
下一篇:在詹金斯中讀取xml
