我有一個 Web 應用程式及其配置的串列。簡化的資料結構看起來像這樣(字典串列)
web_app_details:
- web_sourcedir: UWT_Optimus_UI
web_destdir: 'E:\alexsapp'
loadUserProfile: false
- web_sourcedir: UWT_Optimus_UI
web_destdir: 'E:\bodhi'
現在,有一些默認值我不希望用戶每次都指定(單個字典)
defaults_only:
identityType: LocalSystem
enable32BitAppOnWin64: false
loadUserProfile: true
如果用戶未在defaults_only
字典中的任何欄位中提供值,則應應用默認值。否則,應丟棄默認值。
所以結果是:
web_app_details:
- web_sourcedir: UWT_Optimus_UI
web_destdir: 'E:\alexsapp'
identityType: LocalSystem
enable32BitAppOnWin64: false
loadUserProfile: false # Because user provided the override value
- web_sourcedir: UWT_Optimus_UI
web_destdir: 'E:\bodhi'
identityType: LocalSystem
enable32BitAppOnWin64: false
loadUserProfile: true
我嘗試了一些事情,例如
- 呼叫 Powershell 腳本在 Ansible 之外執行此操作
- 復制默認字典多次
web_app_detail
,然后使用組合過濾器 null
使用 JMESPath 查詢插入值:
- name: Pad web_app_details with null values
set_fact:
web_app_details_with_defaults: "{{ web_app_details | json_query(jmesquery) }}"
vars:
jmesquery:
"[*].{web_sourcedir: web_sourcedir, web_destdir: web_destdir, web_cleancopy: web_cleancopy }"
但沒有任何效果。
有人可以幫忙嗎?
uj5u.com熱心網友回復:
您可以重新創建該串列,添加帶有combine
過濾器的默認值。
這將利用在兩個字典中定義的屬性將被您正在組合的字典覆寫的事實。
所以,你最終會完成這個set_fact
任務:
- set_fact:
web_app_details_with_defaults: >-
{{
web_app_details_with_defaults | default([]) [
defaults_only | combine(item)
]
}}
loop: "{{ web_app_details }}"
鑒于以下幾個任務:
- set_fact:
web_app_details_with_defaults: >-
{{
web_app_details_with_defaults | default([]) [
defaults_only | combine(item)
]
}}
loop: "{{ web_app_details }}"
vars:
defaults_only:
identityType: LocalSystem
enable32BitAppOnWin64: false
loadUserProfile: true
web_app_details:
- web_sourcedir: UWT_Optimus_UI
web_destdir: 'E:\alexsapp'
loadUserProfile: false
- web_sourcedir: UWT_Optimus_UI
web_destdir: 'E:\bodhi'
- debug:
var: web_app_details_with_defaults
這產生了預期的:
web_app_details_with_defaults:
- enable32BitAppOnWin64: false
identityType: LocalSystem
loadUserProfile: false
web_destdir: E:\alexsapp
web_sourcedir: UWT_Optimus_UI
- enable32BitAppOnWin64: false
identityType: LocalSystem
loadUserProfile: true
web_destdir: E:\bodhi
web_sourcedir: UWT_Optimus_UI
uj5u.com熱心網友回復:
有多種方法可以解決這個問題,但我首選的解決方案是:
- hosts: localhost
vars:
web_app_details:
- web_sourcedir: UWT_Optimus_UI
web_destdir: 'E:\alexsapp'
loadUserProfile: false
- web_sourcedir: UWT_Optimus_UI
web_destdir: 'E:\bodhi'
defaults_only:
identityType: LocalSystem
enable32BitAppOnWin64: false
loadUserProfile: true
web_app_details_with_defaults: "{{ [defaults_only] | product(web_app_details) | map('combine') }}"
tasks:
- debug:
msg: "{{ web_app_details_with_defaults }}"
該運算式{{ [defaults_only] | product(web_app_details) | map('combine') }}
創建串列的乘積[defaults_only]
(行內單元素串列)和web_app_details
,然后用于map
將每個字典串列組合成一個字典。
PLAY [localhost] ***************************************************************
TASK [debug] *******************************************************************
ok: [localhost] => {
"msg": [
{
"enable32BitAppOnWin64": false,
"identityType": "LocalSystem",
"loadUserProfile": false,
"web_destdir": "E:\\alexsapp",
"web_sourcedir": "UWT_Optimus_UI"
},
{
"enable32BitAppOnWin64": false,
"identityType": "LocalSystem",
"loadUserProfile": true,
"web_destdir": "E:\\bodhi",
"web_sourcedir": "UWT_Optimus_UI"
}
]
}
如果您使用的是沒有自動展開的古老版本的 Ansible,您將需要| list
在運算式中的某些點添加以將生成器轉換為串列。可能只是{{ [defaults_only] | product(web_app_details) | map('combine') | list }}
,但我沒有測驗過這個。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/479566.html
標籤:列表 可靠的 nshashtable
上一篇:過濾包含特定字串的串列