我有一个看起来像这样的ansible dict:
library(xgboost)
library(Matrix)
labels <- data.frame(labels = sample.int(2, m*1, TRUE)-1L)
observations <- Matrix(as.matrix(data.frame(feat_01=sample.int(2, size=100, T) -1,
feat_02=sample.int(2, size=100, T) -1,
feat_03=sample.int(2, size=100, T) -1,
feat_04=sample.int(2, size=100, T) -1,
feat_05=sample.int(2, size=100, T) -1)), sparse=T)
dtrain <- xgb.DMatrix(data = observations, label = labels$labels)
bstResult <- xgb.train(data = dtrain,
nthread = 1,
nround = 4,
max_depth = 3,
verbose = T,
objective = "binary:logistic",
booster='gbtree')
xgb.dump(bstResult)
xgb.plot.tree(model = bstResult, n_first_tree = 2)
我想要一个ansible / jinja2模板来输出这个:
servers:
- name: foo
port: 1000
- name: bar
port: 2000
到目前为止,我有类似的事情:
result=pre-foo-1000,pre-bar-1000
但只输出:
result={{ servers | json_query('[*].name') | join(',') }}
我尝试了类似result=foo,bar
的内容但没有成功 - 它提供了有关无效json_query('[*].name-[*].port')
字面值的错误。我在json_query上找不到很多文档,但是我能在那里做更多的文档吗?或者更好的选择滑入过滤器?
答案 0 :(得分:7)
你可以先做一个普通循环,然后收集结果:
- hosts: all
connection: local
vars:
servers:
- name: foo
port: 1000
- name: bar
port: 2000
tasks:
- set_fact:
result_item: '{{ item.name }}-{{ item.port }}'
with_items:
- '{{ servers }}'
register: result_list
- set_fact:
result: '{{ result_list.results | map(attribute="ansible_facts.result_item") | join(",") }}'
- debug:
var: result
或者尝试一些内联jinja循环:
- hosts: all
connection: local
vars:
servers:
- name: foo
port: 1000
- name: bar
port: 2000
tasks:
- set_fact:
result: "{% for item in servers %}{{item.name}}-{{item.port}}{{ '' if loop.last else ',' }}{% endfor %}"
- debug:
var: result
这也应该在模板文件中起作用:
result={% for item in servers %}{{item.name}}-{{item.port}}{{ '' if loop.last else ',' }}{% endfor %}